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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::borrow::Cow;
use std::time::Duration;

use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use once_cell::sync::Lazy;

#[derive(Debug)]
pub struct ProgressReport {
    pub pb: Option<ProgressBar>,
    prefix: String,
}

pub static PROG_TEMPLATE: Lazy<ProgressStyle> = Lazy::new(|| {
    ProgressStyle::with_template("{prefix}{wide_msg} {spinner:.blue} {elapsed:3.dim.italic}")
        .unwrap()
});

pub static SUCCESS_TEMPLATE: Lazy<ProgressStyle> = Lazy::new(|| {
    let tmpl = format!(
        "{{prefix}}{{wide_msg}} {} {{elapsed:3.dim.italic}}",
        style("✓").bright().green().for_stderr()
    );
    ProgressStyle::with_template(tmpl.as_str()).unwrap()
});

pub static ERROR_TEMPLATE: Lazy<ProgressStyle> = Lazy::new(|| {
    let tmpl = format!(
        "{{prefix:.red}}{{wide_msg}} {} {{elapsed:3.dim.italic}}",
        style("✗").red().for_stderr()
    );
    ProgressStyle::with_template(tmpl.as_str()).unwrap()
});

impl ProgressReport {
    pub fn new(verbose: bool) -> ProgressReport {
        let pb = match verbose {
            true => None,
            false => Some(ProgressBar::new(0)),
        };
        ProgressReport {
            pb,
            prefix: String::new(),
        }
    }

    pub fn enable_steady_tick(&self) {
        match &self.pb {
            Some(pb) => pb.enable_steady_tick(Duration::from_millis(250)),
            None => (),
        }
    }

    pub fn set_prefix(&mut self, prefix: impl Into<Cow<'static, str>>) {
        match &self.pb {
            Some(pb) => pb.set_prefix(prefix),
            None => {
                self.prefix = prefix.into().to_string();
            }
        }
    }

    pub fn prefix(&self) -> String {
        match &self.pb {
            Some(pb) => pb.prefix(),
            None => self.prefix.clone(),
        }
    }

    pub fn set_style(&self, style: ProgressStyle) {
        match &self.pb {
            Some(pb) => {
                pb.set_style(style);
                pb.set_prefix(console::style("rtx").dim().for_stderr().to_string());
            }
            None => (),
        }
    }
    pub fn set_message<S: AsRef<str>>(&self, message: S) {
        match &self.pb {
            Some(pb) => pb.set_message(message.as_ref().replace('\r', "")),
            None => eprintln!("{}{}", self.prefix, message.as_ref()),
        }
    }
    pub fn println<S: AsRef<str>>(&self, message: S) {
        match &self.pb {
            Some(pb) => pb.println(message),
            None => eprintln!("{}", message.as_ref()),
        }
    }
    pub fn warn<S: AsRef<str>>(&self, message: S) {
        match &self.pb {
            Some(pb) => pb.println(format!("{} {}", style("[WARN]").yellow(), message.as_ref())),
            None => eprintln!("{}{}", self.prefix, message.as_ref()),
        }
    }
    pub fn error(&self) {
        match &self.pb {
            Some(pb) => {
                pb.set_style(ERROR_TEMPLATE.clone());
                pb.finish()
            }
            None => (),
        }
    }
    pub fn finish(&self) {
        match &self.pb {
            Some(pb) => {
                pb.set_style(SUCCESS_TEMPLATE.clone());
                pb.finish()
            }
            None => (),
        }
    }
    pub fn finish_with_message(&self, message: impl Into<Cow<'static, str>>) {
        match &self.pb {
            Some(pb) => {
                pb.set_style(SUCCESS_TEMPLATE.clone());
                pb.finish_with_message(message);
            }
            None => eprintln!("{}{}", self.prefix, message.into()),
        }
    }
    // pub fn clear(&self) {
    //     match &self.pb {
    //         Some(pb) => pb.finish_and_clear(),
    //         None => (),
    //     }
    // }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_progress_report() {
        let mut pr = ProgressReport::new(false);
        pr.set_prefix("prefix");
        assert_eq!(pr.prefix(), "prefix");
        pr.set_message("message");
        pr.finish_with_message("message");
    }

    #[test]
    fn test_progress_report_verbose() {
        let mut pr = ProgressReport::new(true);
        pr.set_prefix("prefix");
        assert_eq!(pr.prefix(), "prefix");
        pr.set_message("message");
        pr.finish_with_message("message");
    }
}