Skip to main content

geolith/
progress.rs

1//! Progress bar helpers using [`indicatif`].
2//!
3//! Provides three levels of progress feedback:
4//! - [`feature_progress`] — determinate bar with position/total and ETA
5//! - `spinner` — simple indeterminate spinner with a message
6//! - [`phase_spinner`] — spinner with elapsed time and automatic tick animation
7
8use std::time::Duration;
9
10use indicatif::{ProgressBar, ProgressStyle};
11
12/// Create a progress bar for feature processing.
13///
14/// Shows a determinate bar with position/total and estimated time remaining.
15/// Use for phases where the total count is known upfront (e.g., file counts).
16#[must_use]
17pub fn feature_progress(total: u64, prefix: &str) -> ProgressBar {
18    let pb = ProgressBar::new(total);
19    pb.set_style(
20        ProgressStyle::default_bar()
21            .template("{prefix} [{bar:40.cyan/blue}] {pos}/{len} ({eta})")
22            .expect("valid progress template")
23            .progress_chars("=>-"),
24    );
25    pb.set_prefix(prefix.to_string());
26    pb
27}
28
29/// Create a spinner with elapsed time for long-running pipeline phases.
30///
31/// Includes automatic tick animation (200 ms interval) so the spinner
32/// stays animated even when the processing thread is busy. Call
33/// [`ProgressBar::set_message`] to update the status text as work
34/// progresses, and [`ProgressBar::finish_with_message`] when complete.
35#[must_use]
36pub fn phase_spinner(prefix: &str) -> ProgressBar {
37    let pb = ProgressBar::new_spinner();
38    pb.set_style(
39        ProgressStyle::default_spinner()
40            .template("{spinner:.green} {prefix}: {msg} [{elapsed_precise}]")
41            .expect("valid progress template"),
42    );
43    pb.set_prefix(prefix.to_string());
44    pb.enable_steady_tick(Duration::from_millis(200));
45    pb
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn feature_progress_initialises_with_total_and_prefix() {
54        let pb = feature_progress(100, "phase-1");
55        assert_eq!(pb.length(), Some(100));
56        assert_eq!(pb.position(), 0);
57        // Bar accepts manual position updates without panicking.
58        pb.inc(25);
59        assert_eq!(pb.position(), 25);
60        pb.finish();
61        assert!(pb.is_finished());
62    }
63
64    #[test]
65    fn feature_progress_with_zero_total() {
66        let pb = feature_progress(0, "noop");
67        assert_eq!(pb.length(), Some(0));
68        pb.finish();
69        assert!(pb.is_finished());
70    }
71}