Skip to content

Commit

Permalink
fix(clap_complete_nushell): Correctly handle commands whose short des…
Browse files Browse the repository at this point in the history
…cription is more than one line

Found while trying to add Nushell completions to [`jj`](https://github.com/martinvonz/jj).
  • Loading branch information
poliorcetics committed Feb 16, 2024
1 parent 8a7a13a commit db4d909
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
8 changes: 7 additions & 1 deletion clap_complete_nushell/src/lib.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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());
}

Expand All @@ -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', " ")
}
4 changes: 2 additions & 2 deletions clap_complete_nushell/tests/common.rs
Expand Up @@ -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)),
)
}
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions clap_complete_nushell/tests/snapshots/basic.nu
Expand Up @@ -6,7 +6,7 @@ module completions {
--help(-h) # Print help
]

# Subcommand
# Subcommand with a second line
export extern "my-app test" [
-d
-c
Expand All @@ -17,7 +17,7 @@ module completions {
export extern "my-app help" [
]

# Subcommand
# Subcommand with a second line
export extern "my-app help test" [
]

Expand Down
6 changes: 3 additions & 3 deletions clap_complete_nushell/tests/snapshots/feature_sample.nu
Expand Up @@ -7,9 +7,9 @@ module completions {
# Tests completions
export extern my-app [
file?: string # some input file
--config(-c) # some config file
--conf # some config file
-C # some config 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
Expand Down
6 changes: 3 additions & 3 deletions clap_complete_nushell/tests/snapshots/special_commands.nu
Expand Up @@ -7,9 +7,9 @@ module completions {
# Tests completions
export extern my-app [
file?: string # some input file
--config(-c) # some config file
--conf # some config file
-C # some config 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
Expand Down
6 changes: 3 additions & 3 deletions clap_complete_nushell/tests/snapshots/sub_subcommands.nu
Expand Up @@ -7,9 +7,9 @@ module completions {
# Tests completions
export extern my-app [
file?: string # some input file
--config(-c) # some config file
--conf # some config file
-C # some config 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
Expand Down

0 comments on commit db4d909

Please sign in to comment.