Skip to content

Commit e95ed39

Browse files
authored
Merge pull request #5775 from vivienm/master
chore(complete): Add descriptions to dynamic Zsh completions
2 parents 18f8d4c + 81d9268 commit e95ed39

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

clap_complete/src/env/shells.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ function _clap_dynamic_completer_NAME() {
361361
)}")
362362
363363
if [[ -n $completions ]]; then
364-
compadd -a completions
364+
_describe 'values' completions
365365
fi
366366
}
367367
@@ -398,8 +398,31 @@ compdef _clap_dynamic_completer_NAME BIN"#
398398
if i != 0 {
399399
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
400400
}
401-
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
401+
write!(
402+
buf,
403+
"{}",
404+
Self::escape_value(&candidate.get_value().to_string_lossy())
405+
)?;
406+
if let Some(help) = candidate.get_help() {
407+
write!(
408+
buf,
409+
":{}",
410+
Self::escape_help(help.to_string().lines().next().unwrap_or_default())
411+
)?;
412+
}
402413
}
403414
Ok(())
404415
}
405416
}
417+
418+
impl Zsh {
419+
/// Escape value string
420+
fn escape_value(string: &str) -> String {
421+
string.replace('\\', "\\\\").replace(':', "\\:")
422+
}
423+
424+
/// Escape help string
425+
fn escape_help(string: &str) -> String {
426+
string.replace('\\', "\\\\")
427+
}
428+
}

clap_complete/tests/snapshots/home/dynamic-env/exhaustive/zsh/zsh/_exhaustive

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function _clap_dynamic_completer_exhaustive() {
1111
)}")
1212

1313
if [[ -n $completions ]]; then
14-
compadd -a completions
14+
_describe 'values' completions
1515
fi
1616
}
1717

clap_complete/tests/testsuite/zsh.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ fn complete_dynamic_env_toplevel() {
192192
let input = "exhaustive \t\t";
193193
let expected = snapbox::str![[r#"
194194
% exhaustive
195-
--generate --help action help last quote
196-
--global --version alias hint pacman value
195+
--generate -- generate
196+
--global -- everywhere
197+
--help -- Print help
198+
--version -- Print version
199+
help -- Print this message or the help of the given subcommand(s)
200+
action alias hint last pacman quote value
197201
"#]];
198202
let actual = runtime.complete(input, &term).unwrap();
199203
assert_data_eq!(actual, expected);
@@ -213,9 +217,18 @@ fn complete_dynamic_env_quoted_help() {
213217
let input = "exhaustive quote \t\t";
214218
let expected = snapbox::str![[r#"
215219
% exhaustive quote
216-
--backslash --choice --global --version cmd-brackets cmd-single-quotes
217-
--backticks --double-quotes --help cmd-backslash cmd-double-quotes escape-help
218-
--brackets --expansions --single-quotes cmd-backticks cmd-expansions help
220+
--global -- everywhere
221+
--help -- Print help (see more with '--help')
222+
--version -- Print version
223+
cmd-backslash --backslash -- Avoid '/n'
224+
cmd-backticks --backticks -- For more information see `echo test`
225+
cmd-brackets --brackets -- List packages [filter]
226+
cmd-double-quotes --double-quotes -- Can be "always", "auto", or "never"
227+
cmd-expansions --expansions -- Execute the shell command with $SHELL
228+
cmd-single-quotes --single-quotes -- Can be 'always', 'auto', or 'never'
229+
escape-help -- /tab/t"'
230+
help -- Print this message or the help of the given subcommand(s)
231+
--choice
219232
"#]];
220233
let actual = runtime.complete(input, &term).unwrap();
221234
assert_data_eq!(actual, expected);
@@ -260,7 +273,10 @@ fn complete_dynamic_env_quoted_value() {
260273
let input = "exhaustive quote --choice \t\t";
261274
let expected = snapbox::str![[r#"
262275
% exhaustive quote --choice
263-
another/ shell bash fish zsh
276+
another shell -- something with a space
277+
bash -- bash (shell)
278+
fish -- fish shell
279+
zsh -- zsh shell
264280
"#]];
265281
let actual = runtime.complete(input, &term).unwrap();
266282
assert_data_eq!(actual, expected);

0 commit comments

Comments
 (0)