Skip to content

Commit

Permalink
fix(help): Wrap long possible values correctly
Browse files Browse the repository at this point in the history
We weren't taking the name into account when determining the wrap width.

Fixes #5022
  • Loading branch information
epage committed Jul 19, 2023
1 parent 0951f93 commit 8b536e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
37 changes: 19 additions & 18 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,32 +701,33 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
self.writer.push_str("Possible values:");
for pv in possible_vals.iter().filter(|pv| !pv.is_hide_set()) {
let name = pv.get_name();

let mut descr = StyledStr::new();
let _ = write!(
self.writer,
"\n{:spaces$}- {}{name}{}",
"",
&mut descr,
"{}{name}{}",
literal.render(),
literal.render_reset()
);
if let Some(help) = pv.get_help() {
debug!("HelpTemplate::help: Possible Value help");

// To align help messages
let padding = longest - display_width(pv.get_name());
let _ = write!(self.writer, ": {:padding$}", "");

let avail_chars = if self.term_w > trailing_indent.len() {
self.term_w - trailing_indent.len()
} else {
usize::MAX
};

let mut help = help.clone();
help.replace_newline_var();
help.wrap(avail_chars);
help.indent("", &trailing_indent);
self.writer.push_styled(&help);
let padding = longest - display_width(name);
let _ = write!(&mut descr, ": {:padding$}", "");
descr.push_styled(help);
}

let avail_chars = if self.term_w > trailing_indent.len() {
self.term_w - trailing_indent.len()
} else {
usize::MAX
};
descr.replace_newline_var();
descr.wrap(avail_chars);
descr.indent("", &trailing_indent);

let _ = write!(self.writer, "\n{:spaces$}- ", "",);
self.writer.push_styled(&descr);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,15 @@ Usage: test [OPTIONS]
Options:
--possible-values <possible_values>
Possible values:
- short_name: Long enough help message, barely warrant wrapping
- short_name: Long enough help message, barely warrant
wrapping
- second: Short help gets handled the same
--possible-values-with-new-line <possible_values_with_new_line>
Possible values:
- long enough name to trigger new line: Really long enough help message to clearly warrant
wrapping believe me
- long enough name to trigger new line: Really long
enough help message to clearly warrant wrapping believe
me
- second
--possible-values-without-new-line <possible_values_without_new_line>
Expand Down

0 comments on commit 8b536e2

Please sign in to comment.