Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(complete): Add descriptions to dynamic Zsh completions #5775

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions clap_complete/src/env/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function _clap_dynamic_completer_NAME() {
)}")

if [[ -n $completions ]]; then
compadd -a completions
_describe 'values' completions
fi
}

Expand Down Expand Up @@ -398,8 +398,31 @@ compdef _clap_dynamic_completer_NAME BIN"#
if i != 0 {
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
}
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
write!(
buf,
"{}",
Self::escape_value(&candidate.get_value().to_string_lossy())
)?;
if let Some(help) = candidate.get_help() {
write!(
buf,
":{}",
Self::escape_help(help.to_string().lines().next().unwrap_or_default())
)?;
}
}
Ok(())
}
}

impl Zsh {
/// Escape value string
fn escape_value(string: &str) -> String {
string.replace('\\', "\\\\").replace(':', "\\:")
}

/// Escape help string
fn escape_help(string: &str) -> String {
string.replace('\\', "\\\\")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function _clap_dynamic_completer_exhaustive() {
)}")

if [[ -n $completions ]]; then
compadd -a completions
_describe 'values' completions
fi
}

Expand Down
28 changes: 22 additions & 6 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,12 @@ fn complete_dynamic_env_toplevel() {
let input = "exhaustive \t\t";
let expected = snapbox::str![[r#"
% exhaustive
--generate --help action help last quote
--global --version alias hint pacman value
--generate -- generate
--global -- everywhere
--help -- Print help
--version -- Print version
help -- Print this message or the help of the given subcommand(s)
action alias hint last pacman quote value
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand All @@ -213,9 +217,18 @@ fn complete_dynamic_env_quoted_help() {
let input = "exhaustive quote \t\t";
let expected = snapbox::str![[r#"
% exhaustive quote
--backslash --choice --global --version cmd-brackets cmd-single-quotes
--backticks --double-quotes --help cmd-backslash cmd-double-quotes escape-help
--brackets --expansions --single-quotes cmd-backticks cmd-expansions help
--global -- everywhere
--help -- Print help (see more with '--help')
--version -- Print version
cmd-backslash --backslash -- Avoid '/n'
cmd-backticks --backticks -- For more information see `echo test`
cmd-brackets --brackets -- List packages [filter]
cmd-double-quotes --double-quotes -- Can be "always", "auto", or "never"
cmd-expansions --expansions -- Execute the shell command with $SHELL
cmd-single-quotes --single-quotes -- Can be 'always', 'auto', or 'never'
escape-help -- /tab/t"'
help -- Print this message or the help of the given subcommand(s)
--choice
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down Expand Up @@ -260,7 +273,10 @@ fn complete_dynamic_env_quoted_value() {
let input = "exhaustive quote --choice \t\t";
let expected = snapbox::str![[r#"
% exhaustive quote --choice
another/ shell bash fish zsh
another shell -- something with a space
bash -- bash (shell)
fish -- fish shell
zsh -- zsh shell
"#]];
let actual = runtime.complete(input, &term).unwrap();
assert_data_eq!(actual, expected);
Expand Down