summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 11:34:45 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 11:34:45 +0530
commit6db07e2e69f7f674191311719054a245e8c8b886 (patch)
treeac9bac2d4a0f031c2b267fc5d49eef5f0a3aee7c
parenta19e3d76fe559f59be467b4967156509e6f26715 (diff)
Add byte formatting
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs13
-rw-r--r--tests/snapshots/success-no-arguments2
3 files changed, 14 insertions, 2 deletions
diff --git a/Cargo.toml b/Cargo.toml
index ad29276..8c57062 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,6 +10,7 @@ failure = "0.1.1"
failure-tools = "4.0.2"
structopt = "0.2.14"
jwalk = "0.4.0"
+byte-unit = "2.1.0"
[[bin]]
name="dua"
diff --git a/src/lib.rs b/src/lib.rs
index ca84134..ee3b35a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,12 @@ pub struct WalkOptions {
}
impl WalkOptions {
+ pub fn format_bytes(&self, b: u64) -> String {
+ use byte_unit::Byte;
+ Byte::from_bytes(b as u128)
+ .get_appropriate_unit(false)
+ .format(2)
+ }
pub fn iter_from_path(&self, path: &Path) -> WalkDir {
WalkDir::new(path)
.preload_metadata(true)
@@ -57,7 +63,12 @@ mod aggregate {
}
}
- writeln!(out, "{}\t{}", num_bytes, path.as_ref().display())?;
+ writeln!(
+ out,
+ "{}\t{}",
+ options.format_bytes(num_bytes),
+ path.as_ref().display()
+ )?;
}
Ok(res)
}
diff --git a/tests/snapshots/success-no-arguments b/tests/snapshots/success-no-arguments
index 884864b..fa1c9b7 100644
--- a/tests/snapshots/success-no-arguments
+++ b/tests/snapshots/success-no-arguments
@@ -1 +1 @@
-1258947 . \ No newline at end of file
+1.26 MB . \ No newline at end of file