From e752a3f7835afd42c30fe4b612a31a067d7b8322 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sun, 31 Mar 2024 15:21:19 +0200 Subject: [PATCH] handle special-case program name '[' --- complete/src/bash.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/complete/src/bash.rs b/complete/src/bash.rs index 37b7711..265c2c0 100644 --- a/complete/src/bash.rs +++ b/complete/src/bash.rs @@ -10,10 +10,14 @@ use crate::{Command, Flag}; /// Also, pretend that files are fine in any position. ValueHints are ignored entirely. pub fn render(c: &Command) -> String { let mut out = String::new(); - let name = &c.name; + // Be careful around the program '['! + let name_identifier = if c.name == "[" { &"bracket" } else { &c.name }; // Register _comp_uu_FOO as a bash function that computes completions: - out.push_str(&format!("complete -F _comp_uu_{name} {name};")); - out.push_str(&format!("_comp_uu_{name}()")); + out.push_str(&format!( + "complete -F _comp_uu_{name_identifier} '{}';", + &c.name + )); + out.push_str(&format!("_comp_uu_{name_identifier}()")); // Unless the current argument starts with "-", pre-populate the completions list with all files and dirs: out.push_str("{ local cur;_init_completion||return;COMPREPLY=();if [[ \"$cur\" != \"-*\" ]]; then _filedir;fi;COMPREPLY+=($(compgen -W \""); for arg in &c.args { @@ -59,6 +63,22 @@ mod test { ], ..Command::default() }; - assert_eq!(render(&c), "complete -F _comp_uu_foo foo;_comp_uu_foo(){ local cur;_init_completion||return;COMPREPLY=();if [[ \"$cur\" != \"-*\" ]]; then _filedir;fi;COMPREPLY+=($(compgen -W \"-a --all -x \" -- \"$cur\"));}\n") + assert_eq!(render(&c), "complete -F _comp_uu_foo 'foo';_comp_uu_foo(){ local cur;_init_completion||return;COMPREPLY=();if [[ \"$cur\" != \"-*\" ]]; then _filedir;fi;COMPREPLY+=($(compgen -W \"-a --all -x \" -- \"$cur\"));}\n") + } + + #[test] + fn bracket() { + let c = Command { + name: "[", + args: vec![Arg { + short: vec![Flag { + flag: "x", + value: Value::No, + }], + ..Arg::default() + }], + ..Command::default() + }; + assert_eq!(render(&c), "complete -F _comp_uu_bracket '[';_comp_uu_bracket(){ local cur;_init_completion||return;COMPREPLY=();if [[ \"$cur\" != \"-*\" ]]; then _filedir;fi;COMPREPLY+=($(compgen -W \"-x \" -- \"$cur\"));}\n") } }