diff --git a/src/app/help.rs b/src/app/help.rs index f0d1e24bb58..b8df90e4234 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 b22c09af5e4..d457fdc9c01 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 018c04cc20b..86d73a0fb92 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