on master help sign in
Allow offset values in `--terminal-width`

Allows the `width` argument to `--terminal-width` to be an offset instead of an absolute number. Examples: --terminal-width=80 # Set output width to 80 characters --terminal-width=-2 # Set output width to actual_width - 2 closes #376

commitdda27b253be828c9c52f040c815e3a5f45d80010 authorsharkdp <davidpeter@web.de> dateWed 31 Oct 2018, 21:34 (8 years ago) parent78f26d0
2 files changed+23−3
src/app.rs+17−1 src/clap_app.rs+6−2
src/app.rs+17−1
@@ -196,7 +196,23 @@ impl App {
196196 term_width: self
197197 .matches
198198 .value_of("terminal-width")
199− .and_then(|w| w.parse().ok())
199+ .and_then(|w| {
200+ if w.starts_with("+") || w.starts_with("-") {
201+ // Treat argument as a delta to the current terminal width
202+ w.parse().ok().map(|delta: i16| {
203+ let old_width: u16 = Term::stdout().size().1;
204+ let new_width: i32 = old_width as i32 + delta as i32;
205+
206+ if new_width <= 0 {
207+ old_width as usize
208+ } else {
209+ new_width as usize
210+ }
211+ })
212+ } else {
213+ w.parse().ok()
214+ }
215+ })
200216 .unwrap_or(Term::stdout().size().1 as usize),
201217 loop_through: !(self.interactive_output
202218 || self.matches.value_of("color") == Some("always")
src/clap_app.rs+6−2
@@ -287,8 +287,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
287287 .long("terminal-width")
288288 .takes_value(true)
289289 .value_name("width")
290− .hidden(true)
291− .help("Set the width of the terminal"),
290+ .hidden_short_help(true)
291+ .help(
292+ "Explicitly set the width of the terminal instead of determining it \
293+ automatically. If prefixed with '+' or '-', the value will be treated \
294+ as an offset to the actual terminal width.",
295+ ),
292296 )
293297 .arg(
294298 Arg::with_name("no-config")