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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::sync::Mutex;
use crate::{style::{Color, Style}, pb::ProgressBar};

lazy_static::lazy_static! {
    pub static ref CURRENT_PROGRESS_BAR: Mutex<Option<ProgressBar>> = Mutex::new(None);
}

pub fn has_progress_bar() -> bool {
    CURRENT_PROGRESS_BAR.lock().unwrap().is_some()
}

pub fn set_progress_bar(progress_bar: ProgressBar) {
    *CURRENT_PROGRESS_BAR.lock().unwrap() = Some(progress_bar);
}

pub fn init_progress_bar(max: usize) {
    let progress_bar = ProgressBar::new(max);
    set_progress_bar(progress_bar);
}

pub fn init_progress_bar_with_eta(max: usize) {
    let progress_bar = ProgressBar::new_with_eta(max);
    set_progress_bar(progress_bar);
}

#[deprecated(note = "Use set_progress_bar_progress instead")]
pub fn set_progress_bar_progression(progress: usize) {
    set_progress_bar_progress(progress);
}

pub fn set_progress_bar_progress(progress: usize) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.set_progress(progress),
        None => eprintln!("ERROR: Unable to set progress bar progress (no progress bar)"),
    }
}

pub fn inc_progress_bar() {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.inc(),
        None => eprintln!("ERROR: Unable to increase progress bar progress (no progress bar)"),
    }
}

pub fn set_progress_bar_width(width: usize) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.set_width(width),
        None => eprintln!("ERROR: Unable to set progress bar width (no progress bar)"),
    }
}

pub fn set_progress_bar_max(max: usize) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.set_max(max),
        None => eprintln!("ERROR: Unable to set progress bar max (no progress bar)"),
    }
}

/// Warning: This resets progress to 0
pub fn enable_eta() {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.enable_eta(),
        None => eprintln!("ERROR: Unable to set progress bar max (no progress bar)"),
    }
}

pub fn disable_eta() {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.disable_eta(),
        None => eprintln!("ERROR: Unable to set progress bar max (no progress bar)"),
    }
}

pub fn print_progress_bar_info(info_name: &str, text: &str, info_color: Color, info_style: Style) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.print_info(info_name, text, info_color, info_style),
        None => eprintln!("ERROR: Unable to print progress bar info (no progress bar)"),
    }
}

pub fn set_progress_bar_action(action: &str, color: Color, style: Style) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.set_action(action, color, style),
        None => eprintln!("ERROR: Unable to set progress bar action (no progress bar)"),
    }
}

pub fn print_progress_bar_final_info(info_name: &str, text: &str, info_color: Color, info_style: Style) {
    match *CURRENT_PROGRESS_BAR.lock().unwrap() {
        Some(ref mut progress_bar) => progress_bar.print_final_info(info_name, text, info_color, info_style),
        None => eprintln!("ERROR: Unable to print progress bar final info (no progress bar)"),
    }
}

pub fn finalize_progress_bar() {
    match CURRENT_PROGRESS_BAR.lock().unwrap().take() {
        Some(mut progress_bar) => progress_bar.finalize(),
        None => eprintln!("ERROR: Unable to finalize progress bar (no progress bar)"),
    }
}