Skip to content

Commit

Permalink
feat(help): allow for limiting detected terminal width
Browse files Browse the repository at this point in the history
Useful when, e.g., the terminal is fullscreen

Closes #653
  • Loading branch information
nabijaczleweli committed Sep 10, 2016
1 parent 377b5f2 commit a43e28a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/app/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand All @@ -34,6 +35,7 @@ impl<'b> Default for AppMeta<'b> {
template: None,
aliases: None,
term_w: None,
max_w: None,
}
}
}
Expand Down Expand Up @@ -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,
}
}
}
28 changes: 28 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a43e28a

Please sign in to comment.