forked from Refefer/cloverleaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.rs
59 lines (48 loc) · 1.38 KB
/
progress.rs
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
use std::sync::Mutex;
use std::time::Duration;
use indicatif::{ProgressBar,ProgressStyle};
pub struct CLProgressBar {
pb: Option<ProgressBar>,
message: Mutex<String>
}
impl CLProgressBar {
pub fn new(work: u64, enabled: bool) -> Self {
let pb = if enabled {
let pb = ProgressBar::new(work as u64);
let style = ProgressStyle::default_bar()
.template("[{msg}] {wide_bar} ({per_sec}) {pos:>7}/{len:7}- Elapsed: {elapsed_precise}, Remaining: {eta_precise}")
.expect("Shouldn't fail!");
pb.set_style(style);
// Update in separate thread
pb.enable_steady_tick(Duration::from_millis(200));
Some(pb)
} else {
None
};
CLProgressBar {
pb,
message: Mutex::new(String::new())
}
}
pub fn update_message<F>(&self, update_message: F)
where
F: Fn(&mut String) -> ()
{
let mut msg = self.message.lock()
.expect("Mutex poisoned!");
update_message(&mut *msg);
if let Some(pb) = &self.pb {
pb.set_message((*msg).clone());
}
}
pub fn inc(&self, amt: u64) {
if let Some(pb) = &self.pb {
pb.inc(amt);
}
}
pub fn finish(&self) {
if let Some(pb) = &self.pb {
pb.finish();
}
}
}