Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f233d43

Browse files
committed
trying to use byte-size shows that the trait interface isn't flexible enough
1 parent 0e15b97 commit f233d43

7 files changed

Lines changed: 120 additions & 43 deletions

File tree

Cargo.toml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ required-features = ["tui-renderer", "tui-renderer-termion", "line-renderer", "l
2424

2525
[features]
2626
default = ["log-renderer", "localtime"]
27+
unit-bytes = ["bytesize"]
2728
tui-renderer-termion = ["crosstermion/tui-react-termion"]
2829
tui-renderer-crossterm = ["crosstermion/tui-react-crossterm", "crosstermion/input-async-crossterm"]
2930
tui-renderer = ["tui",
30-
"unicode-segmentation",
31-
"unicode-width",
32-
"crosstermion/input-async",
33-
"tui-react",
34-
"futures-util",
35-
"futures-lite",
36-
"futures-core",
37-
"async-io",
38-
"humantime"]
31+
"unicode-segmentation",
32+
"unicode-width",
33+
"crosstermion/input-async",
34+
"tui-react",
35+
"futures-util",
36+
"futures-lite",
37+
"futures-core",
38+
"async-io",
39+
"humantime"]
3940
line-renderer = ["crosstermion/color", "humantime", "unicode-width"]
4041
line-renderer-crossterm = ["crosstermion/crossterm"]
4142
line-renderer-termion = ["crosstermion/termion"]
@@ -69,6 +70,9 @@ time = { version = "0.2.9", optional = true, features = ["std"], default-feature
6970
# line renderer
7071
ctrlc = { version = "3.1.4", optional = true, default-features = false, features = ['termination'] }
7172

73+
# units
74+
bytesize = { version = "1.0.1", optional = true }
75+
7276
[package.metadata.docs.rs]
7377
all-features = true
7478

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ check: ## build features in commmon combination to be sure it all stays together
1818
$(MAKE) -C crosstermion check
1919

2020
unit-test: ## Run all unit tests
21-
cargo test --examples
21+
cargo test --examples --features unit-bytes
2222

2323
tests: check unit-test ## Run all tests we have
2424

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ This crate comes with various cargo features to tailor it to your needs.
5656
* Use the `termion` crate as terminal backend
5757
* It has less dependencies but works only on `unix` systems
5858
* to get this, disable default features and chose at least `tui-renderer` and `tui-renderer-termion`.
59+
* **unit-bytes**
60+
* Supports dynamic unit rendering using the tiny `bytesize` crate.
5961

6062
## Features
6163

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use log::warn;
5252

5353
#[cfg(any(feature = "humantime", feature = "time"))]
5454
pub mod time;
55-
mod unit;
55+
pub mod unit;
5656

5757
#[cfg(not(feature = "log-renderer"))]
5858
mod log {

src/unit/bytes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::{tree::ProgressStep, unit::DisplayValue};
2+
use std::fmt;
3+
4+
#[derive(Copy, Clone, Default, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)]
5+
pub struct Bytes;
6+
7+
impl Bytes {
8+
fn format_bytes(f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result {
9+
let string = bytesize::to_string(value as u64, false);
10+
for token in string.split(' ') {
11+
write!(f, "{}", token)?;
12+
}
13+
Ok(())
14+
}
15+
}
16+
17+
impl DisplayValue for Bytes {
18+
fn display_current_value(
19+
&self,
20+
f: &mut fmt::Formatter,
21+
value: ProgressStep,
22+
_upper: Option<ProgressStep>,
23+
) -> fmt::Result {
24+
Self::format_bytes(f, value)
25+
}
26+
fn display_upper_bound(
27+
&self,
28+
f: &mut fmt::Formatter,
29+
upper_bound: ProgressStep,
30+
_value: ProgressStep,
31+
) -> fmt::Result {
32+
Self::format_bytes(f, upper_bound)
33+
}
34+
fn display_unit(&self, _f: &mut fmt::Formatter, _value: usize) -> fmt::Result {
35+
Ok(())
36+
}
37+
}

src/unit/mod.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
use crate::tree::ProgressStep;
2-
use std::fmt::Write;
3-
use std::{fmt, ops::Deref};
2+
use std::{fmt, fmt::Write, ops::Deref};
3+
4+
#[cfg(feature = "unit-bytes")]
5+
mod bytes;
6+
#[cfg(feature = "unit-bytes")]
7+
pub use bytes::Bytes;
48

59
pub trait DisplayValue {
610
fn display_current_value(
711
&self,
812
f: &mut fmt::Formatter,
913
value: ProgressStep,
10-
_max: Option<ProgressStep>,
14+
_upper: Option<ProgressStep>,
1115
) -> fmt::Result {
1216
write!(f, "{}", value)
1317
}
14-
fn display_upper_bound(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result {
15-
write!(f, "{}", value)
18+
fn display_upper_bound(
19+
&self,
20+
f: &mut fmt::Formatter,
21+
upper_bound: ProgressStep,
22+
_value: ProgressStep,
23+
) -> fmt::Result {
24+
write!(f, "{}", upper_bound)
1625
}
1726
fn display_unit(&self, f: &mut fmt::Formatter, value: ProgressStep) -> fmt::Result;
1827
fn display_percentage(&self, f: &mut fmt::Formatter, percentage: f64) -> fmt::Result {
@@ -39,6 +48,12 @@ impl Unit {
3948
pub fn label_and_mode(label: &'static str, mode: Mode) -> Self {
4049
Unit::Label(label, Some(mode))
4150
}
51+
pub fn dynamic(label: impl DisplayValue + 'static) -> Self {
52+
Unit::Dynamic(Box::new(label), None)
53+
}
54+
pub fn dynamic_and_mode(label: impl DisplayValue + 'static, mode: Mode) -> Self {
55+
Unit::Dynamic(Box::new(label), Some(mode))
56+
}
4257
}
4358

4459
/// Display and utilities
@@ -121,13 +136,13 @@ impl<'a> fmt::Display for UnitDisplay<'a> {
121136
unit.display_current_value(f, self.current_value, self.upper_bound)?;
122137
if let Some(upper) = self.upper_bound {
123138
f.write_char('/')?;
124-
unit.display_upper_bound(f, upper)?;
125-
}
126-
if self.display.unit() {
127-
f.write_char(' ')?;
139+
unit.display_upper_bound(f, upper, self.current_value)?;
128140
}
129141
}
130142
if self.display.unit() {
143+
if self.display.values() {
144+
f.write_char(' ')?;
145+
}
131146
unit.display_unit(f, self.current_value)?;
132147

133148
if let Some((Mode::PercentageAfterUnit, fraction)) = mode_and_fraction {

src/unit/tests.rs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1-
mod label {
2-
mod values {
3-
use crate::unit::{Mode, Unit};
4-
#[test]
5-
fn display_current_value_with_upper_bound_percentage_before_value() {
6-
assert_eq!(
7-
format!(
8-
"{}",
9-
Unit::label_and_mode("items", Mode::PercentageBeforeValue)
10-
.display(123, Some(400))
11-
.values()
12-
),
13-
"[30%] 123/400"
14-
);
15-
}
16-
}
17-
mod unit {
18-
use crate::unit::{Mode, Unit};
1+
mod dynamic {
2+
#[cfg(feature = "unit-bytes")]
3+
mod bytes {
4+
use crate::unit::{Bytes, Mode, Unit};
5+
196
#[test]
20-
fn display_current_value_with_upper_bound_percentage_after_unit() {
7+
fn value_and_upper_bound_use_own_unit() {
218
assert_eq!(
229
format!(
2310
"{}",
24-
Unit::label_and_mode("items", Mode::PercentageAfterUnit)
25-
.display(123, Some(400))
26-
.unit()
11+
Unit::dynamic_and_mode(Bytes, Mode::PercentageAfterUnit).display(1002, Some(10_000_000_000))
2712
),
28-
"items [30%]"
13+
"1.0KB/10.0GB [0%]"
2914
);
3015
}
3116
}
17+
}
18+
19+
mod label {
3220
mod with_percentage {
21+
mod only_values {
22+
use crate::unit::{Mode, Unit};
23+
#[test]
24+
fn display_current_value_with_upper_bound_percentage_before_value() {
25+
assert_eq!(
26+
format!(
27+
"{}",
28+
Unit::label_and_mode("items", Mode::PercentageBeforeValue)
29+
.display(123, Some(400))
30+
.values()
31+
),
32+
"[30%] 123/400"
33+
);
34+
}
35+
}
36+
37+
mod only_unit {
38+
use crate::unit::{Mode, Unit};
39+
#[test]
40+
fn display_current_value_with_upper_bound_percentage_after_unit() {
41+
assert_eq!(
42+
format!(
43+
"{}",
44+
Unit::label_and_mode("items", Mode::PercentageAfterUnit)
45+
.display(123, Some(400))
46+
.unit()
47+
),
48+
"items [30%]"
49+
);
50+
}
51+
}
3352
use crate::unit::{Mode, Unit};
3453

3554
#[test]

0 commit comments

Comments
 (0)