Skip to content

Commit e752a3f

Browse files
BenWiederhakecakebaker
authored andcommitted
handle special-case program name '['
1 parent 1b5405c commit e752a3f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

complete/src/bash.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ use crate::{Command, Flag};
1010
/// Also, pretend that files are fine in any position. ValueHints are ignored entirely.
1111
pub fn render(c: &Command) -> String {
1212
let mut out = String::new();
13-
let name = &c.name;
13+
// Be careful around the program '['!
14+
let name_identifier = if c.name == "[" { &"bracket" } else { &c.name };
1415
// Register _comp_uu_FOO as a bash function that computes completions:
15-
out.push_str(&format!("complete -F _comp_uu_{name} {name};"));
16-
out.push_str(&format!("_comp_uu_{name}()"));
16+
out.push_str(&format!(
17+
"complete -F _comp_uu_{name_identifier} '{}';",
18+
&c.name
19+
));
20+
out.push_str(&format!("_comp_uu_{name_identifier}()"));
1721
// Unless the current argument starts with "-", pre-populate the completions list with all files and dirs:
1822
out.push_str("{ local cur;_init_completion||return;COMPREPLY=();if [[ \"$cur\" != \"-*\" ]]; then _filedir;fi;COMPREPLY+=($(compgen -W \"");
1923
for arg in &c.args {
@@ -59,6 +63,22 @@ mod test {
5963
],
6064
..Command::default()
6165
};
62-
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")
66+
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")
67+
}
68+
69+
#[test]
70+
fn bracket() {
71+
let c = Command {
72+
name: "[",
73+
args: vec![Arg {
74+
short: vec![Flag {
75+
flag: "x",
76+
value: Value::No,
77+
}],
78+
..Arg::default()
79+
}],
80+
..Command::default()
81+
};
82+
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")
6383
}
6484
}

0 commit comments

Comments
 (0)