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
use crate::is_stdout_tty;
use indicatif::{ProgressBar, ProgressStyle};
use std::time::Duration;
pub struct Progress {
bar: Option<ProgressBar>,
}
impl Progress {
pub fn start(msg: impl ToString) -> Progress {
let bar = if is_stdout_tty() {
Some(show_progress(msg))
} else {
println!("▹▹▹▹▹ {}", msg.to_string());
None
};
Progress { bar }
}
pub fn finish(&self, msg: &str) {
if let Some(bar) = &self.bar {
bar.finish_with_message(msg.to_string());
} else {
println!("▪▪▪▪▪ {msg}");
}
}
pub fn set_message(&self, msg: &str) {
if let Some(bar) = &self.bar {
bar.set_message(msg.to_string());
} else {
println!("▹▹▹▹▹ {msg}");
}
}
pub fn finish_and_clear(&self) {
if let Some(bar) = &self.bar {
bar.finish_and_clear();
}
}
}
fn show_progress(msg: impl ToString) -> ProgressBar {
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(Duration::from_millis(120));
pb.set_style(ProgressStyle::default_spinner().tick_strings(&[
"▹▹▹▹▹",
"▸▹▹▹▹",
"▹▸▹▹▹",
"▹▹▸▹▹",
"▹▹▹▸▹",
"▹▹▹▹▸",
"▪▪▪▪▪",
]));
pb.set_message(msg.to_string());
pb
}