forked from GitoxideLabs/prodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunits.rs
More file actions
121 lines (107 loc) · 3.66 KB
/
units.rs
File metadata and controls
121 lines (107 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![deny(unsafe_code)]
#[cfg(not(feature = "render-tui"))]
compile_error!(
"The `render-tui` feature must be set, along with either `render-tui-crossterm` or `render-tui-termion`"
);
#[cfg(not(any(feature = "render-tui-crossterm", feature = "render-tui-termion")))]
compile_error!(
"Please set either the 'render-tui-crossterm' or 'render-tui-termion' feature whne using the 'render-tui'"
);
fn main() -> Result {
env_logger::init();
let args: args::Options = argh::from_env();
let root = Tree::default();
let renderer = args.renderer.clone().unwrap_or_else(|| "line".into());
let handle = shared::launch_ambient_gui(root.clone(), &renderer, args, true).unwrap();
let work = async move {
let mut unblock = blocking::Unblock::new(());
unblock.with_mut(move |_| work_for_a_long_time_blocking(root)).await
};
futures_lite::future::block_on(futures_util::future::select(handle, work.boxed()));
Ok(())
}
fn work_for_a_long_time_blocking(root: Tree) {
let mut bytes = root.add_child("download unknown");
bytes.init(
None,
Some(unit::dynamic_and_mode(
unit::Bytes,
unit::display::Mode::with_throughput(),
)),
);
let mut bytes_max = root.add_child("download");
bytes_max.init(
Some(100_000_000),
Some(unit::dynamic_and_mode(
unit::Bytes,
unit::display::Mode::with_percentage().and_throughput(),
)),
);
let mut duration = root.add_child("duration unknown");
duration.init(None, Some(unit::dynamic(unit::Duration)));
let mut duration_max = root.add_child("duration");
duration_max.init(
Some(60 * 60 * 24),
Some(unit::dynamic_and_mode(
unit::Duration,
unit::display::Mode::with_percentage().show_before_value(),
)),
);
fn formatter(decimals: usize) -> unit::human::Formatter {
let mut f = unit::human::Formatter::new();
f.with_decimals(decimals);
f
}
let mut human_count = root.add_child("item count unknown");
human_count.init(
None,
Some(unit::dynamic_and_mode(
unit::Human::new(formatter(0), "items"),
unit::display::Mode::with_throughput(),
)),
);
let mut human_count_max = root.add_child("item count");
human_count_max.init(
Some(7_542_241),
Some(unit::dynamic_and_mode(
unit::Human::new(formatter(2), "items"),
unit::display::Mode::with_percentage().and_throughput(),
)),
);
let mut steps = root.add_child("steps to take unknown");
steps.init(
None,
Some(unit::dynamic_and_mode(
unit::Range::new("steps"),
unit::display::Mode::with_throughput(),
)),
);
let mut steps_max = root.add_child("steps to take");
steps_max.init(
Some(100),
Some(unit::dynamic_and_mode(
unit::Range::new("steps"),
unit::display::Mode::with_percentage().and_throughput(),
)),
);
let steps_per_second = 10;
for step in 0.. {
bytes_max.inc_by(1_459_121);
bytes.inc_by(23_212_159);
duration.inc();
duration_max.inc_by(60);
human_count.inc_by(4);
human_count_max.inc_by(40274 / steps_per_second);
if step % steps_per_second == 0 {
steps.inc();
steps_max.inc();
}
std::thread::sleep(std::time::Duration::from_millis((1000 / steps_per_second) as u64));
}
}
type Result = std::result::Result<(), Box<dyn Error + Send + 'static>>;
mod shared;
use shared::args;
use futures_lite::FutureExt;
use prodash::{unit, Tree};
use std::error::Error;