pub struct ByteFormat(pub u64);
impl std::fmt::Display for ByteFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (unit_bytes, unit_char) in [
(1_000_000_000_000, 'T'),
(1_000_000_000, 'G'),
(1_000_000, 'M'),
(1_000, 'k'),
] {
if self.0 >= unit_bytes {
let whole = self.0 / unit_bytes;
let deci = (self.0 % unit_bytes) * 10 / unit_bytes;
return write!(f, "{whole}.{deci}{unit_char}");
}
}
write!(f, "{}B", self.0)
}
}