Skip to main content

hexz_core/ops/
progress.rs

1//! Progress reporting for long-running pack operations.
2//!
3//! Provides a consistent progress bar interface using indicatif.
4
5use indicatif::{ProgressBar, ProgressStyle};
6
7/// Progress tracker for packing operations
8pub struct PackProgress {
9    bar: ProgressBar,
10}
11
12impl PackProgress {
13    /// Create a new progress bar
14    ///
15    /// # Arguments
16    ///
17    /// * `total_bytes` - Total bytes to process
18    /// * `message` - Initial message to display
19    pub fn new(total_bytes: u64, message: &str) -> Self {
20        let bar = ProgressBar::new(total_bytes);
21        bar.set_style(
22            ProgressStyle::default_bar()
23                .template("{msg} {bar:40.cyan/blue} {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
24                .unwrap()
25                .progress_chars("█▓▒░ "),
26        );
27        bar.set_message(message.to_string());
28
29        Self { bar }
30    }
31
32    /// Increment progress by delta bytes
33    pub fn inc(&self, delta: u64) {
34        self.bar.inc(delta);
35    }
36
37    /// Set current position directly
38    pub fn set_position(&self, pos: u64) {
39        self.bar.set_position(pos);
40    }
41
42    /// Finish with success message
43    pub fn finish(&self) {
44        self.bar.finish_with_message("Packing complete");
45    }
46
47    /// Finish with custom message
48    pub fn finish_with_message(&self, msg: &str) {
49        self.bar.finish_with_message(msg.to_string());
50    }
51
52    /// Update the message
53    pub fn set_message(&self, msg: &str) {
54        self.bar.set_message(msg.to_string());
55    }
56}
57
58impl Drop for PackProgress {
59    fn drop(&mut self) {
60        if !self.bar.is_finished() {
61            self.bar.abandon();
62        }
63    }
64}