hexz_core/ops/
progress.rs1use indicatif::{ProgressBar, ProgressStyle};
6
7pub struct PackProgress {
9 bar: ProgressBar,
10}
11
12impl PackProgress {
13 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 pub fn inc(&self, delta: u64) {
34 self.bar.inc(delta);
35 }
36
37 pub fn set_position(&self, pos: u64) {
39 self.bar.set_position(pos);
40 }
41
42 pub fn finish(&self) {
44 self.bar.finish_with_message("Packing complete");
45 }
46
47 pub fn finish_with_message(&self, msg: &str) {
49 self.bar.finish_with_message(msg.to_string());
50 }
51
52 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}