From cd53fc74945010c06c6a6e0658a9c34e13f5a22c Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Wed, 13 Dec 2023 22:39:51 +0000 Subject: [PATCH] feat: new param: ignore-all-in-file Add new cmd line param to ignore all files or directories listed in this file --- completions/_dust | 2 ++ completions/_dust.ps1 | 2 ++ completions/dust.bash | 10 +++++++++- completions/dust.elv | 2 ++ completions/dust.fish | 1 + man-page/dust.1 | 5 ++++- src/cli.rs | 7 +++++++ src/main.rs | 20 ++++++++++++++++++++ tests/test_dir_hidden_entries/.hidden_file | 3 ++- tests/test_dir_hidden_entries/.secret | 0 tests/test_flags.rs | 14 +++++++++++++- 11 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/test_dir_hidden_entries/.secret diff --git a/completions/_dust b/completions/_dust index 3674d22..ec51dd1 100644 --- a/completions/_dust +++ b/completions/_dust @@ -21,6 +21,8 @@ _dust() { '--number-of-lines=[Number of lines of output to show. (Default is terminal_height - 10)]: : ' \ '*-X+[Exclude any file or directory with this name]: : ' \ '*--ignore-directory=[Exclude any file or directory with this name]: : ' \ +'-I+[Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter]: : ' \ +'--ignore-all-in-file=[Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter]: : ' \ '-z+[Minimum size file to include in output]: : ' \ '--min-size=[Minimum size file to include in output]: : ' \ '(-e --filter -t --file_types)*-v+[Exclude filepaths matching this regex. To ignore png files type\: -v "\\.png\$" ]: : ' \ diff --git a/completions/_dust.ps1 b/completions/_dust.ps1 index d51dec4..f59c04e 100644 --- a/completions/_dust.ps1 +++ b/completions/_dust.ps1 @@ -27,6 +27,8 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock { [CompletionResult]::new('--number-of-lines', 'number-of-lines', [CompletionResultType]::ParameterName, 'Number of lines of output to show. (Default is terminal_height - 10)') [CompletionResult]::new('-X', 'X ', [CompletionResultType]::ParameterName, 'Exclude any file or directory with this name') [CompletionResult]::new('--ignore-directory', 'ignore-directory', [CompletionResultType]::ParameterName, 'Exclude any file or directory with this name') + [CompletionResult]::new('-I', 'I ', [CompletionResultType]::ParameterName, 'Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter') + [CompletionResult]::new('--ignore-all-in-file', 'ignore-all-in-file', [CompletionResultType]::ParameterName, 'Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter') [CompletionResult]::new('-z', 'z', [CompletionResultType]::ParameterName, 'Minimum size file to include in output') [CompletionResult]::new('--min-size', 'min-size', [CompletionResultType]::ParameterName, 'Minimum size file to include in output') [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Exclude filepaths matching this regex. To ignore png files type: -v "\.png$" ') diff --git a/completions/dust.bash b/completions/dust.bash index bbef4a1..b21bc80 100644 --- a/completions/dust.bash +++ b/completions/dust.bash @@ -19,7 +19,7 @@ _dust() { case "${cmd}" in dust) - opts="-d -n -p -X -L -x -s -r -c -b -B -z -R -f -i -v -e -t -w -H -P -D -F -S -h -V --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 --stack-size --help --version [params]..." + opts="-d -n -p -X -I -L -x -s -r -c -b -B -z -R -f -i -v -e -t -w -H -P -D -F -S -h -V --depth --number-of-lines --full-paths --ignore-directory --ignore-all-in-file --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 --stack-size --help --version [params]..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -49,6 +49,14 @@ _dust() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --ignore-all-in-file) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -I) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; --min-size) COMPREPLY=($(compgen -f "${cur}")) return 0 diff --git a/completions/dust.elv b/completions/dust.elv index 036e0b8..f5faf47 100644 --- a/completions/dust.elv +++ b/completions/dust.elv @@ -24,6 +24,8 @@ set edit:completion:arg-completer[dust] = {|@words| cand --number-of-lines 'Number of lines of output to show. (Default is terminal_height - 10)' cand -X 'Exclude any file or directory with this name' cand --ignore-directory 'Exclude any file or directory with this name' + cand -I 'Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter' + cand --ignore-all-in-file 'Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter' cand -z 'Minimum size file to include in output' cand --min-size 'Minimum size file to include in output' cand -v 'Exclude filepaths matching this regex. To ignore png files type: -v "\.png$" ' diff --git a/completions/dust.fish b/completions/dust.fish index 30181cf..c36e86e 100644 --- a/completions/dust.fish +++ b/completions/dust.fish @@ -1,6 +1,7 @@ complete -c dust -s d -l depth -d 'Depth to show' -r complete -c dust -s n -l number-of-lines -d 'Number of lines of output to show. (Default is terminal_height - 10)' -r complete -c dust -s X -l ignore-directory -d 'Exclude any file or directory with this name' -r +complete -c dust -s I -l ignore-all-in-file -d 'Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter' -r complete -c dust -s z -l min-size -d 'Minimum size file to include in output' -r complete -c dust -s v -l invert-filter -d 'Exclude filepaths matching this regex. To ignore png files type: -v "\\.png$" ' -r complete -c dust -s e -l filter -d 'Only include filepaths matching this regex. For png files type: -e "\\.png$" ' -r diff --git a/man-page/dust.1 b/man-page/dust.1 index 288a663..4a71924 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\-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] [\fB\-S\fR|\fB\-\-stack\-size\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR] [\fIparams\fR] +\fBDust\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\-I\fR|\fB\-\-ignore\-all\-in\-file\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] [\fB\-S\fR|\fB\-\-stack\-size\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR] [\fIparams\fR] .SH DESCRIPTION Like du but more intuitive .SH OPTIONS @@ -21,6 +21,9 @@ Subdirectories will not have their path shortened \fB\-X\fR, \fB\-\-ignore\-directory\fR Exclude any file or directory with this name .TP +\fB\-I\fR, \fB\-\-ignore\-all\-in\-file\fR +Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by \-\-invert_filter +.TP \fB\-L\fR, \fB\-\-dereference\-links\fR dereference sym links \- Treat sym links as directories and go into them .TP diff --git a/src/cli.rs b/src/cli.rs index 2028671..468f93c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -37,6 +37,13 @@ pub fn build_cli() -> Command { .long("ignore-directory") .action(clap::ArgAction::Append) .help("Exclude any file or directory with this name"), + ) + .arg( + Arg::new("ignore_all_in_file") + .short('I') + .long("ignore-all-in-file") + .value_parser(value_parser!(String)) + .help("Exclude any file or directory with a regex matching that listed in this file, the file entries will be added to the ignore regexs provided by --invert_filter"), ) .arg( Arg::new("dereference_links") diff --git a/src/main.rs b/src/main.rs index b31421c..52c0a0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,9 @@ use display::InitialDisplayData; use filter::AggregateData; use progress::PIndicator; use progress::ORDERING; +use regex::Error; use std::collections::HashSet; +use std::fs::read_to_string; use std::panic; use std::process; use sysinfo::{System, SystemExt}; @@ -142,6 +144,24 @@ fn main() { None => vec![], }; + let ignore_from_file_result = match options.get_one::("ignore_all_in_file") { + Some(val) => read_to_string(val) + .unwrap() + .lines() + .map(Regex::new) + .collect::>>(), + None => vec![], + }; + let ignore_from_file = ignore_from_file_result + .into_iter() + .filter_map(|x| x.ok()) + .collect::>(); + + let invert_filter_regexs = invert_filter_regexs + .into_iter() + .chain(ignore_from_file) + .collect::>(); + let by_filecount = options.get_flag("by_filecount"); let limit_filesystem = options.get_flag("limit_filesystem"); let follow_links = options.get_flag("dereference_links"); diff --git a/tests/test_dir_hidden_entries/.hidden_file b/tests/test_dir_hidden_entries/.hidden_file index 32f95c0..0294700 100644 --- a/tests/test_dir_hidden_entries/.hidden_file +++ b/tests/test_dir_hidden_entries/.hidden_file @@ -1 +1,2 @@ -hi \ No newline at end of file +something +.secret diff --git a/tests/test_dir_hidden_entries/.secret b/tests/test_dir_hidden_entries/.secret new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_flags.rs b/tests/test_flags.rs index 583f87c..199ac0e 100644 --- a/tests/test_flags.rs +++ b/tests/test_flags.rs @@ -64,6 +64,7 @@ pub fn test_d_flag_works_and_still_recurses_down() { // We had a bug where running with '-d 1' would stop at the first directory and the code // would fail to recurse down let output = build_command(vec!["-d", "1", "-f", "-c", "tests/test_dir2/"]); + assert!(output.contains("1 ┌── dir")); assert!(output.contains("4 ┌─┴ test_dir2")); } @@ -73,7 +74,18 @@ pub fn test_ignore_dir() { let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]); assert!(!output.contains("dir_substring")); } -// Add test for multiple dirs - with -d 0 and maybe -d 1 check the + +#[test] +pub fn test_ignore_all_in_file() { + let output = build_command(vec![ + "-c", + "-I", + "tests/test_dir_hidden_entries/.hidden_file", + "tests/test_dir_hidden_entries/", + ]); + assert!(output.contains(" test_dir_hidden_entries")); + assert!(!output.contains(".secret")); +} #[test] pub fn test_with_bad_param() {