From a43e28af85c9a9deaedd5ef735f4f13008daab29 Mon Sep 17 00:00:00 2001 From: nabijaczleweli Date: Sat, 10 Sep 2016 19:46:02 +0200 Subject: [PATCH] feat(help): allow for limiting detected terminal width Useful when, e.g., the terminal is fullscreen Closes #653 --- src/app/help.rs | 10 +++++++--- src/app/meta.rs | 3 +++ src/app/mod.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/app/help.rs b/src/app/help.rs index f0d1e24b..b8df90e4 100644 --- a/src/app/help.rs +++ b/src/app/help.rs @@ -94,7 +94,8 @@ impl<'a> Help<'a> { hide_pv: bool, color: bool, cizer: Colorizer, - term_w: Option) + term_w: Option, + max_w: Option) -> 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. diff --git a/src/app/meta.rs b/src/app/meta.rs index b22c09af..d457fdc9 100644 --- a/src/app/meta.rs +++ b/src/app/meta.rs @@ -14,6 +14,7 @@ pub struct AppMeta<'b> { pub help_str: Option<&'b str>, pub disp_ord: usize, pub term_w: Option, + pub max_w: Option, 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, } } } diff --git a/src/app/mod.rs b/src/app/mod.rs index 018c04cc..86d73a0f 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -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