asimov_huggingface/
progress.rs

1// This is free and unencumbered software released into the public domain.
2
3//! Unified minimal progress bar for all downloads.
4
5use hf_hub::api::Progress as HfProgress;
6use indicatif::{ProgressBar as IBar, ProgressStyle};
7
8pub struct Progress {
9    bar: IBar,
10    started: bool,
11}
12
13impl Progress {
14    pub fn new() -> Self {
15        let bar = IBar::new(0);
16        bar.set_style(
17            ProgressStyle::default_bar()
18                .template("{wide_bar} {percent:>3}%")
19                .unwrap()
20                .progress_chars("█░"),
21        );
22        Self {
23            bar,
24            started: false,
25        }
26    }
27}
28
29impl HfProgress for Progress {
30    fn init(&mut self, size: usize, _filename: &str) {
31        if size == 0 {
32            return;
33        }
34        self.bar.set_length(size as u64);
35        self.bar.set_position(0);
36        self.started = true;
37    }
38
39    fn update(&mut self, n: usize) {
40        if !self.started {
41            return;
42        }
43        let pos = self.bar.position().saturating_add(n as u64);
44        self.bar.set_position(pos);
45    }
46
47    fn finish(&mut self) {
48        if !self.started {
49            return;
50        }
51        self.bar.finish_and_clear();
52        self.started = false;
53    }
54}