Skip to content

Commit

Permalink
fix(help): Defaulting max_term_width instead of max_term_width
Browse files Browse the repository at this point in the history
Fixes #4295
  • Loading branch information
epage committed Jul 17, 2023
1 parent d741e95 commit bc000aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Breaking Changes

- Made `ArgPredicate` `non_exhaustive`
- *(help)* Change default `Command::term_width` to "source format"
- *(help)* Change default `Command::max_term_width` to 100
- *(derive)* `Vec<Vec<T>>` types are now assuming to capture occurrences

### Features
Expand Down
5 changes: 5 additions & 0 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,9 @@ impl Command {
/// Defaults to current terminal width when `wrap_help` feature flag is enabled. If current
/// width cannot be determined, the default is 100.
///
/// **`unstable-v5` feature**: Defaults to unbound, being subject to
/// [`Command::max_term_width`].
///
/// **NOTE:** This setting applies globally and *not* on a per-command basis.
///
/// **NOTE:** This requires the `wrap_help` feature
Expand Down Expand Up @@ -1160,6 +1163,8 @@ impl Command {
///
/// Using `0` will ignore this, always respecting [`Command::term_width`] (default).
///
/// **`unstable-v5` feature**: Defaults to 100.
///
/// **NOTE:** This setting applies globally and *not* on a per-command basis.
///
/// **NOTE:** This requires the `wrap_help` feature
Expand Down
21 changes: 21 additions & 0 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
}
}

#[cfg(not(feature = "unstable-v5"))]
fn term_w(cmd: &'cmd Command) -> usize {
match cmd.get_term_width() {
Some(0) => usize::MAX,
Expand All @@ -131,6 +132,26 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
}
}

#[cfg(feature = "unstable-v5")]
fn term_w(cmd: &'cmd Command) -> usize {
let term_w = match cmd.get_term_width() {
Some(0) => usize::MAX,
Some(w) => w,
None => {
let (current_width, _h) = dimensions();
current_width.unwrap_or(usize::MAX)
}
};

let max_term_w = match cmd.get_max_term_width() {
Some(0) => usize::MAX,
Some(mw) => mw,
None => 100,
};

cmp::min(term_w, max_term_w)
}

/// Write help to stream for the parser in the format defined by the template.
///
/// For details about the template language see [`Command::help_template`].
Expand Down
25 changes: 25 additions & 0 deletions tests/builder/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ fn old_newline_variables() {
#[test]
#[cfg(feature = "wrap_help")]
fn issue_688_hide_pos_vals() {
#[cfg(not(feature = "unstable-v5"))]
static ISSUE_688: &str = "\
Usage: ctest [OPTIONS]
Expand All @@ -927,6 +928,18 @@ Options:
-V, --version Print version
";

#[cfg(feature = "unstable-v5")]
static ISSUE_688: &str = "\
Usage: ctest [OPTIONS]
Options:
--filter <filter> Sets the filter, or sampling method, to use for interpolation when resizing
the particle images. The default is Linear (Bilinear). [possible values:
Nearest, Linear, Cubic, Gaussian, Lanczos3]
-h, --help Print help
-V, --version Print version
";

let filter_values = ["Nearest", "Linear", "Cubic", "Gaussian", "Lanczos3"];

let app1 = Command::new("ctest")
Expand Down Expand Up @@ -1511,6 +1524,7 @@ fn hide_default_val() {
#[test]
#[cfg(feature = "wrap_help")]
fn escaped_whitespace_values() {
#[cfg(not(feature = "unstable-v5"))]
static ESCAPED_DEFAULT_VAL: &str = "\
Usage: default [OPTIONS]
Expand All @@ -1521,6 +1535,17 @@ Options:
-V, --version Print version
";

#[cfg(feature = "unstable-v5")]
static ESCAPED_DEFAULT_VAL: &str = "\
Usage: default [OPTIONS]
Options:
--arg <argument> Pass an argument to the program. [default: \"\\n\"] [possible values: normal, \"
\", \"\\n\", \"\\t\", other]
-h, --help Print help
-V, --version Print version
";

let app1 = Command::new("default").version("0.1").term_width(120).arg(
Arg::new("argument")
.help("Pass an argument to the program.")
Expand Down

0 comments on commit bc000aa

Please sign in to comment.