Skip to content

Commit

Permalink
integrate humantime
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 8, 2020
1 parent afb0c91 commit f0f55bb
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ required-features = ["tui-renderer", "tui-renderer-termion", "line-renderer", "l
[features]
default = ["log-renderer", "localtime"]
unit-bytes = ["bytesize"]
unit-human = ["human_format"]
tui-renderer-termion = ["crosstermion/tui-react-termion"]
tui-renderer-crossterm = ["crosstermion/tui-react-crossterm", "crosstermion/input-async-crossterm"]
tui-renderer = ["tui",
Expand Down Expand Up @@ -72,6 +73,7 @@ ctrlc = { version = "3.1.4", optional = true, default-features = false, features

# units
bytesize = { version = "1.0.1", optional = true }
human_format = { version = "1.0.3", optional = true }

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ check: ## build features in commmon combination to be sure it all stays together
$(MAKE) -C crosstermion check

unit-test: ## Run all unit tests
cargo test --examples --features unit-bytes
cargo test --examples --features unit-bytes,unit-human

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

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ This crate comes with various cargo features to tailor it to your needs.
* It has less dependencies but works only on `unix` systems
* to get this, disable default features and chose at least `tui-renderer` and `tui-renderer-termion`.
* **unit-bytes**
* Supports dynamic unit rendering using the tiny `bytesize` crate.
* Supports dynamic byte display using the tiny `bytesize` crate.
* **unit-human**
* Display counts in a way that is easier to grasp for humans, using the tiny `human_format` crate.

## Features

Expand Down
2 changes: 1 addition & 1 deletion src/unit/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DisplayValue for Bytes {
) -> fmt::Result {
Self::format_bytes(w, upper_bound)
}
fn display_unit(&self, _w: &mut dyn fmt::Write, _value: usize) -> fmt::Result {
fn display_unit(&self, _w: &mut dyn fmt::Write, _value: ProgressStep) -> fmt::Result {
Ok(())
}
}
47 changes: 47 additions & 0 deletions src/unit/human.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::tree::ProgressStep;
use crate::unit::DisplayValue;
pub use human_format::{Formatter, Scales};
use std::fmt;

#[derive(Debug)]
pub struct Human {
pub name: &'static str,
pub formatter: Formatter,
}

impl Human {
pub fn new(formatter: Formatter, name: &'static str) -> Self {
Human { formatter, name }
}
fn format_bytes(&self, w: &mut dyn fmt::Write, value: ProgressStep) -> fmt::Result {
let string = self.formatter.format(value as f64);
for token in string.split(' ') {
w.write_str(token)?;
}
Ok(())
}
}

impl DisplayValue for Human {
fn display_current_value(
&self,
w: &mut dyn fmt::Write,
value: ProgressStep,
_upper: Option<ProgressStep>,
) -> fmt::Result {
self.format_bytes(w, value)
}

fn display_upper_bound(
&self,
w: &mut dyn fmt::Write,
upper_bound: ProgressStep,
_value: ProgressStep,
) -> fmt::Result {
self.format_bytes(w, upper_bound)
}

fn display_unit(&self, w: &mut dyn fmt::Write, _value: ProgressStep) -> fmt::Result {
w.write_str(self.name)
}
}
9 changes: 8 additions & 1 deletion src/unit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use std::{fmt, fmt::Write, ops::Deref};
mod bytes;
#[cfg(feature = "unit-bytes")]
pub use bytes::Bytes;

#[cfg(feature = "unit-human")]
pub mod human;
#[cfg(feature = "unit-human")]
#[doc(inline)]
pub use human::Human;

mod range;
pub use range::Range;

Expand Down Expand Up @@ -131,7 +138,7 @@ impl<'a> fmt::Display for UnitDisplay<'a> {

let mode_and_fraction = mode.and_then(|mode| {
self.upper_bound
.map(|upper| (mode, (self.current_value as f64 / upper as f64) * 100.0))
.map(|upper| (mode, ((self.current_value as f64 / upper as f64) * 100.0).floor()))
});
if self.display.values() {
if let Some((Mode::PercentageBeforeValue, fraction)) = mode_and_fraction {
Expand Down
2 changes: 1 addition & 1 deletion src/unit/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DisplayValue for Range {
fn separator(&self, w: &mut dyn fmt::Write, _value: ProgressStep, _upper: Option<ProgressStep>) -> fmt::Result {
w.write_str(" of ")
}
fn display_unit(&self, w: &mut dyn fmt::Write, _value: usize) -> fmt::Result {
fn display_unit(&self, w: &mut dyn fmt::Write, _value: ProgressStep) -> fmt::Result {
w.write_str(self.name)
}
}
24 changes: 24 additions & 0 deletions src/unit/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
mod dynamic {
#[cfg(feature = "unit-human")]
mod human {
use crate::unit::{human, Human, Mode, Unit};

#[test]
fn various_combinations() {
let unit = Unit::dynamic_and_mode(
Human::new(
{
let mut f = human::Formatter::new();
f.with_decimals(1);
f
},
"objects",
),
Mode::PercentageAfterUnit,
);
assert_eq!(
format!("{}", unit.display(100_002, Some(7_500_000))),
"100.0k/7.5M objects [1%]"
);
assert_eq!(format!("{}", unit.display(100_002, None)), "100.0k objects");
}
}
mod range {
use crate::unit::{Mode, Range, Unit};
#[test]
Expand Down

0 comments on commit f0f55bb

Please sign in to comment.