vault_audit_tools/utils/
progress.rs1use indicatif::{ProgressBar as IndicatifBar, ProgressStyle};
7
8pub struct ProgressBar {
10 bar: IndicatifBar,
11}
12
13impl ProgressBar {
14 pub fn new(total: usize, label: &str) -> Self {
16 let bar = IndicatifBar::new(total as u64);
17 bar.set_style(
18 ProgressStyle::default_bar()
19 .template(
20 "{msg} [{bar:40.cyan/blue}] {percent:>3}% ({pos}/{len}) ({per_sec}) {eta}",
21 )
22 .expect("Invalid progress bar template")
23 .progress_chars("█░"),
24 );
25 bar.set_message(label.to_string());
26
27 Self { bar }
28 }
29
30 #[allow(dead_code)]
32 pub fn new_spinner(label: &str) -> Self {
33 let bar = IndicatifBar::new_spinner();
34 bar.set_style(
35 ProgressStyle::default_spinner()
36 .template("{msg} {spinner} {pos}")
37 .expect("Invalid spinner template"),
38 );
39 bar.set_message(label.to_string());
40
41 Self { bar }
42 }
43
44 pub fn update(&self, current: usize) {
46 self.bar.set_position(current as u64);
47 }
48
49 #[allow(dead_code)]
51 pub fn inc(&self) {
52 self.bar.inc(1);
53 }
54
55 #[allow(dead_code)]
57 pub fn render(&self) {
58 self.bar.tick();
60 }
61
62 pub fn finish(&self) {
64 self.bar.finish();
65 }
66
67 pub fn finish_with_message(&self, message: &str) {
69 self.bar.finish_with_message(message.to_string());
70 }
71
72 pub fn println<S: AsRef<str>>(&self, msg: S) {
74 self.bar.println(msg.as_ref());
75 }
76}