mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Feature: Implement ISO output
This commit is contained in:
+24
-13
@@ -28,6 +28,7 @@ pub struct DisplayData {
|
|||||||
pub base_size: u64,
|
pub base_size: u64,
|
||||||
pub longest_string_length: usize,
|
pub longest_string_length: usize,
|
||||||
pub ls_colors: LsColors,
|
pub ls_colors: LsColors,
|
||||||
|
pub iso: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DisplayData {
|
impl DisplayData {
|
||||||
@@ -114,6 +115,7 @@ pub fn draw_it(
|
|||||||
terminal_width: usize,
|
terminal_width: usize,
|
||||||
by_filecount: bool,
|
by_filecount: bool,
|
||||||
option_root_node: Option<DisplayNode>,
|
option_root_node: Option<DisplayNode>,
|
||||||
|
iso: bool,
|
||||||
) {
|
) {
|
||||||
if option_root_node.is_none() {
|
if option_root_node.is_none() {
|
||||||
return;
|
return;
|
||||||
@@ -149,6 +151,7 @@ pub fn draw_it(
|
|||||||
base_size: root_node.size,
|
base_size: root_node.size,
|
||||||
longest_string_length,
|
longest_string_length,
|
||||||
ls_colors: LsColors::from_env().unwrap_or_default(),
|
ls_colors: LsColors::from_env().unwrap_or_default(),
|
||||||
|
iso,
|
||||||
};
|
};
|
||||||
let draw_data = DrawData {
|
let draw_data = DrawData {
|
||||||
indent: "".to_string(),
|
indent: "".to_string(),
|
||||||
@@ -318,7 +321,7 @@ fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayD
|
|||||||
display_data.num_chars_needed_on_left_most - size_as_str.chars().count();
|
display_data.num_chars_needed_on_left_most - size_as_str.chars().count();
|
||||||
size_as_str + " ".repeat(spaces_to_add).as_str()
|
size_as_str + " ".repeat(spaces_to_add).as_str()
|
||||||
} else {
|
} else {
|
||||||
format!("{:>5}", human_readable_number(node.size))
|
format!("{:>5}", human_readable_number(node.size, display_data.iso))
|
||||||
};
|
};
|
||||||
|
|
||||||
if is_biggest && display_data.colors_on {
|
if is_biggest && display_data.colors_on {
|
||||||
@@ -347,9 +350,10 @@ fn get_pretty_name(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn human_readable_number(size: u64) -> String {
|
fn human_readable_number(size: u64, iso: bool) -> String {
|
||||||
for (i, u) in UNITS.iter().enumerate() {
|
for (i, u) in UNITS.iter().enumerate() {
|
||||||
let marker = 1024u64.pow((UNITS.len() - i) as u32);
|
let num: u64 = if iso { 1000 } else { 1024 };
|
||||||
|
let marker = num.pow((UNITS.len() - i) as u32);
|
||||||
if size >= marker {
|
if size >= marker {
|
||||||
if size / marker < 10 {
|
if size / marker < 10 {
|
||||||
return format!("{:.1}{}", (size as f32 / marker as f32), u);
|
return format!("{:.1}{}", (size as f32 / marker as f32), u);
|
||||||
@@ -378,6 +382,7 @@ mod tests {
|
|||||||
base_size: 1,
|
base_size: 1,
|
||||||
longest_string_length,
|
longest_string_length,
|
||||||
ls_colors: LsColors::from_env().unwrap_or_default(),
|
ls_colors: LsColors::from_env().unwrap_or_default(),
|
||||||
|
iso: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,15 +429,21 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_human_readable_number() {
|
fn test_human_readable_number() {
|
||||||
assert_eq!(human_readable_number(1), "1B");
|
assert_eq!(human_readable_number(1, false), "1B");
|
||||||
assert_eq!(human_readable_number(956), "956B");
|
assert_eq!(human_readable_number(956, false), "956B");
|
||||||
assert_eq!(human_readable_number(1004), "1004B");
|
assert_eq!(human_readable_number(1004, false), "1004B");
|
||||||
assert_eq!(human_readable_number(1024), "1.0K");
|
assert_eq!(human_readable_number(1024, false), "1.0K");
|
||||||
assert_eq!(human_readable_number(1536), "1.5K");
|
assert_eq!(human_readable_number(1536, false), "1.5K");
|
||||||
assert_eq!(human_readable_number(1024 * 512), "512K");
|
assert_eq!(human_readable_number(1024 * 512, false), "512K");
|
||||||
assert_eq!(human_readable_number(1024 * 1024), "1.0M");
|
assert_eq!(human_readable_number(1024 * 1024, false), "1.0M");
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 - 1), "1023M");
|
assert_eq!(
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 20), "20G");
|
human_readable_number(1024 * 1024 * 1024 - 1, false),
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 1024), "1.0T");
|
"1023M"
|
||||||
|
);
|
||||||
|
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 20, false), "20G");
|
||||||
|
assert_eq!(
|
||||||
|
human_readable_number(1024 * 1024 * 1024 * 1024, false),
|
||||||
|
"1.0T"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -220,6 +220,12 @@ fn main() {
|
|||||||
.help("Specify width of output overriding the auto detection of terminal width"),
|
.help("Specify width of output overriding the auto detection of terminal width"),
|
||||||
)
|
)
|
||||||
.arg(Arg::with_name("inputs").multiple(true).default_value("."))
|
.arg(Arg::with_name("inputs").multiple(true).default_value("."))
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("iso")
|
||||||
|
.short("H")
|
||||||
|
.long("si")
|
||||||
|
.help("print sizes in powers of 1000 (e.g., 1.1G)")
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let target_dirs = options
|
let target_dirs = options
|
||||||
@@ -314,5 +320,6 @@ fn main() {
|
|||||||
terminal_width,
|
terminal_width,
|
||||||
by_filecount,
|
by_filecount,
|
||||||
tree,
|
tree,
|
||||||
|
options.is_present("iso"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user