Skip to content

Commit

Permalink
Add support for static byte units
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 4, 2019
1 parent 9d430a2 commit a1ecbf0
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ A sub-command bringing up a terminal user interface to allow drilling into direc

##### Other Features

* [ ] Single Unit Mode, see [reddit](https://www.reddit.com/r/rust/comments/bvjtan/introducing_dua_a_parallel_du_for_humans/epsroxg/)
* [ ] Evaluate unit coloring
* [x] Single Unit Mode, see [reddit](https://www.reddit.com/r/rust/comments/bvjtan/introducing_dua_a_parallel_du_for_humans/epsroxg/)
* [ ] Evaluate unit coloring - can we highlight different units better, make them stick out?

#### ✅v1.0 (_released_) - aggregate directories, fast

Expand Down
32 changes: 26 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -67,6 +76,8 @@ impl ByteFormat {
match self {
Metric | Binary => 10,
Bytes => 12,
MiB|MB => 12,
_ => 10,
}
}
pub fn display(&self, bytes: u64) -> ByteFormatDisplay {
Expand All @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion src/interactive/widgets/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Widget for Header {
text(env!("CARGO_PKG_VERSION")),
text(" "),
italic("(press "),
modified("?", Modifier::BOLD|Modifier::UNDERLINED),
modified("?", Modifier::BOLD | Modifier::UNDERLINED),
italic(" for help)"),
];
Paragraph::new(lines.iter())
Expand Down
14 changes: 13 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ arg_enum! {
pub enum ByteFormat {
Metric,
Binary,
Bytes
Bytes,
GB,
GiB,
MB,
MiB
}
}

Expand All @@ -17,6 +21,10 @@ impl From<ByteFormat> for LibraryByteFormat {
ByteFormat::Metric => LibraryByteFormat::Metric,
ByteFormat::Binary => LibraryByteFormat::Binary,
ByteFormat::Bytes => LibraryByteFormat::Bytes,
ByteFormat::GB => LibraryByteFormat::GB,
ByteFormat::GiB => LibraryByteFormat::GiB,
ByteFormat::MB => LibraryByteFormat::MB,
ByteFormat::MiB => LibraryByteFormat::MiB,
}
}
}
Expand All @@ -37,6 +45,10 @@ pub struct Args {
/// Metric - uses 1000 as base (default)
/// Binary - uses 1024 as base
/// Bytes - plain bytes without any formatting
/// GB - only gigabytes
/// GiB - only gibibytes
/// MB - only megabytes
/// MiB - only mebibytes
#[structopt(short = "f", long = "format")]
pub format: Option<ByteFormat>,

Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/snapshots/success-bytes-gb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0.00 GB b.empty
0.00 GB z123.b
0.00 GB a
0.00 GB c.lnk
0.00 GB .hidden.666
0.00 GB dir
0.00 GB total
7 changes: 7 additions & 0 deletions tests/snapshots/success-bytes-gib
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0.00 GiB b.empty
0.00 GiB z123.b
0.00 GiB a
0.00 GiB c.lnk
0.00 GiB .hidden.666
0.00 GiB dir
0.00 GiB total
7 changes: 7 additions & 0 deletions tests/snapshots/success-bytes-mb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0.00 MB b.empty
0.00 MB z123.b
0.00 MB a
0.00 MB c.lnk
0.00 MB .hidden.666
1.26 MB dir
1.26 MB total
7 changes: 7 additions & 0 deletions tests/snapshots/success-bytes-metric
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0.00 B b.empty
123.00 B z123.b
256.00 B a
256.00 B c.lnk
666.00 B .hidden.666
1.26 MB dir
1.26 MB total
7 changes: 7 additions & 0 deletions tests/snapshots/success-bytes-mib
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0.00 MiB b.empty
0.00 MiB z123.b
0.00 MiB a
0.00 MiB c.lnk
0.00 MiB .hidden.666
1.20 MiB dir
1.20 MiB total
20 changes: 8 additions & 12 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,14 @@ WITH_FAILURE=1
)

(with "the byte format set"
(with "human-binary"
it "produces a human-readable aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-bytes-binary" \
expect_run ${SUCCESSFULLY} "$exe" --format binary
}
)
(with "bytes"
it "produces a human-readable aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-bytes-only" \
expect_run ${SUCCESSFULLY} "$exe" --format bytes
}
)
for format in binary bytes metric gb gib mb mib; do
(with $format
it "produces a human-readable aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-bytes-$format" \
expect_run ${SUCCESSFULLY} "$exe" --format $format
}
)
done
)
)
(with "interactive mode"
Expand Down

0 comments on commit a1ecbf0

Please sign in to comment.