Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions completions/_dust
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ _dust() {
'(-F --only-file -t --file_types)--only-dir[Only directories will be displayed.]' \
'(-D --only-dir)-F[Only files will be displayed. (Finds your largest files)]' \
'(-D --only-dir)--only-file[Only files will be displayed. (Finds your largest files)]' \
'-k[display the size of a file or directory in kilobytes]' \
'--display-kb[display the size of a file or directory in kilobytes]' \
'*::inputs:' \
&& ret=0
}
Expand Down
2 changes: 2 additions & 0 deletions completions/_dust.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock {
[CompletionResult]::new('--only-dir', 'only-dir', [CompletionResultType]::ParameterName, 'Only directories will be displayed.')
[CompletionResult]::new('-F', 'F', [CompletionResultType]::ParameterName, 'Only files will be displayed. (Finds your largest files)')
[CompletionResult]::new('--only-file', 'only-file', [CompletionResultType]::ParameterName, 'Only files will be displayed. (Finds your largest files)')
[CompletionResult]::new('-k', 'k', [CompletionResultType]::ParameterName, 'display the size of a file or directory in kilobytes')
[CompletionResult]::new('--display-kb', 'display-kb', [CompletionResultType]::ParameterName, 'display the size of a file or directory in kilobytes')
break
}
})
Expand Down
2 changes: 1 addition & 1 deletion completions/dust.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 -z -R -f -i -v -e -t -w -H -P -D -F -k --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 --display-kb <inputs>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
2 changes: 2 additions & 0 deletions completions/dust.elv
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ set edit:completion:arg-completer[dust] = {|@words|
cand --only-dir 'Only directories will be displayed.'
cand -F 'Only files will be displayed. (Finds your largest files)'
cand --only-file 'Only files will be displayed. (Finds your largest files)'
cand -k 'display the size of a file or directory in kilobytes'
cand --display-kb 'display the size of a file or directory in kilobytes'
}
]
$completions[$command]
Expand Down
1 change: 1 addition & 0 deletions completions/dust.fish
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ complete -c dust -s H -l si -d 'print sizes in powers of 1000 (e.g., 1.1G)'
complete -c dust -s P -l no-progress -d 'Disable the progress indication.'
complete -c dust -s D -l only-dir -d 'Only directories will be displayed.'
complete -c dust -s F -l only-file -d 'Only files will be displayed. (Finds your largest files)'
complete -c dust -s k -l display-kb -d 'display the size of a file or directory in kilobytes'
5 changes: 4 additions & 1 deletion man-page/dust.1
Original file line number Diff line number Diff line change
Expand Up @@ -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\-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\-k\fR|\fB\-\-display\-kb\fR] [\fIinputs\fR]
.SH DESCRIPTION
Like du but more intuitive
.SH OPTIONS
Expand Down Expand Up @@ -84,6 +84,9 @@ Only directories will be displayed.
\fB\-F\fR, \fB\-\-only\-file\fR
Only files will be displayed. (Finds your largest files)
.TP
\fB\-k\fR, \fB\-\-display\-kb\fR
display the size of a file or directory in kilobytes
.TP
[\fIinputs\fR]

.SH VERSION
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,10 @@ pub fn build_cli() -> Command<'static> {
.help("Only files will be displayed. (Finds your largest files)"),
)
.arg(Arg::new("inputs").multiple_occurrences(true))
.arg(
Arg::new("display_kb")
.short('k')
.long("display-kb")
.help("display the size of a file or directory in kilobytes"),
)
}
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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_display_kb(&self, options: &ArgMatches) -> bool {
Some(true) == self.only_file || options.is_present("display_kb")
}
}

fn convert_min_size(input: &str, iso: bool) -> Option<usize> {
Expand Down
62 changes: 45 additions & 17 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct InitialDisplayData {
pub by_filecount: bool,
pub is_screen_reader: bool,
pub iso: bool,
pub display_kb: bool,
}

pub struct DisplayData {
Expand Down Expand Up @@ -118,6 +119,7 @@ pub fn draw_it(
terminal_width: usize,
root_node: &DisplayNode,
skip_total: bool,
display_kb: bool,
) {
let biggest = match skip_total {
false => root_node,
Expand All @@ -131,7 +133,7 @@ pub fn draw_it(
let max_size = biggest.size;
max_size.separate_with_commas().chars().count()
} else {
find_biggest_size_str(root_node, idd.iso)
find_biggest_size_str(root_node, idd.iso, display_kb)
};

assert!(
Expand Down Expand Up @@ -179,10 +181,12 @@ pub fn draw_it(
}
}

fn find_biggest_size_str(node: &DisplayNode, iso: bool) -> usize {
let mut mx = human_readable_number(node.size, iso).chars().count();
fn find_biggest_size_str(node: &DisplayNode, iso: bool, display_kb: bool) -> usize {
let mut mx = human_readable_number(node.size, iso, display_kb)
.chars()
.count();
for n in node.children.iter() {
mx = max(mx, find_biggest_size_str(n, iso));
mx = max(mx, find_biggest_size_str(n, iso, display_kb));
}
mx
}
Expand Down Expand Up @@ -364,7 +368,11 @@ fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayD
let output = if display_data.initial.by_filecount {
node.size.separate_with_commas()
} else {
human_readable_number(node.size, display_data.initial.iso)
human_readable_number(
node.size,
display_data.initial.iso,
display_data.initial.display_kb,
)
};
let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count();
let output = " ".repeat(spaces_to_add) + output.as_str();
Expand Down Expand Up @@ -396,9 +404,12 @@ fn get_pretty_name(
}
}

pub fn human_readable_number(size: u64, iso: bool) -> String {
pub fn human_readable_number(size: u64, iso: bool, display_kb: bool) -> String {
let num: u64 = if iso { 1000 } else { 1024 };
if display_kb {
return format!("{:.1}{}", (size as f32 / num as f32), 'K');
}
for (i, u) in UNITS.iter().enumerate() {
let num: u64 = if iso { 1000 } else { 1024 };
let marker = num.pow((UNITS.len() - i) as u32);
if size >= marker {
if size / marker < 10 {
Expand Down Expand Up @@ -426,6 +437,7 @@ mod tests {
by_filecount: false,
is_screen_reader: false,
iso: false,
display_kb: false,
};
DisplayData {
initial,
Expand Down Expand Up @@ -491,21 +503,37 @@ mod tests {

#[test]
fn test_human_readable_number() {
assert_eq!(human_readable_number(1, false), "1B");
assert_eq!(human_readable_number(956, false), "956B");
assert_eq!(human_readable_number(1004, false), "1004B");
assert_eq!(human_readable_number(1024, false), "1.0K");
assert_eq!(human_readable_number(1536, false), "1.5K");
assert_eq!(human_readable_number(1024 * 512, false), "512K");
assert_eq!(human_readable_number(1024 * 1024, false), "1.0M");
assert_eq!(human_readable_number(1, false, false), "1B");
assert_eq!(human_readable_number(956, false, false), "956B");
assert_eq!(human_readable_number(1004, false, false), "1004B");
assert_eq!(human_readable_number(1024, false, false), "1.0K");
assert_eq!(human_readable_number(1536, false, false), "1.5K");
assert_eq!(human_readable_number(1024 * 512, false, false), "512K");
assert_eq!(human_readable_number(1024 * 1024, false, false), "1.0M");
assert_eq!(
human_readable_number(1024 * 1024 * 1024 - 1, false),
human_readable_number(1024 * 1024 * 1024 - 1, false, false),
"1023M"
);
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 20, false), "20G");
assert_eq!(
human_readable_number(1024 * 1024 * 1024 * 1024, false),
human_readable_number(1024 * 1024 * 1024 * 20, false, false),
"20G"
);
assert_eq!(
human_readable_number(1024 * 1024 * 1024 * 1024, false, false),
"1.0T"
);
}

#[test]
fn test_human_readable_number_kb() {
assert_eq!(human_readable_number(1, false, true), "0.0K");
assert_eq!(human_readable_number(1024, false, true), "1.0K");
assert_eq!(human_readable_number(1536, false, true), "1.5K");
assert_eq!(human_readable_number(1024 * 512, false, true), "512.0K");
assert_eq!(human_readable_number(1024 * 1024, false, true), "1024.0K");
assert_eq!(
human_readable_number(1024 * 1000 * 1000 * 20, false, true),
"20000000.0K"
);
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ fn main() {

let iso = config.get_iso(&options);

let display_kb = config.get_display_kb(&options);

let ignore_hidden = config.get_ignore_hidden(&options);

let mut indicator = PIndicator::build_me();
if !config.get_disable_progress(&options) {
indicator.spawn(iso);
indicator.spawn(iso, display_kb);
}

let walk_data = WalkData {
Expand Down Expand Up @@ -229,13 +231,15 @@ fn main() {
by_filecount,
iso,
is_screen_reader: config.get_screen_reader(&options),
display_kb,
};
draw_it(
idd,
config.get_no_bars(&options),
terminal_width,
&root_node,
config.get_skip_total(&options),
display_kb,
)
}
}
Expand Down
28 changes: 21 additions & 7 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,26 @@ impl PAtomicInfo {

/* -------------------------------------------------------------------------- */

fn format_preparing_str(prog_char: char, data: &PAtomicInfo, is_iso: bool) -> String {
fn format_preparing_str(
prog_char: char,
data: &PAtomicInfo,
is_iso: bool,
is_display_kb: bool,
) -> String {
let path_in = data.current_path.get();
let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso);
let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso, is_display_kb);
format!("Preparing: {path_in} {size} ... {prog_char}")
}

fn format_indexing_str(prog_char: char, data: &PAtomicInfo, is_iso: bool) -> String {
fn format_indexing_str(
prog_char: char,
data: &PAtomicInfo,
is_iso: bool,
display_kb: bool,
) -> String {
let path_in = data.current_path.get();
let file_count = data.num_files.load(ORDERING);
let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso);
let size = human_readable_number(data.total_file_size.load(ORDERING), is_iso, display_kb);
let file_str = format!("{file_count} files, {size}");
format!("Indexing: {path_in} {file_str} ... {prog_char}")
}
Expand All @@ -99,7 +109,7 @@ impl PIndicator {
}
}

pub fn spawn(&mut self, is_iso: bool) {
pub fn spawn(&mut self, is_iso: bool, display_kb: bool) {
let data = self.data.clone();
let (stop_handler, receiver) = mpsc::channel::<()>();

Expand All @@ -118,8 +128,12 @@ impl PIndicator {
let prog_char = PROGRESS_CHARS[progress_char_i];

msg = match data.state.load(ORDERING) {
Operation::INDEXING => format_indexing_str(prog_char, &data, is_iso),
Operation::PREPARING => format_preparing_str(prog_char, &data, is_iso),
Operation::INDEXING => {
format_indexing_str(prog_char, &data, is_iso, display_kb)
}
Operation::PREPARING => {
format_preparing_str(prog_char, &data, is_iso, display_kb)
}
_ => panic!("Unknown State"),
};

Expand Down