summaryrefslogtreecommitdiffstats
path: root/src/common.rs
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-04 20:42:15 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-04 20:42:15 +0530
commita1ecbf0a1a68ca7bb9f4e372e89b66ac3a945264 (patch)
treec1ca24667caa36da2c577335cc7293f19249c930 /src/common.rs
parent9d430a23d950edabfbeca55ba4805c48dfde99a3 (diff)
Add support for static byte units
Diffstat (limited to 'src/common.rs')
-rw-r--r--src/common.rs32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/common.rs b/src/common.rs
index 0d688cd..2bb428f 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -2,6 +2,7 @@ use crate::{
interactive::SortMode,
traverse::{EntryData, Tree, TreeIndex},
};
+use byte_unit::{n_gb_bytes, n_gib_bytes, n_mb_bytes, n_mib_bytes, ByteUnit};
use itertools::Itertools;
use jwalk::WalkDir;
use petgraph::Direction;
@@ -59,6 +60,14 @@ pub enum ByteFormat {
Binary,
/// raw bytes, without additional formatting
Bytes,
+ /// only gigabytes without smart-unit
+ GB,
+ /// only gibibytes without smart-unit
+ GiB,
+ /// only megabytes without smart-unit
+ MB,
+ /// only mebibytes without smart-unit
+ MiB,
}
impl ByteFormat {
@@ -67,6 +76,8 @@ impl ByteFormat {
match self {
Metric | Binary => 10,
Bytes => 12,
+ MiB|MB => 12,
+ _ => 10,
}
}
pub fn display(&self, bytes: u64) -> ByteFormatDisplay {
@@ -87,14 +98,23 @@ impl fmt::Display for ByteFormatDisplay {
use byte_unit::Byte;
use ByteFormat::*;
- let binary = match self.format {
+ let format = match self.format {
Bytes => return write!(f, "{} b", self.bytes),
- Binary => true,
- Metric => false,
+ Binary => (true, None),
+ Metric => (false, None),
+ GB => (false, Some((n_gb_bytes!(1), ByteUnit::GB))),
+ GiB => (false, Some((n_gib_bytes!(1), ByteUnit::GiB))),
+ MB => (false, Some((n_mb_bytes!(1), ByteUnit::MB))),
+ MiB => (false, Some((n_mib_bytes!(1), ByteUnit::MiB))),
};
- let b = Byte::from_bytes(self.bytes as u128)
- .get_appropriate_unit(binary)
- .format(2);
+
+ let b = match format {
+ (_, Some((divisor, unit))) => Byte::from_unit(self.bytes as f64 / divisor as f64, unit)
+ .expect("byte count > 0")
+ .get_adjusted_unit(unit),
+ (binary, None) => Byte::from_bytes(self.bytes as u128).get_appropriate_unit(binary),
+ }
+ .format(2);
let mut splits = b.split(' ');
match (splits.next(), splits.next()) {
(Some(bytes), Some(unit)) => write!(