From fba7c8597bf3028400d6f6a22d83cedb7a2bd5a5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 16 Feb 2024 16:16:36 -0600 Subject: [PATCH 1/2] test(complete): Show newline issue --- clap_complete/tests/snapshots/basic.elvish | 6 ++++-- clap_complete/tests/snapshots/basic.fish | 6 ++++-- clap_complete/tests/snapshots/basic.ps1 | 6 ++++-- clap_complete/tests/snapshots/basic.zsh | 6 ++++-- clap_complete/tests/snapshots/custom_bin_name.elvish | 6 ++++-- clap_complete/tests/snapshots/custom_bin_name.fish | 6 ++++-- clap_complete/tests/snapshots/custom_bin_name.ps1 | 6 ++++-- clap_complete/tests/snapshots/custom_bin_name.zsh | 6 ++++-- clap_complete/tests/testsuite/common.rs | 12 +++++++----- clap_complete_fig/tests/common.rs | 12 +++++++----- clap_complete_fig/tests/snapshots/basic.fig.js | 4 ++-- clap_complete_nushell/tests/common.rs | 4 ++-- clap_complete_nushell/tests/snapshots/basic.nu | 2 ++ .../tests/snapshots/feature_sample.nu | 3 +++ .../tests/snapshots/special_commands.nu | 3 +++ .../tests/snapshots/sub_subcommands.nu | 3 +++ clap_mangen/tests/common.rs | 12 +++++++----- clap_mangen/tests/snapshots/basic.bash.roff | 1 + 18 files changed, 69 insertions(+), 35 deletions(-) diff --git a/clap_complete/tests/snapshots/basic.elvish b/clap_complete/tests/snapshots/basic.elvish index 8d1f725e..b9736473 100644 --- a/clap_complete/tests/snapshots/basic.elvish +++ b/clap_complete/tests/snapshots/basic.elvish @@ -22,7 +22,8 @@ set edit:completion:arg-completer[my-app] = {|@words| cand -v 'v' cand -h 'Print help' cand --help 'Print help' - cand test 'Subcommand' + cand test 'Subcommand +with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'my-app;test'= { @@ -32,7 +33,8 @@ set edit:completion:arg-completer[my-app] = {|@words| cand --help 'Print help' } &'my-app;help'= { - cand test 'Subcommand' + cand test 'Subcommand +with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'my-app;help;test'= { diff --git a/clap_complete/tests/snapshots/basic.fish b/clap_complete/tests/snapshots/basic.fish index 43cd26d4..e269f6e1 100644 --- a/clap_complete/tests/snapshots/basic.fish +++ b/clap_complete/tests/snapshots/basic.fish @@ -1,10 +1,12 @@ complete -c my-app -n "__fish_use_subcommand" -s c complete -c my-app -n "__fish_use_subcommand" -s v complete -c my-app -n "__fish_use_subcommand" -s h -l help -d 'Print help' -complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand' +complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand +with a second line' complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c my-app -n "__fish_seen_subcommand_from test" -s d complete -c my-app -n "__fish_seen_subcommand_from test" -s c complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help' -complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand' +complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand +with a second line' complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' diff --git a/clap_complete/tests/snapshots/basic.ps1 b/clap_complete/tests/snapshots/basic.ps1 index 5cb77fa0..d561b8bd 100644 --- a/clap_complete/tests/snapshots/basic.ps1 +++ b/clap_complete/tests/snapshots/basic.ps1 @@ -25,7 +25,8 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand +with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } @@ -37,7 +38,8 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { break } 'my-app;help' { - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand +with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } diff --git a/clap_complete/tests/snapshots/basic.zsh b/clap_complete/tests/snapshots/basic.zsh index 408bb7e4..0a0e4cfe 100644 --- a/clap_complete/tests/snapshots/basic.zsh +++ b/clap_complete/tests/snapshots/basic.zsh @@ -68,7 +68,8 @@ esac (( $+functions[_my-app_commands] )) || _my-app_commands() { local commands; commands=( -'test:Subcommand' \ +'test:Subcommand +with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'my-app commands' commands "$@" @@ -76,7 +77,8 @@ _my-app_commands() { (( $+functions[_my-app__help_commands] )) || _my-app__help_commands() { local commands; commands=( -'test:Subcommand' \ +'test:Subcommand +with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'my-app help commands' commands "$@" diff --git a/clap_complete/tests/snapshots/custom_bin_name.elvish b/clap_complete/tests/snapshots/custom_bin_name.elvish index 734c6cb3..80b6bf5f 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.elvish +++ b/clap_complete/tests/snapshots/custom_bin_name.elvish @@ -22,7 +22,8 @@ set edit:completion:arg-completer[bin-name] = {|@words| cand -v 'v' cand -h 'Print help' cand --help 'Print help' - cand test 'Subcommand' + cand test 'Subcommand +with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'bin-name;test'= { @@ -32,7 +33,8 @@ set edit:completion:arg-completer[bin-name] = {|@words| cand --help 'Print help' } &'bin-name;help'= { - cand test 'Subcommand' + cand test 'Subcommand +with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'bin-name;help;test'= { diff --git a/clap_complete/tests/snapshots/custom_bin_name.fish b/clap_complete/tests/snapshots/custom_bin_name.fish index 5652ac0a..a75e468e 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.fish +++ b/clap_complete/tests/snapshots/custom_bin_name.fish @@ -1,10 +1,12 @@ complete -c bin-name -n "__fish_use_subcommand" -s c complete -c bin-name -n "__fish_use_subcommand" -s v complete -c bin-name -n "__fish_use_subcommand" -s h -l help -d 'Print help' -complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand' +complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand +with a second line' complete -c bin-name -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c bin-name -n "__fish_seen_subcommand_from test" -s d complete -c bin-name -n "__fish_seen_subcommand_from test" -s c complete -c bin-name -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help' -complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand' +complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand +with a second line' complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' diff --git a/clap_complete/tests/snapshots/custom_bin_name.ps1 b/clap_complete/tests/snapshots/custom_bin_name.ps1 index d65e8c32..bf6002be 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.ps1 +++ b/clap_complete/tests/snapshots/custom_bin_name.ps1 @@ -25,7 +25,8 @@ Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock { [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand +with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } @@ -37,7 +38,8 @@ Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock { break } 'bin-name;help' { - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand +with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } diff --git a/clap_complete/tests/snapshots/custom_bin_name.zsh b/clap_complete/tests/snapshots/custom_bin_name.zsh index 205bd23c..130aebcd 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.zsh +++ b/clap_complete/tests/snapshots/custom_bin_name.zsh @@ -68,7 +68,8 @@ esac (( $+functions[_bin-name_commands] )) || _bin-name_commands() { local commands; commands=( -'test:Subcommand' \ +'test:Subcommand +with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'bin-name commands' commands "$@" @@ -76,7 +77,8 @@ _bin-name_commands() { (( $+functions[_bin-name__help_commands] )) || _bin-name__help_commands() { local commands; commands=( -'test:Subcommand' \ +'test:Subcommand +with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'bin-name help commands' commands "$@" diff --git a/clap_complete/tests/testsuite/common.rs b/clap_complete/tests/testsuite/common.rs index a2f13f50..75dc4b6f 100644 --- a/clap_complete/tests/testsuite/common.rs +++ b/clap_complete/tests/testsuite/common.rs @@ -15,11 +15,13 @@ pub fn basic_command(name: &'static str) -> clap::Command { .action(clap::ArgAction::SetTrue), ) .subcommand( - clap::Command::new("test").about("Subcommand").arg( - clap::Arg::new("debug") - .short('d') - .action(clap::ArgAction::Count), - ), + clap::Command::new("test") + .about("Subcommand\nwith a second line") + .arg( + clap::Arg::new("debug") + .short('d') + .action(clap::ArgAction::Count), + ), ) } diff --git a/clap_complete_fig/tests/common.rs b/clap_complete_fig/tests/common.rs index afdf2a76..ba4db145 100644 --- a/clap_complete_fig/tests/common.rs +++ b/clap_complete_fig/tests/common.rs @@ -13,11 +13,13 @@ pub fn basic_command(name: &'static str) -> clap::Command { .action(clap::ArgAction::SetTrue), ) .subcommand( - clap::Command::new("test").about("Subcommand").arg( - clap::Arg::new("debug") - .short('d') - .action(clap::ArgAction::Count), - ), + clap::Command::new("test") + .about("Subcommand\nwith a second line") + .arg( + clap::Arg::new("debug") + .short('d') + .action(clap::ArgAction::Count), + ), ) } diff --git a/clap_complete_fig/tests/snapshots/basic.fig.js b/clap_complete_fig/tests/snapshots/basic.fig.js index 301e489b..5c9b929c 100644 --- a/clap_complete_fig/tests/snapshots/basic.fig.js +++ b/clap_complete_fig/tests/snapshots/basic.fig.js @@ -4,7 +4,7 @@ const completion: Fig.Spec = { subcommands: [ { name: "test", - description: "Subcommand", + description: "Subcommand with a second line", options: [ { name: "-d", @@ -25,7 +25,7 @@ const completion: Fig.Spec = { subcommands: [ { name: "test", - description: "Subcommand", + description: "Subcommand with a second line", }, { name: "help", diff --git a/clap_complete_nushell/tests/common.rs b/clap_complete_nushell/tests/common.rs index 343e7307..83ff466e 100644 --- a/clap_complete_nushell/tests/common.rs +++ b/clap_complete_nushell/tests/common.rs @@ -18,7 +18,7 @@ pub fn basic_command(name: &'static str) -> Command { ) .subcommand( Command::new("test") - .about("Subcommand") + .about("Subcommand\nwith a second line") .arg(Arg::new("debug").short('d').action(ArgAction::Count)), ) } @@ -36,7 +36,7 @@ pub fn feature_sample_command(name: &'static str) -> Command { .arg( Arg::new("config") .action(ArgAction::Count) - .help("some config file") + .help("some config file\nwith another line") .short('c') .visible_short_alias('C') .long("config") diff --git a/clap_complete_nushell/tests/snapshots/basic.nu b/clap_complete_nushell/tests/snapshots/basic.nu index f1d52b1f..c45024d4 100644 --- a/clap_complete_nushell/tests/snapshots/basic.nu +++ b/clap_complete_nushell/tests/snapshots/basic.nu @@ -7,6 +7,7 @@ module completions { ] # Subcommand +with a second line export extern "my-app test" [ -d -c @@ -18,6 +19,7 @@ module completions { ] # Subcommand +with a second line export extern "my-app help test" [ ] diff --git a/clap_complete_nushell/tests/snapshots/feature_sample.nu b/clap_complete_nushell/tests/snapshots/feature_sample.nu index f98c5a2b..696f55f6 100644 --- a/clap_complete_nushell/tests/snapshots/feature_sample.nu +++ b/clap_complete_nushell/tests/snapshots/feature_sample.nu @@ -8,8 +8,11 @@ module completions { export extern my-app [ file?: string # some input file --config(-c) # some config file +with another line --conf # some config file +with another line -C # some config file +with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version diff --git a/clap_complete_nushell/tests/snapshots/special_commands.nu b/clap_complete_nushell/tests/snapshots/special_commands.nu index fc01a39a..5c6e0b87 100644 --- a/clap_complete_nushell/tests/snapshots/special_commands.nu +++ b/clap_complete_nushell/tests/snapshots/special_commands.nu @@ -8,8 +8,11 @@ module completions { export extern my-app [ file?: string # some input file --config(-c) # some config file +with another line --conf # some config file +with another line -C # some config file +with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version diff --git a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu index c6e32a0b..511d8540 100644 --- a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu +++ b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu @@ -8,8 +8,11 @@ module completions { export extern my-app [ file?: string # some input file --config(-c) # some config file +with another line --conf # some config file +with another line -C # some config file +with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version diff --git a/clap_mangen/tests/common.rs b/clap_mangen/tests/common.rs index 90c48103..0fad276e 100644 --- a/clap_mangen/tests/common.rs +++ b/clap_mangen/tests/common.rs @@ -15,11 +15,13 @@ pub fn basic_command(name: &'static str) -> clap::Command { .action(clap::ArgAction::SetTrue), ) .subcommand( - clap::Command::new("test").about("Subcommand").arg( - clap::Arg::new("debug") - .short('d') - .action(clap::ArgAction::Count), - ), + clap::Command::new("test") + .about("Subcommand\nwith a second line") + .arg( + clap::Arg::new("debug") + .short('d') + .action(clap::ArgAction::Count), + ), ) } diff --git a/clap_mangen/tests/snapshots/basic.bash.roff b/clap_mangen/tests/snapshots/basic.bash.roff index d92a55fa..e9ab64a9 100644 --- a/clap_mangen/tests/snapshots/basic.bash.roff +++ b/clap_mangen/tests/snapshots/basic.bash.roff @@ -20,6 +20,7 @@ Print help .TP my\-app\-test(1) Subcommand +with a second line .TP my\-app\-help(1) Print this message or the help of the given subcommand(s) From e782775229081697e47d340a155d6b087eeb46f8 Mon Sep 17 00:00:00 2001 From: "Alexis (Poliorcetics) Bourget" Date: Fri, 16 Feb 2024 21:57:54 +0100 Subject: [PATCH 2/2] fix(complete): Handle newlines in command/arg descriptions Found while trying to add Nushell completions to [`jj`](https://github.com/martinvonz/jj). --- clap_complete/src/shells/elvish.rs | 14 +++++++------- clap_complete/src/shells/fish.rs | 14 ++++++++------ clap_complete/src/shells/powershell.rs | 10 +++++----- clap_complete/src/shells/zsh.rs | 1 + clap_complete/tests/snapshots/basic.elvish | 6 ++---- clap_complete/tests/snapshots/basic.fish | 6 ++---- clap_complete/tests/snapshots/basic.ps1 | 6 ++---- clap_complete/tests/snapshots/basic.zsh | 6 ++---- .../tests/snapshots/custom_bin_name.elvish | 6 ++---- clap_complete/tests/snapshots/custom_bin_name.fish | 6 ++---- clap_complete/tests/snapshots/custom_bin_name.ps1 | 6 ++---- clap_complete/tests/snapshots/custom_bin_name.zsh | 6 ++---- .../home/static/exhaustive/elvish/elvish/rc.elv | 9 +++------ .../fish/fish/completions/exhaustive.fish | 9 +++------ .../home/static/exhaustive/zsh/zsh/_exhaustive | 9 +++------ clap_complete_nushell/src/lib.rs | 8 +++++++- clap_complete_nushell/tests/snapshots/basic.nu | 6 ++---- .../tests/snapshots/feature_sample.nu | 9 +++------ .../tests/snapshots/special_commands.nu | 9 +++------ .../tests/snapshots/sub_subcommands.nu | 9 +++------ 20 files changed, 64 insertions(+), 91 deletions(-) diff --git a/clap_complete/src/shells/elvish.rs b/clap_complete/src/shells/elvish.rs index 48a0f852..082cb3df 100644 --- a/clap_complete/src/shells/elvish.rs +++ b/clap_complete/src/shells/elvish.rs @@ -57,9 +57,9 @@ fn escape_string(string: &str) -> String { string.replace('\'', "''") } -fn get_tooltip(help: Option<&StyledStr>, data: T) -> String { +fn escape_help(help: Option<&StyledStr>, data: T) -> String { match help { - Some(help) => escape_string(&help.to_string()), + Some(help) => escape_string(&help.to_string().replace('\n', " ")), _ => data.to_string(), } } @@ -78,7 +78,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { for option in p.get_opts() { if let Some(shorts) = option.get_short_and_visible_aliases() { - let tooltip = get_tooltip(option.get_help(), shorts[0]); + let tooltip = escape_help(option.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str(format!("-{short} '{tooltip}'").as_str()); @@ -86,7 +86,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { } if let Some(longs) = option.get_long_and_visible_aliases() { - let tooltip = get_tooltip(option.get_help(), longs[0]); + let tooltip = escape_help(option.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str(format!("--{long} '{tooltip}'").as_str()); @@ -96,7 +96,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { for flag in utils::flags(p) { if let Some(shorts) = flag.get_short_and_visible_aliases() { - let tooltip = get_tooltip(flag.get_help(), shorts[0]); + let tooltip = escape_help(flag.get_help(), shorts[0]); for short in shorts { completions.push_str(&preamble); completions.push_str(format!("-{short} '{tooltip}'").as_str()); @@ -104,7 +104,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { } if let Some(longs) = flag.get_long_and_visible_aliases() { - let tooltip = get_tooltip(flag.get_help(), longs[0]); + let tooltip = escape_help(flag.get_help(), longs[0]); for long in longs { completions.push_str(&preamble); completions.push_str(format!("--{long} '{tooltip}'").as_str()); @@ -114,7 +114,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { for subcommand in p.get_subcommands() { let data = &subcommand.get_name(); - let tooltip = get_tooltip(subcommand.get_about(), data); + let tooltip = escape_help(subcommand.get_about(), data); completions.push_str(&preamble); completions.push_str(format!("{data} '{tooltip}'").as_str()); diff --git a/clap_complete/src/shells/fish.rs b/clap_complete/src/shells/fish.rs index 7dae5b6d..d8e7fde1 100644 --- a/clap_complete/src/shells/fish.rs +++ b/clap_complete/src/shells/fish.rs @@ -36,6 +36,10 @@ fn escape_string(string: &str, escape_comma: bool) -> String { } } +fn escape_help(help: &clap::builder::StyledStr) -> String { + escape_string(&help.to_string().replace('\n', " "), false) +} + fn gen_fish_inner( root_command: &str, parent_commands: &[&str], @@ -98,8 +102,7 @@ fn gen_fish_inner( } if let Some(data) = option.get_help() { - template - .push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str()); + template.push_str(&format!(" -d '{}'", escape_help(data))); } template.push_str(value_completion(option).as_str()); @@ -124,8 +127,7 @@ fn gen_fish_inner( } if let Some(data) = flag.get_help() { - template - .push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str()); + template.push_str(&format!(" -d '{}'", escape_help(data))); } buffer.push_str(template.as_str()); @@ -139,7 +141,7 @@ fn gen_fish_inner( template.push_str(format!(" -a \"{}\"", &subcommand.get_name()).as_str()); if let Some(data) = subcommand.get_about() { - template.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str()) + template.push_str(format!(" -d '{}'", escape_help(data)).as_str()) } buffer.push_str(template.as_str()); @@ -173,7 +175,7 @@ fn value_completion(option: &Arg) -> String { Some(format!( "{}\t'{}'", escape_string(value.get_name(), true).as_str(), - escape_string(&value.get_help().unwrap_or_default().to_string(), false) + escape_help(value.get_help().unwrap_or_default()) )) }) .collect::>() diff --git a/clap_complete/src/shells/powershell.rs b/clap_complete/src/shells/powershell.rs index 6b09b2e3..24649580 100644 --- a/clap_complete/src/shells/powershell.rs +++ b/clap_complete/src/shells/powershell.rs @@ -62,9 +62,9 @@ fn escape_string(string: &str) -> String { string.replace('\'', "''") } -fn get_tooltip(help: Option<&StyledStr>, data: T) -> String { +fn escape_help(help: Option<&StyledStr>, data: T) -> String { match help { - Some(help) => escape_string(&help.to_string()), + Some(help) => escape_string(&help.to_string().replace('\n', " ")), _ => data.to_string(), } } @@ -91,7 +91,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String { for subcommand in p.get_subcommands() { let data = &subcommand.get_name(); - let tooltip = get_tooltip(subcommand.get_about(), data); + let tooltip = escape_help(subcommand.get_about(), data); completions.push_str(&preamble); completions.push_str( @@ -120,7 +120,7 @@ fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) { use std::fmt::Write as _; if let Some(aliases) = arg.get_short_and_visible_aliases() { - let tooltip = get_tooltip(arg.get_help(), aliases[0]); + let tooltip = escape_help(arg.get_help(), aliases[0]); for alias in aliases { let _ = write!( completions, @@ -131,7 +131,7 @@ fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) { } } if let Some(aliases) = arg.get_long_and_visible_aliases() { - let tooltip = get_tooltip(arg.get_help(), aliases[0]); + let tooltip = escape_help(arg.get_help(), aliases[0]); for alias in aliases { let _ = write!( completions, diff --git a/clap_complete/src/shells/zsh.rs b/clap_complete/src/shells/zsh.rs index 65d7af65..b0573c1f 100644 --- a/clap_complete/src/shells/zsh.rs +++ b/clap_complete/src/shells/zsh.rs @@ -429,6 +429,7 @@ fn escape_help(string: &str) -> String { .replace(':', "\\:") .replace('$', "\\$") .replace('`', "\\`") + .replace('\n', " ") } /// Escape value string inside single quotes and parentheses diff --git a/clap_complete/tests/snapshots/basic.elvish b/clap_complete/tests/snapshots/basic.elvish index b9736473..85cf1c6d 100644 --- a/clap_complete/tests/snapshots/basic.elvish +++ b/clap_complete/tests/snapshots/basic.elvish @@ -22,8 +22,7 @@ set edit:completion:arg-completer[my-app] = {|@words| cand -v 'v' cand -h 'Print help' cand --help 'Print help' - cand test 'Subcommand -with a second line' + cand test 'Subcommand with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'my-app;test'= { @@ -33,8 +32,7 @@ with a second line' cand --help 'Print help' } &'my-app;help'= { - cand test 'Subcommand -with a second line' + cand test 'Subcommand with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'my-app;help;test'= { diff --git a/clap_complete/tests/snapshots/basic.fish b/clap_complete/tests/snapshots/basic.fish index e269f6e1..73915f02 100644 --- a/clap_complete/tests/snapshots/basic.fish +++ b/clap_complete/tests/snapshots/basic.fish @@ -1,12 +1,10 @@ complete -c my-app -n "__fish_use_subcommand" -s c complete -c my-app -n "__fish_use_subcommand" -s v complete -c my-app -n "__fish_use_subcommand" -s h -l help -d 'Print help' -complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand -with a second line' +complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand with a second line' complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c my-app -n "__fish_seen_subcommand_from test" -s d complete -c my-app -n "__fish_seen_subcommand_from test" -s c complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help' -complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand -with a second line' +complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line' complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' diff --git a/clap_complete/tests/snapshots/basic.ps1 b/clap_complete/tests/snapshots/basic.ps1 index d561b8bd..bb608095 100644 --- a/clap_complete/tests/snapshots/basic.ps1 +++ b/clap_complete/tests/snapshots/basic.ps1 @@ -25,8 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock { [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand -with a second line') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } @@ -38,8 +37,7 @@ with a second line') break } 'my-app;help' { - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand -with a second line') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } diff --git a/clap_complete/tests/snapshots/basic.zsh b/clap_complete/tests/snapshots/basic.zsh index 0a0e4cfe..df818fdb 100644 --- a/clap_complete/tests/snapshots/basic.zsh +++ b/clap_complete/tests/snapshots/basic.zsh @@ -68,8 +68,7 @@ esac (( $+functions[_my-app_commands] )) || _my-app_commands() { local commands; commands=( -'test:Subcommand -with a second line' \ +'test:Subcommand with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'my-app commands' commands "$@" @@ -77,8 +76,7 @@ with a second line' \ (( $+functions[_my-app__help_commands] )) || _my-app__help_commands() { local commands; commands=( -'test:Subcommand -with a second line' \ +'test:Subcommand with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'my-app help commands' commands "$@" diff --git a/clap_complete/tests/snapshots/custom_bin_name.elvish b/clap_complete/tests/snapshots/custom_bin_name.elvish index 80b6bf5f..6e362e15 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.elvish +++ b/clap_complete/tests/snapshots/custom_bin_name.elvish @@ -22,8 +22,7 @@ set edit:completion:arg-completer[bin-name] = {|@words| cand -v 'v' cand -h 'Print help' cand --help 'Print help' - cand test 'Subcommand -with a second line' + cand test 'Subcommand with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'bin-name;test'= { @@ -33,8 +32,7 @@ with a second line' cand --help 'Print help' } &'bin-name;help'= { - cand test 'Subcommand -with a second line' + cand test 'Subcommand with a second line' cand help 'Print this message or the help of the given subcommand(s)' } &'bin-name;help;test'= { diff --git a/clap_complete/tests/snapshots/custom_bin_name.fish b/clap_complete/tests/snapshots/custom_bin_name.fish index a75e468e..4afbd5d0 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.fish +++ b/clap_complete/tests/snapshots/custom_bin_name.fish @@ -1,12 +1,10 @@ complete -c bin-name -n "__fish_use_subcommand" -s c complete -c bin-name -n "__fish_use_subcommand" -s v complete -c bin-name -n "__fish_use_subcommand" -s h -l help -d 'Print help' -complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand -with a second line' +complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand with a second line' complete -c bin-name -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c bin-name -n "__fish_seen_subcommand_from test" -s d complete -c bin-name -n "__fish_seen_subcommand_from test" -s c complete -c bin-name -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help' -complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand -with a second line' +complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line' complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' diff --git a/clap_complete/tests/snapshots/custom_bin_name.ps1 b/clap_complete/tests/snapshots/custom_bin_name.ps1 index bf6002be..b00876d1 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.ps1 +++ b/clap_complete/tests/snapshots/custom_bin_name.ps1 @@ -25,8 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock { [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand -with a second line') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } @@ -38,8 +37,7 @@ with a second line') break } 'bin-name;help' { - [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand -with a second line') + [CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } diff --git a/clap_complete/tests/snapshots/custom_bin_name.zsh b/clap_complete/tests/snapshots/custom_bin_name.zsh index 130aebcd..e97b7c09 100644 --- a/clap_complete/tests/snapshots/custom_bin_name.zsh +++ b/clap_complete/tests/snapshots/custom_bin_name.zsh @@ -68,8 +68,7 @@ esac (( $+functions[_bin-name_commands] )) || _bin-name_commands() { local commands; commands=( -'test:Subcommand -with a second line' \ +'test:Subcommand with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'bin-name commands' commands "$@" @@ -77,8 +76,7 @@ with a second line' \ (( $+functions[_bin-name__help_commands] )) || _bin-name__help_commands() { local commands; commands=( -'test:Subcommand -with a second line' \ +'test:Subcommand with a second line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'bin-name help commands' commands "$@" diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv b/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv index e403bd83..c8253260 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv +++ b/clap_complete/tests/snapshots/home/static/exhaustive/elvish/elvish/rc.elv @@ -66,8 +66,7 @@ set edit:completion:arg-completer[exhaustive] = {|@words| cand cmd-backslash 'Avoid ''\n''' cand cmd-brackets 'List packages [filter]' cand cmd-expansions 'Execute the shell command with $SHELL' - cand escape-help '\tab "'' -New Line' + cand escape-help '\tab "'' New Line' cand help 'Print this message or the help of the given subcommand(s)' } &'exhaustive;quote;cmd-single-quotes'= { @@ -126,8 +125,7 @@ New Line' cand cmd-backslash 'Avoid ''\n''' cand cmd-brackets 'List packages [filter]' cand cmd-expansions 'Execute the shell command with $SHELL' - cand escape-help '\tab "'' -New Line' + cand escape-help '\tab "'' New Line' cand help 'Print this message or the help of the given subcommand(s)' } &'exhaustive;quote;help;cmd-single-quotes'= { @@ -269,8 +267,7 @@ New Line' cand cmd-backslash 'Avoid ''\n''' cand cmd-brackets 'List packages [filter]' cand cmd-expansions 'Execute the shell command with $SHELL' - cand escape-help '\tab "'' -New Line' + cand escape-help '\tab "'' New Line' } &'exhaustive;help;quote;cmd-single-quotes'= { } diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish b/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish index e9558b91..147a44f1 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish +++ b/clap_complete/tests/snapshots/home/static/exhaustive/fish/fish/completions/exhaustive.fish @@ -34,8 +34,7 @@ complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_see complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-backslash" -d 'Avoid \'\\n\'' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-brackets" -d 'List packages [filter]' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL' -complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "escape-help" -d '\\tab "\' -New Line' +complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "escape-help" -d '\\tab "\' New Line' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from cmd-single-quotes" -l global -d 'everywhere' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from cmd-single-quotes" -s h -l help -d 'Print help' @@ -64,8 +63,7 @@ complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_su complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-backslash" -d 'Avoid \'\\n\'' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-brackets" -d 'List packages [filter]' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL' -complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "escape-help" -d '\\tab "\' -New Line' +complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "escape-help" -d '\\tab "\' New Line' complete -c exhaustive -n "__fish_seen_subcommand_from quote; and __fish_seen_subcommand_from help; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' complete -c exhaustive -n "__fish_seen_subcommand_from value" -l delim -r complete -c exhaustive -n "__fish_seen_subcommand_from value" -l tuple -r @@ -132,7 +130,6 @@ complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_sub complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help" -f -a "cmd-backslash" -d 'Avoid \'\\n\'' complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help" -f -a "cmd-brackets" -d 'List packages [filter]' complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help" -f -a "cmd-expansions" -d 'Execute the shell command with $SHELL' -complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help" -f -a "escape-help" -d '\\tab "\' -New Line' +complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from quote; and not __fish_seen_subcommand_from cmd-single-quotes; and not __fish_seen_subcommand_from cmd-double-quotes; and not __fish_seen_subcommand_from cmd-backticks; and not __fish_seen_subcommand_from cmd-backslash; and not __fish_seen_subcommand_from cmd-brackets; and not __fish_seen_subcommand_from cmd-expansions; and not __fish_seen_subcommand_from escape-help" -f -a "escape-help" -d '\\tab "\' New Line' complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from pacman; and not __fish_seen_subcommand_from one; and not __fish_seen_subcommand_from two" -f -a "one" complete -c exhaustive -n "__fish_seen_subcommand_from help; and __fish_seen_subcommand_from pacman; and not __fish_seen_subcommand_from one; and not __fish_seen_subcommand_from two" -f -a "two" diff --git a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive index 82e68ced..7308976a 100644 --- a/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive +++ b/clap_complete/tests/snapshots/home/static/exhaustive/zsh/zsh/_exhaustive @@ -645,8 +645,7 @@ _exhaustive__quote__help_commands() { 'cmd-backslash:Avoid '\''\\n'\''' \ 'cmd-brackets:List packages \[filter\]' \ 'cmd-expansions:Execute the shell command with \$SHELL' \ -'escape-help:\\tab "'\'' -New Line' \ +'escape-help:\\tab "'\'' New Line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'exhaustive quote help commands' commands "$@" @@ -717,8 +716,7 @@ _exhaustive__help__quote_commands() { 'cmd-backslash:Avoid '\''\\n'\''' \ 'cmd-brackets:List packages \[filter\]' \ 'cmd-expansions:Execute the shell command with \$SHELL' \ -'escape-help:\\tab "'\'' -New Line' \ +'escape-help:\\tab "'\'' New Line' \ ) _describe -t commands 'exhaustive help quote commands' commands "$@" } @@ -731,8 +729,7 @@ _exhaustive__quote_commands() { 'cmd-backslash:Avoid '\''\\n'\''' \ 'cmd-brackets:List packages \[filter\]' \ 'cmd-expansions:Execute the shell command with \$SHELL' \ -'escape-help:\\tab "'\'' -New Line' \ +'escape-help:\\tab "'\'' New Line' \ 'help:Print this message or the help of the given subcommand(s)' \ ) _describe -t commands 'exhaustive quote commands' commands "$@" diff --git a/clap_complete_nushell/src/lib.rs b/clap_complete_nushell/src/lib.rs index e686b7cf..24005d21 100644 --- a/clap_complete_nushell/src/lib.rs +++ b/clap_complete_nushell/src/lib.rs @@ -19,6 +19,7 @@ #![warn(missing_docs, trivial_casts, unused_allocation, trivial_numeric_casts)] #![forbid(unsafe_code)] +use clap::builder::StyledStr; use clap::{builder::PossibleValue, Arg, ArgAction, Command}; use clap_complete::Generator; @@ -75,7 +76,7 @@ fn append_value_completion_and_help( None => 0, }; - s.push_str(format!("{:>width$}# {}", ' ', help).as_str()); + s.push_str(format!("{:>width$}# {}", ' ', single_line_styled_str(help)).as_str()); } s.push('\n'); @@ -180,6 +181,7 @@ fn generate_completion(completions: &mut String, cmd: &Command, is_subcommand: b } if let Some(about) = cmd.get_about() { + let about = single_line_styled_str(about); completions.push_str(format!(" # {about}\n").as_str()); } @@ -201,3 +203,7 @@ fn generate_completion(completions: &mut String, cmd: &Command, is_subcommand: b } } } + +fn single_line_styled_str(text: &StyledStr) -> String { + text.to_string().replace('\n', " ") +} diff --git a/clap_complete_nushell/tests/snapshots/basic.nu b/clap_complete_nushell/tests/snapshots/basic.nu index c45024d4..79cdb057 100644 --- a/clap_complete_nushell/tests/snapshots/basic.nu +++ b/clap_complete_nushell/tests/snapshots/basic.nu @@ -6,8 +6,7 @@ module completions { --help(-h) # Print help ] - # Subcommand -with a second line + # Subcommand with a second line export extern "my-app test" [ -d -c @@ -18,8 +17,7 @@ with a second line export extern "my-app help" [ ] - # Subcommand -with a second line + # Subcommand with a second line export extern "my-app help test" [ ] diff --git a/clap_complete_nushell/tests/snapshots/feature_sample.nu b/clap_complete_nushell/tests/snapshots/feature_sample.nu index 696f55f6..78e772bc 100644 --- a/clap_complete_nushell/tests/snapshots/feature_sample.nu +++ b/clap_complete_nushell/tests/snapshots/feature_sample.nu @@ -7,12 +7,9 @@ module completions { # Tests completions export extern my-app [ file?: string # some input file - --config(-c) # some config file -with another line - --conf # some config file -with another line - -C # some config file -with another line + --config(-c) # some config file with another line + --conf # some config file with another line + -C # some config file with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version diff --git a/clap_complete_nushell/tests/snapshots/special_commands.nu b/clap_complete_nushell/tests/snapshots/special_commands.nu index 5c6e0b87..f74a3c86 100644 --- a/clap_complete_nushell/tests/snapshots/special_commands.nu +++ b/clap_complete_nushell/tests/snapshots/special_commands.nu @@ -7,12 +7,9 @@ module completions { # Tests completions export extern my-app [ file?: string # some input file - --config(-c) # some config file -with another line - --conf # some config file -with another line - -C # some config file -with another line + --config(-c) # some config file with another line + --conf # some config file with another line + -C # some config file with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version diff --git a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu index 511d8540..7b0570f8 100644 --- a/clap_complete_nushell/tests/snapshots/sub_subcommands.nu +++ b/clap_complete_nushell/tests/snapshots/sub_subcommands.nu @@ -7,12 +7,9 @@ module completions { # Tests completions export extern my-app [ file?: string # some input file - --config(-c) # some config file -with another line - --conf # some config file -with another line - -C # some config file -with another line + --config(-c) # some config file with another line + --conf # some config file with another line + -C # some config file with another line choice?: string@"nu-complete my-app choice" --help(-h) # Print help --version(-V) # Print version