1 use std
::{fmt, fmt::Debug, hash::Hasher}
;
3 pub use human_format
::{Formatter, Scales}
;
5 use crate::{progress::Step, unit::DisplayValue}
;
7 /// A helper for formatting numbers in a format easily read by humans in renderers, as in `2.54 million objects`
10 /// The name of the represented unit, like 'items' or 'objects'.
11 pub name
: &'
static str,
12 /// The formatter to format the actual numbers.
13 pub formatter
: Formatter
,
17 /// A convenience method to create a new new instance and its `formatter` and `name` fields.
18 pub fn new(formatter
: Formatter
, name
: &'
static str) -> Self {
19 Human { name, formatter }
21 fn format_bytes(&self, w
: &mut dyn fmt
::Write
, value
: Step
) -> fmt
::Result
{
22 let string
= self.formatter
.format(value
as f64);
23 for token
in string
.split(' '
) {
30 impl DisplayValue
for Human
{
31 fn display_current_value(&self, w
: &mut dyn fmt
::Write
, value
: Step
, _upper
: Option
<Step
>) -> fmt
::Result
{
32 self.format_bytes(w
, value
)
35 fn display_upper_bound(&self, w
: &mut dyn fmt
::Write
, upper_bound
: Step
, _value
: Step
) -> fmt
::Result
{
36 self.format_bytes(w
, upper_bound
)
39 fn dyn_hash(&self, state
: &mut dyn Hasher
) {
40 state
.write(self.name
.as_bytes());
44 fn display_unit(&self, w
: &mut dyn fmt
::Write
, _value
: Step
) -> fmt
::Result
{
45 w
.write_str(self.name
)