Skip to content

Commit

Permalink
Merge pull request #5014 from epage/max_width
Browse files Browse the repository at this point in the history
fix(help): Defaulting max_term_width instead of max_term_width
  • Loading branch information
epage committed Jul 17, 2023
2 parents 33feb65 + bc000aa commit 8db992c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 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
7 changes: 6 additions & 1 deletion 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 All @@ -1158,7 +1161,9 @@ impl Command {
/// This only applies when [`term_width`][Command::term_width] is unset so that the current
/// terminal's width will be used. See [`Command::term_width`] for more details.
///
/// Using `0` will ignore terminal widths and use source formatting (default).
/// 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.
///
Expand Down
47 changes: 36 additions & 11 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,23 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
cmd.get_name(),
use_long
);
let term_w = match cmd.get_term_width() {
let term_w = Self::term_w(cmd);
let next_line_help = cmd.is_next_line_help_set();

HelpTemplate {
writer,
cmd,
styles: cmd.get_styles(),
usage,
next_line_help,
term_w,
use_long,
}
}

#[cfg(not(feature = "unstable-v5"))]
fn term_w(cmd: &'cmd Command) -> usize {
match cmd.get_term_width() {
Some(0) => usize::MAX,
Some(w) => w,
None => {
Expand All @@ -113,18 +129,27 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
};
cmp::min(current_width, max_width)
}
}
}

#[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 next_line_help = cmd.is_next_line_help_set();

HelpTemplate {
writer,
cmd,
styles: cmd.get_styles(),
usage,
next_line_help,
term_w,
use_long,
}
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.
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 8db992c

Please sign in to comment.