From afefdc94140afb8a4e8fb07b66dfbc7c491d0891 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 22 Aug 2023 16:00:10 -0500 Subject: [PATCH 1/2] test(help): Show padding bug --- tests/builder/help.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/builder/help.rs b/tests/builder/help.rs index 14ff54bcff4..23c05b96cb5 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -1333,6 +1333,23 @@ Options: utils::assert_output(cmd, "ctest --help", ISSUE_777, false); } +#[test] +fn dont_strip_padding_issue_5083() { + let cmd = Command::new("test") + .help_template("{subcommands}") + .subcommands([ + Command::new("one"), + Command::new("two"), + Command::new("three"), + ]); + static EXPECTED: &str = "one + two + three + help Print this message or the help of the given subcommand(s) +"; + utils::assert_output(cmd, "test --help", EXPECTED, false); +} + static OVERRIDE_HELP_SHORT: &str = "\ Usage: test From bf3f25ebb23beb4d9d71eea16aaf0fbba27afa8a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 22 Aug 2023 16:02:33 -0500 Subject: [PATCH 2/2] fix(help): Ensure padding isn't stripped Fixes #5083 --- clap_builder/src/builder/styled_str.rs | 15 +++++++++++++++ clap_builder/src/output/help.rs | 6 ++++-- tests/builder/help.rs | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/clap_builder/src/builder/styled_str.rs b/clap_builder/src/builder/styled_str.rs index d856a301516..df0f1b03b7c 100644 --- a/clap_builder/src/builder/styled_str.rs +++ b/clap_builder/src/builder/styled_str.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "usage"), allow(dead_code))] + /// Terminal-styling container /// /// Styling may be encoded as [ANSI Escape Code](https://en.wikipedia.org/wiki/ANSI_escape_code) @@ -47,6 +49,19 @@ impl StyledStr { self.0 = self.0.trim().to_owned() } + pub(crate) fn trim_start_lines(&mut self) { + if let Some(pos) = self.0.find('\n') { + let (leading, help) = self.0.split_at(pos + 1); + if leading.trim().is_empty() { + self.0 = help.to_owned() + } + } + } + + pub(crate) fn trim_end(&mut self) { + self.0 = self.0.trim_end().to_owned() + } + #[cfg(feature = "help")] pub(crate) fn replace_newline_var(&mut self) { self.0 = self.0.replace("{n}", "\n"); diff --git a/clap_builder/src/output/help.rs b/clap_builder/src/output/help.rs index 410616eb1b8..a5073a9e4b6 100644 --- a/clap_builder/src/output/help.rs +++ b/clap_builder/src/output/help.rs @@ -30,8 +30,10 @@ pub(crate) fn write_help(writer: &mut StyledStr, cmd: &Command, usage: &Usage<'_ } } - // Remove any extra lines caused by book keeping - writer.trim(); + // Remove any lines from unused sections + writer.trim_start_lines(); + // Remove any whitespace caused by book keeping + writer.trim_end(); // Ensure there is still a trailing newline writer.push_str("\n"); } diff --git a/tests/builder/help.rs b/tests/builder/help.rs index 23c05b96cb5..0d6509bdc9b 100644 --- a/tests/builder/help.rs +++ b/tests/builder/help.rs @@ -1342,7 +1342,7 @@ fn dont_strip_padding_issue_5083() { Command::new("two"), Command::new("three"), ]); - static EXPECTED: &str = "one + static EXPECTED: &str = " one two three help Print this message or the help of the given subcommand(s)