exa/format.rs
Ben S bc4df2cf3c Darken file size suffixes
I think this is necessary because 'bytes' currently has no 'B' suffix, and it's
kind of hard to distinguish a long number from a suffix.
2014-06-04 14:00:25 +01:00

25 lines
687 B
Rust

static METRIC_PREFIXES: &'static [&'static str] = &[
"", "K", "M", "G", "T", "P", "E", "Z", "Y"
];
static IEC_PREFIXES: &'static [&'static str] = &[
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
];
fn format_bytes(mut amount: u64, kilo: u64, prefixes: &[&str]) -> (String, String) {
let mut prefix = 0;
while amount > kilo {
amount /= kilo;
prefix += 1;
}
return (format!("{}", amount), prefixes[prefix].to_string());
}
pub fn format_IEC_bytes(amount: u64) -> (String, String) {
format_bytes(amount, 1024, IEC_PREFIXES)
}
pub fn format_metric_bytes(amount: u64) -> (String, String) {
format_bytes(amount, 1000, METRIC_PREFIXES)
}