Skip to main content

git_simple_encrypt/utils/
progress.rs

1//! Progress reporting abstraction.
2//!
3//! When the `progress` feature is enabled, [`Progress`] wraps an
4//! `indicatif::ProgressBar`. When disabled, it is a zero-sized type and
5//! progress is reported only via `log::info!` at start/finish. Either way
6//! the call-site API is identical, so `crypt::encrypt_repo` and friends do
7//! not need any feature gates.
8
9#[cfg(feature = "progress")]
10use indicatif::{ProgressBar, ProgressStyle};
11
12/// Monomorphic progress handle used by repo-wide operations.
13///
14/// `Send + Sync` so it can be shared (by reference) across rayon workers.
15pub struct Progress {
16    #[cfg(feature = "progress")]
17    inner: ProgressBar,
18}
19
20impl Progress {
21    /// Create a new progress handle for `len` items with a short label.
22    #[must_use]
23    pub fn new(len: usize, prefix: &'static str) -> Self {
24        // Always log the start line so non-progress lib users still see output.
25        log::info!("{prefix}: {len} files");
26        Self {
27            #[cfg(feature = "progress")]
28            inner: make_indicatif(len, prefix),
29        }
30    }
31
32    /// Advance the progress by `n` finished items.
33    #[inline]
34    pub fn inc(&self, n: u64) {
35        #[cfg(feature = "progress")]
36        self.inner.inc(n);
37        // No-op without the feature; per-item logging would be too noisy.
38        #[cfg(not(feature = "progress"))]
39        let _ = n;
40    }
41
42    /// Finalize and clear the progress bar (if any).
43    #[inline]
44    pub fn finish_and_clear(&self) {
45        #[cfg(feature = "progress")]
46        self.inner.finish_and_clear();
47    }
48}
49
50#[cfg(feature = "progress")]
51fn make_indicatif(len: usize, prefix: &'static str) -> ProgressBar {
52    let pb = ProgressBar::new(len as u64);
53    pb.set_style(
54        ProgressStyle::with_template(
55            "{prefix:.bold} {bar:40.cyan/blue} {pos}/{len} {spinner} [{elapsed_precise}]",
56        )
57        .expect("valid indicatif template")
58        .progress_chars("#>-"),
59    );
60    pb.set_prefix(prefix);
61    pb.enable_steady_tick(std::time::Duration::from_millis(200));
62    pb
63}