1use crate::cli::*;
2use std::sync::Arc;
3use std::thread::JoinHandle;
4
5const PROGRESS_UPDATE_MILLIS: u64 = 500;
6
7pub struct Tracker {
8 progress: Arc<Progress<DetailCounter>>,
9 handle: Option<JoinHandle<()>>,
10}
11
12impl Tracker {
13 pub fn new(units: &'static str, len: usize) -> Self {
14 let progress = Arc::new(Progress::new(DetailCounter::new(units)));
15 progress.value.total.store(len);
16 let handle = progress
17 .clone()
18 .spawn_auto_update_thread(std::time::Duration::from_millis(PROGRESS_UPDATE_MILLIS));
19 Tracker {
20 progress,
21 handle: Some(handle),
22 }
23 }
24 pub fn increment(&self) {
25 self.progress.value.processed.fetch_add(1);
26 }
27}
28impl Drop for Tracker {
29 fn drop(&mut self) {
30 self.progress.finish();
31 if let Err(_) = self.handle.take().unwrap().join() {
32 eprintln!("Warning: progress thread panicked!");
33 }
34 }
35}