-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_bar.rs
More file actions
75 lines (71 loc) · 2.45 KB
/
progress_bar.rs
File metadata and controls
75 lines (71 loc) · 2.45 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
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::time::Duration;
pub fn get_handler() -> MultiProgress {
let p = MultiProgress::new();
#[cfg(test)]
{
use indicatif::ProgressDrawTarget;
p.set_draw_target(ProgressDrawTarget::hidden())
}
p
}
pub fn get_progress_bar(length: impl Into<Option<u64>>) -> ProgressBar {
let length = length.into();
let bar = if let Some(l) = length {
ProgressBar::new(l).with_style(
ProgressStyle::with_template(
"[{elapsed_precise}] {bar:41.cyan/blue} {human_pos:>7}/{human_len:7} {msg}",
)
.unwrap(),
)
} else {
let tick_string = format!("{pattern}.", pattern = ". ".repeat(20));
let indicator = std::char::from_u32(0x0001F9EC).unwrap();
let mut tick_strings = vec![];
for i in (0..tick_string.len()).step_by(2) {
if i == tick_string.len() - 1 {
tick_strings.push(format!(
"{left}{indicator}",
left = &tick_string[..tick_string.len() - 1]
));
} else {
tick_strings.push(format!(
"{left}{indicator} {right}",
left = &tick_string[..(i + 2)],
right = &tick_string[(i + 2)..]
));
}
}
ProgressBar::no_length().with_style(
ProgressStyle::with_template(
"[{elapsed_precise}] {spinner:40.cyan/blue} {human_pos:>7}{'':8} {msg}",
)
.unwrap()
.tick_strings(
&tick_strings
.iter()
.map(|f| f.as_str())
.collect::<Vec<&str>>(),
),
)
};
bar.enable_steady_tick(Duration::from_millis(250));
bar
}
pub fn get_time_elapsed_bar() -> ProgressBar {
let bar = ProgressBar::no_length().with_style(
ProgressStyle::with_template(
"[{elapsed_precise}] {'':19}{spinner:2.cyan/blue}{'':37} {msg}",
)
.unwrap(),
);
bar.enable_steady_tick(Duration::from_millis(250));
bar
}
pub fn add_saving_operation_bar(progress_bar: &MultiProgress) -> ProgressBar {
// we have this pattern here because calling set_message before a bar is added to the multibar
// causes duplicate lines to appear
let bar = progress_bar.add(get_time_elapsed_bar());
bar.set_message("Saving operation");
bar
}