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
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use lazy_static::lazy_static;
lazy_static! {
    
    pub static ref PROGRESS_BAR: MultiProgress = MultiProgress::with_draw_target(ProgressDrawTarget::stdout());
    
    pub static ref PROGRESS_PRINTER: ProgressBar = add_bar("", 0, BarType::Hidden);
}
#[derive(Copy, Clone)]
pub enum BarType {
    
    Hidden,
    
    Default,
    
    Message,
    
    Total,
    
    Quiet,
}
pub fn add_bar(prefix: &str, length: u64, bar_type: BarType) -> ProgressBar {
    let mut style = ProgressStyle::default_bar().progress_chars("#>-");
    style = match bar_type {
        BarType::Hidden => style.template(""),
        BarType::Default => style
            .template("[{bar:.cyan/blue}] - {elapsed:<4} {pos:>7}/{len:7} {per_sec:7} {prefix}"),
        BarType::Message => style.template(&format!(
            "[{{bar:.cyan/blue}}] - {{elapsed:<4}} {{pos:>7}}/{{len:7}} {:7} {{prefix}}",
            "-"
        )),
        BarType::Total => {
            style.template("[{bar:.yellow/blue}] - {elapsed:<4} {pos:>7}/{len:7} {eta:7} {msg}")
        }
        BarType::Quiet => style.template("Scanning: {prefix}"),
    };
    let progress_bar = PROGRESS_BAR.add(ProgressBar::new(length));
    progress_bar.set_style(style);
    progress_bar.set_prefix(&prefix);
    progress_bar
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    
    fn add_bar_with_all_configurations() {
        let p1 = add_bar("prefix", 2, BarType::Hidden); 
        let p2 = add_bar("prefix", 2, BarType::Message); 
        let p3 = add_bar("prefix", 2, BarType::Default); 
        let p4 = add_bar("prefix", 2, BarType::Total); 
        p1.finish();
        p2.finish();
        p3.finish();
        p4.finish();
        assert!(p1.is_finished());
        assert!(p2.is_finished());
        assert!(p3.is_finished());
        assert!(p4.is_finished());
    }
}