From e15cf0c42e772580364f248c09f8a75c1cf0618c Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Wed, 25 Nov 2020 14:03:58 +0000 Subject: [PATCH] [core] New flag: width Add support for width flag https://github.com/bootandy/dust/issues/126 Requested because some people may cat the output All terminal height/width detection is now in the main file. One method now has too many args for clippy, this complaint is valid and in the future we should consider pulling these out into a separate object. --- src/display.rs | 16 +++------------- src/main.rs | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/display.rs b/src/display.rs index a0309aa..d7d6459 100644 --- a/src/display.rs +++ b/src/display.rs @@ -5,8 +5,6 @@ use crate::utils::{Errors, Node}; use self::ansi_term::Colour::Red; use lscolors::{LsColors, Style}; -use terminal_size::{terminal_size, Height, Width}; - use unicode_width::UnicodeWidthStr; use stfu8::encode_u8; @@ -20,7 +18,6 @@ use thousands::Separable; static UNITS: [char; 4] = ['T', 'G', 'M', 'K']; static BLOCKS: [char; 5] = ['█', '▓', '▒', '░', ' ']; -static DEFAULT_TERMINAL_WIDTH: u16 = 80; pub struct DisplayData { pub short_paths: bool, @@ -108,21 +105,14 @@ impl DrawData<'_> { } } -fn get_width_of_terminal() -> u16 { - // Windows CI runners detect a very low terminal width - if let Some((Width(w), Height(_h))) = terminal_size() { - max(w, DEFAULT_TERMINAL_WIDTH) - } else { - DEFAULT_TERMINAL_WIDTH - } -} - +#[allow(clippy::too_many_arguments)] pub fn draw_it( errors: Errors, use_full_path: bool, is_reversed: bool, no_colors: bool, no_percents: bool, + terminal_width: usize, by_filecount: bool, root_node: Node, ) { @@ -139,7 +129,7 @@ pub fn draw_it( 5 // Under normal usage we need 5 chars to display the size of a directory }; - let terminal_width = get_width_of_terminal() as usize - 9 - num_chars_needed_on_left_most; + let terminal_width = terminal_width - 9 - num_chars_needed_on_left_most; let num_indent_chars = 3; let longest_string_length = root_node .children diff --git a/src/main.rs b/src/main.rs index b034d7f..1a6d31e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod display; mod utils; static DEFAULT_NUMBER_OF_LINES: usize = 30; +static DEFAULT_TERMINAL_WIDTH: usize = 80; #[cfg(windows)] fn init_color(no_color: bool) -> bool { @@ -52,6 +53,15 @@ fn get_height_of_terminal() -> usize { } } +fn get_width_of_terminal() -> usize { + // Windows CI runners detect a very low terminal width + if let Some((Width(w), Height(_h))) = terminal_size() { + max(w as usize, DEFAULT_TERMINAL_WIDTH) + } else { + DEFAULT_TERMINAL_WIDTH + } +} + fn main() { let default_height = get_height_of_terminal(); let def_num_str = default_height.to_string(); @@ -71,7 +81,7 @@ fn main() { Arg::with_name("number_of_lines") .short("n") .long("number-of-lines") - .help("Number of lines of output to show") + .help("Number of lines of output to show. This is Height, (but h is help)") .takes_value(true) .default_value(def_num_str.as_ref()), ) @@ -132,6 +142,14 @@ fn main() { .long("ignore_hidden") .help("Obey .git_ignore rules & Do not display hidden files"), ) + .arg( + Arg::with_name("width") + .short("w") + .long("terminal_width") + .takes_value(true) + .number_of_values(1) + .help("Specify width of output overriding the auto detection of terminal width"), + ) .arg(Arg::with_name("inputs").multiple(true)) .get_matches(); @@ -151,6 +169,11 @@ fn main() { } }; + let terminal_width = match value_t!(options.value_of("width"), usize) { + Ok(v) => v, + Err(_) => get_width_of_terminal(), + }; + let depth = options.value_of("depth").and_then(|depth| { depth .parse::() @@ -197,6 +220,7 @@ fn main() { !options.is_present("reverse"), no_colors, options.is_present("no_bars"), + terminal_width, by_filecount, tree, );