geolith 0.1.1

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! Progress bar helpers using [`indicatif`].
//!
//! Provides three levels of progress feedback:
//! - [`feature_progress`] — determinate bar with position/total and ETA
//! - [`spinner`] — simple indeterminate spinner with a message
//! - [`phase_spinner`] — spinner with elapsed time and automatic tick animation

use std::time::Duration;

use indicatif::{ProgressBar, ProgressStyle};

/// Create a progress bar for feature processing.
///
/// Shows a determinate bar with position/total and estimated time remaining.
/// Use for phases where the total count is known upfront (e.g., file counts).
#[must_use]
pub fn feature_progress(total: u64, prefix: &str) -> ProgressBar {
    let pb = ProgressBar::new(total);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{prefix} [{bar:40.cyan/blue}] {pos}/{len} ({eta})")
            .expect("valid progress template")
            .progress_chars("=>-"),
    );
    pb.set_prefix(prefix.to_string());
    pb
}

/// Create a spinner with elapsed time for long-running pipeline phases.
///
/// Includes automatic tick animation (200 ms interval) so the spinner
/// stays animated even when the processing thread is busy. Call
/// [`ProgressBar::set_message`] to update the status text as work
/// progresses, and [`ProgressBar::finish_with_message`] when complete.
#[must_use]
pub fn phase_spinner(prefix: &str) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {prefix}: {msg} [{elapsed_precise}]")
            .expect("valid progress template"),
    );
    pb.set_prefix(prefix.to_string());
    pb.enable_steady_tick(Duration::from_millis(200));
    pb
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn feature_progress_initialises_with_total_and_prefix() {
        let pb = feature_progress(100, "phase-1");
        assert_eq!(pb.length(), Some(100));
        assert_eq!(pb.position(), 0);
        // Bar accepts manual position updates without panicking.
        pb.inc(25);
        assert_eq!(pb.position(), 25);
        pb.finish();
        assert!(pb.is_finished());
    }

    #[test]
    fn feature_progress_with_zero_total() {
        let pb = feature_progress(0, "noop");
        assert_eq!(pb.length(), Some(0));
        pb.finish();
        assert!(pb.is_finished());
    }
}