FEATURE: support only directories will be displayed. Flag -D

This commit is contained in:
DIRE
2022-09-30 11:32:46 +09:00
committed by andy.boot
parent e858f9e976
commit 2ca7177446
10 changed files with 34 additions and 7 deletions
+1
View File
@@ -59,6 +59,7 @@ Usage: dust -p (full-path - Show fullpath of the subdirectories)
Usage: dust -s (apparent-size - shows the length of the file as opposed to the amount of disk space it uses) Usage: dust -s (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
Usage: dust -n 30 (Shows 30 directories instead of the default [default is terminal height]) Usage: dust -n 30 (Shows 30 directories instead of the default [default is terminal height])
Usage: dust -d 3 (Shows 3 levels of subdirectories) Usage: dust -d 3 (Shows 3 levels of subdirectories)
Usage: dust -D (Show only directories (eg dust -D))
Usage: dust -r (reverse order of output) Usage: dust -r (reverse order of output)
Usage: dust -H (si print sizes in powers of 1000 instead of 1024) Usage: dust -H (si print sizes in powers of 1000 instead of 1024)
Usage: dust -X ignore (ignore all files and directories with the name 'ignore') Usage: dust -X ignore (ignore all files and directories with the name 'ignore')
+2
View File
@@ -54,6 +54,8 @@ _dust() {
'(-d --depth)--file_types[show only these file types]' \ '(-d --depth)--file_types[show only these file types]' \
'-H[print sizes in powers of 1000 (e.g., 1.1G)]' \ '-H[print sizes in powers of 1000 (e.g., 1.1G)]' \
'--si[print sizes in powers of 1000 (e.g., 1.1G)]' \ '--si[print sizes in powers of 1000 (e.g., 1.1G)]' \
'-D[Only directories will be displayed.]' \
'--only-dir[Only directories will be displayed.]' \
'*::inputs:' \ '*::inputs:' \
&& ret=0 && ret=0
} }
+2
View File
@@ -60,6 +60,8 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock {
[CompletionResult]::new('--file_types', 'file_types', [CompletionResultType]::ParameterName, 'show only these file types') [CompletionResult]::new('--file_types', 'file_types', [CompletionResultType]::ParameterName, 'show only these file types')
[CompletionResult]::new('-H', 'H', [CompletionResultType]::ParameterName, 'print sizes in powers of 1000 (e.g., 1.1G)') [CompletionResult]::new('-H', 'H', [CompletionResultType]::ParameterName, 'print sizes in powers of 1000 (e.g., 1.1G)')
[CompletionResult]::new('--si', 'si', [CompletionResultType]::ParameterName, 'print sizes in powers of 1000 (e.g., 1.1G)') [CompletionResult]::new('--si', 'si', [CompletionResultType]::ParameterName, 'print sizes in powers of 1000 (e.g., 1.1G)')
[CompletionResult]::new('-D', 'D', [CompletionResultType]::ParameterName, 'Only directories will be displayed.')
[CompletionResult]::new('--only-dir', 'only-dir', [CompletionResultType]::ParameterName, 'Only directories will be displayed.')
break break
} }
}) })
+1 -1
View File
@@ -19,7 +19,7 @@ _dust() {
case "${cmd}" in case "${cmd}" in
dust) dust)
opts="-h -V -d -n -p -X -x -s -r -c -b -z -f -i -v -e -t -w -H --help --version --depth --number-of-lines --full-paths --ignore-directory --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si <inputs>..." opts="-h -V -d -n -p -X -x -s -r -c -b -z -f -i -v -e -t -w -H -D --help --version --depth --number-of-lines --full-paths --ignore-directory --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --only-dir <inputs>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
+2
View File
@@ -57,6 +57,8 @@ set edit:completion:arg-completer[dust] = {|@words|
cand --file_types 'show only these file types' cand --file_types 'show only these file types'
cand -H 'print sizes in powers of 1000 (e.g., 1.1G)' cand -H 'print sizes in powers of 1000 (e.g., 1.1G)'
cand --si 'print sizes in powers of 1000 (e.g., 1.1G)' cand --si 'print sizes in powers of 1000 (e.g., 1.1G)'
cand -D 'Only directories will be displayed.'
cand --only-dir 'Only directories will be displayed.'
} }
] ]
$completions[$command] $completions[$command]
+1
View File
@@ -18,3 +18,4 @@ complete -c dust -s f -l filecount -d 'Directory \'size\' is number of child fil
complete -c dust -s i -l ignore_hidden -d 'Do not display hidden files' complete -c dust -s i -l ignore_hidden -d 'Do not display hidden files'
complete -c dust -s t -l file_types -d 'show only these file types' complete -c dust -s t -l file_types -d 'show only these file types'
complete -c dust -s H -l si -d 'print sizes in powers of 1000 (e.g., 1.1G)' complete -c dust -s H -l si -d 'print sizes in powers of 1000 (e.g., 1.1G)'
complete -c dust -s D -l only-dir -d 'Only directories will be displayed.'
+6
View File
@@ -132,4 +132,10 @@ pub fn build_cli() -> Command<'static> {
.help("print sizes in powers of 1000 (e.g., 1.1G)") .help("print sizes in powers of 1000 (e.g., 1.1G)")
) )
.arg(Arg::new("inputs").multiple_occurrences(true)) .arg(Arg::new("inputs").multiple_occurrences(true))
.arg(
Arg::new("only_dir")
.short('D')
.long("only-dir")
.help("Only directories will be displayed."),
)
} }
+4
View File
@@ -19,6 +19,7 @@ pub struct Config {
pub ignore_hidden: Option<bool>, pub ignore_hidden: Option<bool>,
pub iso: Option<bool>, pub iso: Option<bool>,
pub min_size: Option<String>, pub min_size: Option<String>,
pub only_dir: Option<bool>,
} }
impl Config { impl Config {
@@ -61,6 +62,9 @@ impl Config {
size_from_param size_from_param
} }
} }
pub fn get_only_dir(&self, options: &ArgMatches) -> bool {
Some(true) == self.only_dir || options.is_present("only_dir")
}
} }
fn convert_min_size(input: &str, iso: bool) -> Option<usize> { fn convert_min_size(input: &str, iso: bool) -> Option<usize> {
+12 -4
View File
@@ -8,6 +8,7 @@ use std::path::PathBuf;
pub fn get_biggest( pub fn get_biggest(
top_level_nodes: Vec<Node>, top_level_nodes: Vec<Node>,
min_size: Option<usize>, min_size: Option<usize>,
only_dir: bool,
n: usize, n: usize,
depth: usize, depth: usize,
using_a_filter: bool, using_a_filter: bool,
@@ -23,14 +24,14 @@ pub fn get_biggest(
let mut allowed_nodes = HashSet::new(); let mut allowed_nodes = HashSet::new();
allowed_nodes.insert(root.name.as_path()); allowed_nodes.insert(root.name.as_path());
heap = add_children(using_a_filter, min_size, &root, depth, heap); heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap);
for _ in number_top_level_nodes..n { for _ in number_top_level_nodes..n {
let line = heap.pop(); let line = heap.pop();
match line { match line {
Some(line) => { Some(line) => {
allowed_nodes.insert(line.name.as_path()); allowed_nodes.insert(line.name.as_path());
heap = add_children(using_a_filter, min_size, line, depth, heap); heap = add_children(using_a_filter, min_size, only_dir, line, depth, heap);
} }
None => break, None => break,
} }
@@ -41,15 +42,22 @@ pub fn get_biggest(
fn add_children<'a>( fn add_children<'a>(
using_a_filter: bool, using_a_filter: bool,
min_size: Option<usize>, min_size: Option<usize>,
only_dir: bool,
file_or_folder: &'a Node, file_or_folder: &'a Node,
depth: usize, depth: usize,
mut heap: BinaryHeap<&'a Node>, mut heap: BinaryHeap<&'a Node>,
) -> BinaryHeap<&'a Node> { ) -> BinaryHeap<&'a Node> {
if depth > file_or_folder.depth { if depth > file_or_folder.depth {
heap.extend(file_or_folder.children.iter().filter(|c| match min_size { heap.extend(
file_or_folder
.children
.iter()
.filter(|c| match min_size {
Some(ms) => c.size > ms as u64, Some(ms) => c.size > ms as u64,
None => !using_a_filter || c.name.is_file() || c.size > 0, None => !using_a_filter || c.name.is_file() || c.size > 0,
})) })
.filter(|c| if only_dir { c.name.is_dir() } else { true }),
)
} }
heap heap
} }
+1
View File
@@ -181,6 +181,7 @@ fn main() {
false => get_biggest( false => get_biggest(
top_level_nodes, top_level_nodes,
config.get_min_size(&options, iso), config.get_min_size(&options, iso),
config.get_only_dir(&options),
number_of_lines, number_of_lines,
depth, depth,
options.values_of("filter").is_some() || options.value_of("invert_filter").is_some(), options.values_of("filter").is_some() || options.value_of("invert_filter").is_some(),