snm_core/
loader.rs

1// https://mattgathu.github.io/2017/08/29/writing-cli-app-rust.html
2use indicatif::{ProgressBar, ProgressBarIter, ProgressStyle};
3use std::io::Read;
4
5const TMPL: &str = "{spinner} [{elapsed_precise}] [{wide_bar}] {bytes}/{total_bytes} ({eta})";
6
7pub struct Spinner {
8    s: ProgressBar,
9}
10
11impl Spinner {
12    pub fn new(msg: &'static str) -> Self {
13        let s = ProgressBar::new_spinner();
14
15        s.enable_steady_tick(100);
16        s.set_message(msg);
17
18        Self { s }
19    }
20
21    pub fn finish(&self) {
22        self.s.finish_and_clear();
23    }
24}
25
26pub struct Bar {
27    bar: ProgressBar,
28}
29
30impl Bar {
31    pub fn new(len: u64) -> Self {
32        let bar = ProgressBar::new(len);
33
34        bar.set_style(
35            ProgressStyle::default_bar()
36                .template(TMPL)
37                .progress_chars("#>-"),
38        );
39
40        Self { bar }
41    }
42
43    pub fn take_reader<R: Read>(&self, r: R) -> ProgressBarIter<R> {
44        self.bar.wrap_read(r)
45    }
46
47    pub fn finish(&self) {
48        self.bar.finish_and_clear()
49    }
50}