post_archiver_utils/
progress.rs1use std::{cell::RefCell, collections::HashMap};
2
3use indicatif::{MultiProgress, ProgressBar, ProgressStyle, WeakProgressBar};
4
5pub struct ProgressSet {
6 pub multi: MultiProgress,
7 pub map: RefCell<HashMap<&'static str, WeakProgressBar>>,
8 pub disabled: &'static[&'static str],
9}
10
11impl ProgressSet {
12 pub fn new(multi: MultiProgress) -> Self {
13 Self {
14 multi: multi,
15 map: Default::default(),
16 disabled: &[],
17 }
18 }
19
20 pub fn disabled(mut self, prefixes: &'static[&'static str]) -> Self {
21 self.disabled = prefixes;
22 self
23 }
24
25 pub fn add(&self, prefix: &'static str) -> ProgressBar {
26 if self.disabled.contains(&prefix) {
27 return ProgressBar::hidden();
28 }
29
30 let mut map = self.map.borrow_mut();
31
32 if let Some(weak) = map.get(prefix)
33 && let Some(bar) = weak.upgrade()
34 {
35 return bar;
36 }
37
38 let pb = self.multi.add(
39 ProgressBar::new(0)
40 .with_style(Self::style())
41 .with_prefix(format!("[{prefix}]")),
42 );
43
44 map.insert(prefix, pb.downgrade());
45 pb
46 }
47
48 fn style() -> ProgressStyle {
49 ProgressStyle::with_template("{prefix:.bold.dim} {wide_bar:.cyan/blue} {pos:>3}/{len:3}")
50 .unwrap()
51 .progress_chars("#>-")
52 }
53}