feat(help): allow for limiting detected terminal width

Useful when, e.g., the terminal is fullscreen

Closes #653
This commit is contained in:
nabijaczleweli 2016-09-10 19:46:02 +02:00
parent 377b5f20da
commit a43e28af85
No known key found for this signature in database
GPG key ID: BCFD0B018D2658F1
3 changed files with 38 additions and 3 deletions

View file

@ -94,7 +94,8 @@ impl<'a> Help<'a> {
hide_pv: bool,
color: bool,
cizer: Colorizer,
term_w: Option<usize>)
term_w: Option<usize>,
max_w: Option<usize>)
-> Self {
debugln!("fn=Help::new;");
Help {
@ -103,7 +104,10 @@ impl<'a> Help<'a> {
hide_pv: hide_pv,
term_w: match term_w {
Some(width) => if width == 0 { usize::MAX } else { width },
None => term_size::dimensions().map_or(120, |(w, _)| w),
None => cmp::min(term_size::dimensions().map_or(120, |(w, _)| w), match max_w {
None | Some(0) => usize::MAX,
Some(mw) => mw,
}),
},
color: color,
cizer: cizer,
@ -142,7 +146,7 @@ impl<'a> Help<'a> {
use_stderr: stderr,
when: parser.color(),
};
Self::new(w, nlh, hide_v, color, cizer, parser.meta.term_w).write_help(parser)
Self::new(w, nlh, hide_v, color, cizer, parser.meta.term_w, parser.meta.max_w).write_help(parser)
}
/// Writes the parser help to the wrapped stream.

View file

@ -14,6 +14,7 @@ pub struct AppMeta<'b> {
pub help_str: Option<&'b str>,
pub disp_ord: usize,
pub term_w: Option<usize>,
pub max_w: Option<usize>,
pub template: Option<&'b str>,
}
@ -34,6 +35,7 @@ impl<'b> Default for AppMeta<'b> {
template: None,
aliases: None,
term_w: None,
max_w: None,
}
}
}
@ -64,6 +66,7 @@ impl<'b> Clone for AppMeta<'b> {
template: self.template,
aliases: self.aliases.clone(),
term_w: self.term_w,
max_w: self.max_w,
}
}
}

View file

@ -580,6 +580,34 @@ impl<'a, 'b> App<'a, 'b> {
self
}
/// Sets the max terminal width at which to wrap help messages. Using `0` will ignore terminal
/// widths and use source formatting.
///
/// `clap` automatically tries to determine the terminal width on Unix, Linux, OSX and Windows
/// if the `wrap_help` cargo "feature" has been used while compiling, but one might want to
/// limit the size (e.g. when the terminal is running fullscreen).
///
/// **NOTE:** This setting applies globally and *not* on a per-command basis.
///
/// **NOTE:** This setting must be set **before** any subcommands are added!
///
/// # Platform Specific
///
/// Only Unix, Linux, OSX and Windows support automatic determination of terminal width.
///
/// # Examples
///
/// ```no_run
/// # use clap::App;
/// App::new("myprog")
/// .max_term_width(100)
/// # ;
/// ```
pub fn max_term_width(mut self, w: usize) -> Self {
self.p.meta.max_w = Some(w);
self
}
/// Adds an [argument] to the list of valid possibilties.
///
/// # Examples