Skip to main content

cherenkov/download/
progress.rs

1//! Keep long transfers visible without flooding stderr with per-chunk updates.
2
3use crate::units::BYTES_PER_GB;
4use hf_hub::progress::{DownloadEvent, ProgressEvent, ProgressHandler};
5use std::{
6    sync::Mutex,
7    time::{Duration, Instant},
8};
9
10#[derive(Default)]
11pub(super) struct Reporter {
12    last: Mutex<Option<Instant>>,
13}
14
15impl ProgressHandler for Reporter {
16    fn on_progress(&self, event: &ProgressEvent) {
17        let ProgressEvent::Download(event) = event else {
18            return;
19        };
20
21        match event {
22            DownloadEvent::AggregateProgress {
23                bytes_completed,
24                total_bytes,
25                ..
26            } => {
27                let mut last = self.last.lock().unwrap();
28
29                if last.is_none_or(|t| t.elapsed() >= Duration::from_secs(2)) {
30                    eprintln!(
31                        "download {:.2}/{:.2} GB",
32                        *bytes_completed as f64 / BYTES_PER_GB as f64,
33                        *total_bytes as f64 / BYTES_PER_GB as f64
34                    );
35
36                    *last = Some(Instant::now());
37                }
38            }
39            DownloadEvent::Complete => eprintln!("download complete"),
40            _ => {}
41        }
42    }
43}