diff --git a/src/modes/common.rs b/src/modes/common.rs index 57fea08..87862c2 100644 --- a/src/modes/common.rs +++ b/src/modes/common.rs @@ -30,8 +30,28 @@ pub fn get_meta_from_env() -> HashMap { } pub fn format_size_human_readable(size: u64) -> String { - let options = FormatSizeOptions::from(BINARY).decimal_places(1); - humansize::format_size(size, options) + const UNITS: &[&str] = &["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei"]; + const THRESHOLD: u64 = 1024; + + if size == 0 { + return "0".to_string(); + } + + let mut size_f = size as f64; + let mut unit_index = 0; + + while size_f >= THRESHOLD as f64 && unit_index < UNITS.len() - 1 { + size_f /= THRESHOLD as f64; + unit_index += 1; + } + + if unit_index == 0 { + format!("{}", size) + } else if size_f.fract() == 0.0 { + format!("{}{}", size_f as u64, UNITS[unit_index]) + } else { + format!("{:.1}{}", size_f, UNITS[unit_index]) + } } pub fn format_size(size: u64, human_readable: bool) -> String {