1use std::{borrow::Cow, ops::Deref, time::Duration};
2
3use coarsetime::Clock;
4pub use indicatif::*;
5
6#[derive(Debug)]
7pub struct Pbar {
8 pub bar: ProgressBar,
9 pub pre: u64,
10}
11
12impl Pbar {
13 pub fn set_message(&mut self, msg: impl Into<Cow<'static, str>>) {
14 let sec = Clock::now_since_epoch().as_secs();
15 if sec - self.pre >= 1 {
16 self.pre = sec;
17 self.bar.set_message(msg);
18 }
19 }
20}
21
22impl Deref for Pbar {
23 type Target = ProgressBar;
24
25 fn deref(&self) -> &Self::Target {
26 &self.bar
27 }
28}
29
30pub fn pbar(total: u64) -> Pbar {
31 let pb = pbar_no_run(total);
32 pb.enable_steady_tick(Duration::from_millis(200));
33 pb
34}
35
36pub fn pbar_no_run(total: u64) -> Pbar {
37 let pb = ProgressBar::new(total);
38 pb.set_style(
39 ProgressStyle::default_bar()
40 .template(
41 "{msg} {wide_bar:.green/gray} {percent}% {spinner:.yellow} {elapsed_precise} ETA {eta}",
42 )
43 .unwrap()
44 .progress_chars("=>─"),
45 );
46 Pbar { bar: pb, pre: 0 }
47}