Skip to content

Commit

Permalink
Add byte formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 1, 2019
1 parent a19e3d7 commit 6db07e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/success-no-arguments
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1258947 .
1.26 MB .

0 comments on commit 6db07e2

Please sign in to comment.