diff options
author | andy.boot <bootandy@gmail.com> | 2023-11-09 21:52:13 +0000 |
---|---|---|
committer | andy.boot <bootandy@gmail.com> | 2023-11-23 22:34:24 +0000 |
commit | 6e0505bfd7a0c4a9932d1c8c4ec5211fc20ac562 (patch) | |
tree | 79a3bc2136d7e031fa009c95976729a5d64ed73a | |
parent | 24bdbf036e34bdce6dfaf93c8bc9ccb267d645bd (diff) |
feat: Reverse direction of bars
Add bars on right option
Add tests
-rw-r--r-- | completions/_dust | 2 | ||||
-rw-r--r-- | completions/_dust.ps1 | 2 | ||||
-rw-r--r-- | completions/dust.bash | 2 | ||||
-rw-r--r-- | completions/dust.elv | 2 | ||||
-rw-r--r-- | completions/dust.fish | 1 | ||||
-rw-r--r-- | man-page/dust.1 | 5 | ||||
-rw-r--r-- | src/cli.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/display.rs | 70 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | tests/test_exact_output.rs | 9 |
11 files changed, 96 insertions, 8 deletions
diff --git a/completions/_dust b/completions/_dust index c15e06e..f201070 100644 --- a/completions/_dust +++ b/completions/_dust @@ -47,6 +47,8 @@ _dust() { '--no-colors[No colors will be printed (Useful for commands like: watch)]' \ '-b[No percent bars or percentages will be displayed]' \ '--no-percent-bars[No percent bars or percentages will be displayed]' \ +'-B[percent bars moved to right side of screen]' \ +'--bars-on-right[percent bars moved to right side of screen]' \ '-R[For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)]' \ '--screen-reader[For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)]' \ '--skip-total[No total row will be displayed]' \ diff --git a/completions/_dust.ps1 b/completions/_dust.ps1 index 865ee38..0f4ffa8 100644 --- a/completions/_dust.ps1 +++ b/completions/_dust.ps1 @@ -53,6 +53,8 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock { [CompletionResult]::new('--no-colors', 'no-colors', [CompletionResultType]::ParameterName, 'No colors will be printed (Useful for commands like: watch)') [CompletionResult]::new('-b', 'b', [CompletionResultType]::ParameterName, 'No percent bars or percentages will be displayed') [CompletionResult]::new('--no-percent-bars', 'no-percent-bars', [CompletionResultType]::ParameterName, 'No percent bars or percentages will be displayed') + [CompletionResult]::new('-B', 'B', [CompletionResultType]::ParameterName, 'percent bars moved to right side of screen') + [CompletionResult]::new('--bars-on-right', 'bars-on-right', [CompletionResultType]::ParameterName, 'percent bars moved to right side of screen') [CompletionResult]::new('-R', 'R', [CompletionResultType]::ParameterName, 'For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)') [CompletionResult]::new('--screen-reader', 'screen-reader', [CompletionResultType]::ParameterName, 'For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)') [CompletionResult]::new('--skip-total', 'skip-total', [CompletionResultType]::ParameterName, 'No total row will be displayed') diff --git a/completions/dust.bash b/completions/dust.bash index 83d7b65..816ce76 100644 --- a/completions/dust.bash +++ b/completions/dust.bash @@ -19,7 +19,7 @@ _dust() { case "${cmd}" in dust) - opts="-h -V -d -n -p -X -L -x -s -r -c -b -z -R -f -i -v -e -t -w -H -P -D -F --help --version --depth --number-of-lines --full-paths --ignore-directory --dereference-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --screen-reader --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --no-progress --only-dir --only-file <inputs>..." + opts="-h -V -d -n -p -X -L -x -s -r -c -b -B -z -R -f -i -v -e -t -w -H -P -D -F --help --version --depth --number-of-lines --full-paths --ignore-directory --dereference-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --bars-on-right --min-size --screen-reader --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --no-progress --only-dir --only-file <inputs>..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/dust.elv b/completions/dust.elv index af7ccb3..7b835e7 100644 --- a/completions/dust.elv +++ b/completions/dust.elv @@ -50,6 +50,8 @@ set edit:completion:arg-completer[dust] = {|@words| cand --no-colors 'No colors will be printed (Useful for commands like: watch)' cand -b 'No percent bars or percentages will be displayed' cand --no-percent-bars 'No percent bars or percentages will be displayed' + cand -B 'percent bars moved to right side of screen' + cand --bars-on-right 'percent bars moved to right side of screen' cand -R 'For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)' cand --screen-reader 'For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)' cand --skip-total 'No total row will be displayed' diff --git a/completions/dust.fish b/completions/dust.fish index b08ec96..957b503 100644 --- a/completions/dust.fish +++ b/completions/dust.fish @@ -14,6 +14,7 @@ complete -c dust -s s -l apparent-size -d 'Use file length instead of blocks' complete -c dust -s r -l reverse -d 'Print tree upside down (biggest highest)' complete -c dust -s c -l no-colors -d 'No colors will be printed (Useful for commands like: watch)' complete -c dust -s b -l no-percent-bars -d 'No percent bars or percentages will be displayed' +complete -c dust -s B -l bars-on-right -d 'percent bars moved to right side of screen' complete -c dust -s R -l screen-reader -d 'For screen readers. Removes bars. Adds new column: depth level (May want to use -p too for full path)' complete -c dust -l skip-total -d 'No total row will be displayed' complete -c dust -s f -l filecount -d 'Directory \'size\' is number of child files instead of disk size' diff --git a/man-page/dust.1 b/man-page/dust.1 index 86db4d1..8d9219b 100644 --- a/man-page/dust.1 +++ b/man-page/dust.1 @@ -4,7 +4,7 @@ .SH NAME Dust \- Like du but more intuitive .SH SYNOPSIS -\fBDust\fR [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR] [\fB\-d\fR|\fB\-\-depth\fR] [\fB\-n\fR|\fB\-\-number\-of\-lines\fR] [\fB\-p\fR|\fB\-\-full\-paths\fR] [\fB\-X\fR|\fB\-\-ignore\-directory\fR] [\fB\-L\fR|\fB\-\-dereference\-links\fR] [\fB\-x\fR|\fB\-\-limit\-filesystem\fR] [\fB\-s\fR|\fB\-\-apparent\-size\fR] [\fB\-r\fR|\fB\-\-reverse\fR] [\fB\-c\fR|\fB\-\-no\-colors\fR] [\fB\-b\fR|\fB\-\-no\-percent\-bars\fR] [\fB\-z\fR|\fB\-\-min\-size\fR] [\fB\-R\fR|\fB\-\-screen\-reader\fR] [\fB\-\-skip\-total\fR] [\fB\-f\fR|\fB\-\-filecount\fR] [\fB\-i\fR|\fB\-\-ignore_hidden\fR] [\fB\-v\fR|\fB\-\-invert\-filter\fR] [\fB\-e\fR|\fB\-\-filter\fR] [\fB\-t\fR|\fB\-\-file_types\fR] [\fB\-w\fR|\fB\-\-terminal_width\fR] [\fB\-H\fR|\fB\-\-si\fR] [\fB\-P\fR|\fB\-\-no\-progress\fR] [\fB\-D\fR|\fB\-\-only\-dir\fR] [\fB\-F\fR|\fB\-\-only\-file\fR] [\fIinputs\fR] +\fBDust\fR [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR] [\fB\-d\fR|\fB\-\-depth\fR] [\fB\-n\fR|\fB\-\-number\-of\-lines\fR] [\fB\-p\fR|\fB\-\-full\-paths\fR] [\fB\-X\fR|\fB\-\-ignore\-directory\fR] [\fB\-L\fR|\fB\-\-dereference\-links\fR] [\fB\-x\fR|\fB\-\-limit\-filesystem\fR] [\fB\-s\fR|\fB\-\-apparent\-size\fR] [\fB\-r\fR|\fB\-\-reverse\fR] [\fB\-c\fR|\fB\-\-no\-colors\fR] [\fB\-b\fR|\fB\-\-no\-percent\-bars\fR] [\fB\-B\fR|\fB\-\-bars\-on\-right\fR] [\fB\-z\fR|\fB\-\-min\-size\fR] [\fB\-R\fR|\fB\-\-screen\-reader\fR] [\fB\-\-skip\-total\fR] [\fB\-f\fR|\fB\-\-filecount\fR] [\fB\-i\fR|\fB\-\-ignore_hidden\fR] [\fB\-v\fR|\fB\-\-invert\-filter\fR] [\fB\-e\fR|\fB\-\-filter\fR] [\fB\-t\fR|\fB\-\-file_types\fR] [\fB\-w\fR|\fB\-\-terminal_width\fR] [\fB\-H\fR|\fB\-\-si\fR] [\fB\-P\fR|\fB\-\-no\-progress\fR] [\fB\-D\fR|\fB\-\-only\-dir\fR] [\fB\-F\fR|\fB\-\-only\-file\fR] [\fIinputs\fR] .SH DESCRIPTION Like du but more intuitive .SH OPTIONS @@ -45,6 +45,9 @@ No colors will be printed (Useful for commands like: watch) \fB\-b\fR, \fB\-\-no\-percent\-bars\fR No percent bars or percentages will be displayed .TP +\fB\-B\fR, \fB\-\-bars\-on\-right\fR +percent bars moved to right side of screen +.TP \fB\-z\fR, \fB\-\-min\-size\fR Minimum size file to include in output .TP @@ -74,6 +74,12 @@ pub fn build_cli() -> Command<'static> { .help("No percent bars or percentages will be displayed"), ) .arg( + Arg::new("bars_on_right") + .short('B') + .long("bars-on-right") + .help("percent bars moved to right side of screen"), + ) + .arg( Arg::new("min_size") .short('z') .long("min-size") diff --git a/src/config.rs b/src/config.rs index 17cb244..f69dc51 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,7 @@ pub struct Config { pub only_file: Option<bool>, pub disable_progress: Option<bool>, pub depth: Option<usize>, + pub bars_on_right: Option<bool>, } impl Config { @@ -93,6 +94,9 @@ impl Config { pub fn get_only_file(&self, options: &ArgMatches) -> bool { Some(true) == self.only_file || options.is_present("only_file") } + pub fn get_bars_on_right(&self, options: &ArgMatches) -> bool { + Some(true) == self.bars_on_right || options.is_present("bars_on_right") + } } fn convert_min_size(input: &str, iso: bool) -> Option<usize> { diff --git a/src/display.rs b/src/display.rs index e6837cd..0516f2f 100644 --- a/src/display.rs +++ b/src/display.rs @@ -24,6 +24,7 @@ pub struct InitialDisplayData { pub by_filecount: bool, pub is_screen_reader: bool, pub iso: bool, + pub bars_on_right: bool, } pub struct DisplayData { @@ -98,7 +99,13 @@ impl DrawData<'_> { let mut new_bar = "".to_string(); let idx = 5 - level.clamp(1, 4); - for c in self.percent_bar.chars() { + let itr: Box<dyn Iterator<Item = char>> = if self.display_data.initial.bars_on_right { + Box::new(self.percent_bar.chars()) + } else { + Box::new(self.percent_bar.chars().rev()) + }; + + for c in itr { num_not_my_bar -= 1; if num_not_my_bar <= 0 { new_bar.push(BLOCKS[0]); @@ -108,7 +115,11 @@ impl DrawData<'_> { new_bar.push(c); } } - new_bar + if self.display_data.initial.bars_on_right { + new_bar + } else { + new_bar.chars().rev().collect() + } } } @@ -426,6 +437,7 @@ mod tests { by_filecount: false, is_screen_reader: false, iso: false, + bars_on_right: false, }; DisplayData { initial, @@ -508,4 +520,58 @@ mod tests { "1.0T" ); } + + #[cfg(test)] + fn build_draw_data<'a>(disp: &'a DisplayData, size: u32) -> (DrawData<'a>, DisplayNode) { + let n = DisplayNode { + name: PathBuf::from("/short"), + size: 2_u64.pow(size), + children: vec![], + }; + let first_size_bar = repeat(BLOCKS[0]).take(13).collect(); + let dd = DrawData { + indent: "".into(), + percent_bar: first_size_bar, + display_data: disp, + }; + (dd, n) + } + + #[test] + fn test_draw_data() { + let disp = &get_fake_display_data(20); + let (dd, n) = build_draw_data(disp, 12); + let bar = dd.generate_bar(&n, 1); + assert_eq!(bar, "█████████████"); + } + + #[test] + fn test_draw_data2() { + let disp = &get_fake_display_data(20); + let (dd, n) = build_draw_data(disp, 11); + let bar = dd.generate_bar(&n, 2); + assert_eq!(bar, "███████░░░░░░"); + } + #[test] + fn test_draw_data3() { + let mut disp = get_fake_display_data(20); + let (dd, n) = build_draw_data(&disp, 11); + let bar = dd.generate_bar(&n, 3); + assert_eq!(bar, "███████▒▒▒▒▒▒"); + + disp.initial.bars_on_right = true; + let (dd, n) = build_draw_data(&disp, 11); + let bar = dd.generate_bar(&n, 3); + assert_eq!(bar, "▒▒▒▒▒▒███████") + } + #[test] + fn test_draw_data4() { + let disp = &get_fake_display_data(20); + let (dd, n) = build_draw_data(disp, 10); + // After 4 we have no more levels of shading so 4+ is the same + let bar = dd.generate_bar(&n, 4); + assert_eq!(bar, "████▓▓▓▓▓▓▓▓▓"); + let bar = dd.generate_bar(&n, 5); + assert_eq!(bar, "████▓▓▓▓▓▓▓▓▓"); + } } diff --git a/src/main.rs b/src/main.rs index 998fd31..0595924 100644 --- a/src/main.rs +++ b/src/main.rs @@ -214,6 +214,7 @@ fn main() { by_filecount, iso, is_screen_reader: config.get_screen_reader(&options), + bars_on_right: config.get_bars_on_right(&options), }; draw_it( idd, diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 4302c0b..89a5014 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -60,7 +60,7 @@ fn exact_output_test<T: AsRef<OsStr>>(valid_outputs: Vec<String>, command_args: #[test] pub fn test_main_basic() { // -c is no color mode - This makes testing much simpler - exact_output_test(main_output(), vec!["-c", "/tmp/test_dir/"]) + exact_output_test(main_output(), vec!["-c", "-B", "/tmp/test_dir/"]) } #[cfg_attr(target_os = "windows", ignore)] @@ -68,6 +68,7 @@ pub fn test_main_basic() { pub fn test_main_multi_arg() { let command_args = vec![ "-c", + "-B", "/tmp/test_dir/many/", "/tmp/test_dir", "/tmp/test_dir", @@ -102,7 +103,7 @@ fn main_output() -> Vec<String> { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_main_long_paths() { - let command_args = vec!["-c", "-p", "/tmp/test_dir/"]; + let command_args = vec!["-c", "-p", "-B", "/tmp/test_dir/"]; exact_output_test(main_output_long_paths(), command_args); } @@ -130,7 +131,7 @@ fn main_output_long_paths() -> Vec<String> { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_substring_of_names_and_long_names() { - let command_args = vec!["-c", "/tmp/test_dir2"]; + let command_args = vec!["-c", "-B", "/tmp/test_dir2"]; exact_output_test(no_substring_of_names_output(), command_args); } @@ -164,7 +165,7 @@ fn no_substring_of_names_output() -> Vec<String> { #[cfg_attr(target_os = "windows", ignore)] #[test] pub fn test_unicode_directories() { - let command_args = vec!["-c", "/tmp/test_dir_unicode"]; + let command_args = vec!["-c", "-B", "/tmp/test_dir_unicode"]; exact_output_test(unicode_dir(), command_args); } |