exa/format.rs
Ben S b116889abc Change format of file sizes to be shorter
I'm copying ls here because we don't really need the 'B' for bytes to be
listed every time. I think it looks better the new way. Unlike ls, don't
list directory sizes, because I've never found the pseudo-sizes they get
given at all useful.

Also, fix a bug where aligning columns didn't work when the number of
format characters (like '\x1B' and '[') were different between each
line.
2014-05-25 15:52:36 +01:00

25 lines
634 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 formatBytes(mut amount: u64, kilo: u64, prefixes: &[&str]) -> StrBuf {
let mut prefix = 0;
while amount > kilo {
amount /= kilo;
prefix += 1;
}
format!("{}{}", amount, prefixes[prefix])
}
pub fn formatBinaryBytes(amount: u64) -> StrBuf {
formatBytes(amount, 1024, IEC_PREFIXES)
}
pub fn formatDecimalBytes(amount: u64) -> StrBuf {
formatBytes(amount, 1000, METRIC_PREFIXES)
}