Skip to main content

sys_shred/ui/
progress.rs

1//! # Terminal UI & Progress Feedback
2//!
3//! Provides interactive visual feedback to the user during long-running
4//! shredding operations using the `indicatif` crate.
5
6use console::style;
7use indicatif::{ProgressBar, ProgressStyle};
8
9/// A high-level reporter for managing terminal progress visuals.
10pub struct ProgressReporter {
11    /// The primary progress bar handle.
12    bar: ProgressBar,
13}
14
15impl ProgressReporter {
16    /// Creates a new `ProgressReporter` instance.
17    pub fn new() -> Self {
18        let bar = ProgressBar::new(0);
19        let style = ProgressStyle::default_bar()
20            .template(&format!(
21                "{} [{{bar:40.cyan/blue}}] {{pos}}/{{len}} files ({{msg}})",
22                style("   Shredding").green().bold()
23            ))
24            .unwrap_or_else(|_| ProgressStyle::default_bar());
25        bar.set_style(style.progress_chars("#>-"));
26
27        Self { bar }
28    }
29
30    /// Initializes the progress bar with the total number of files.
31    pub fn start_files(&self, total: u64) {
32        self.bar.set_length(total);
33        self.bar.set_message("processing...");
34    }
35
36    /// Increments the count of completed files.
37    pub fn inc_file_complete(&self) {
38        self.bar.inc(1);
39    }
40
41    /// Finalizes the progress reporting.
42    pub fn finish(&self) {
43        if !self.bar.is_finished() {
44            self.bar.set_position(self.bar.length().unwrap_or(0));
45            self.bar.finish_and_clear();
46        }
47    }
48}
49
50impl Default for ProgressReporter {
51    fn default() -> Self {
52        Self::new()
53    }
54}