this repo has no description
1use crate::utils::CmprssOutput;
2use indicatif::{HumanBytes, ProgressBar};
3
4#[derive(clap::ValueEnum, Clone, Copy, Debug)]
5pub enum ProgressDisplay {
6 Auto,
7 On,
8 Off,
9}
10
11/// Progress bar for the compress process
12pub struct Progress {
13 /// The progress bar
14 bar: ProgressBar,
15 /// The number of bytes read from the input
16 input_read: u64,
17 /// The number of bytes written to the output
18 output_written: u64,
19}
20
21/// Create a progress bar if necessary
22pub fn progress_bar(
23 input_size: Option<u64>,
24 progress: ProgressDisplay,
25 output: &CmprssOutput,
26) -> Option<Progress> {
27 match (progress, output) {
28 (ProgressDisplay::Auto, CmprssOutput::Pipe(_)) => None,
29 (ProgressDisplay::Off, _) => None,
30 (_, _) => Some(Progress::new(input_size)),
31 }
32}
33
34impl Progress {
35 /// Create a new progress bar
36 /// Draws to stderr by default
37 pub fn new(input_size: Option<u64>) -> Self {
38 let bar = match input_size {
39 Some(size) => ProgressBar::new(size),
40 None => ProgressBar::new_spinner(),
41 };
42 bar.set_style(
43 indicatif::ProgressStyle::default_bar()
44 .template("{spinner:.green} [{elapsed_precise}] ({eta}) [{bar:40.cyan/blue}] {bytes}/{total_bytes} => {msg}").unwrap()
45 .progress_chars("#>-"),
46 );
47 Progress {
48 bar,
49 input_read: 0,
50 output_written: 0,
51 }
52 }
53
54 /// Update the progress bar with the number of bytes read from the input
55 pub fn update_input(&mut self, bytes_read: u64) {
56 self.input_read = bytes_read;
57 self.bar.set_position(self.input_read);
58 }
59
60 /// Update the progress bar with the number of bytes written to the output
61 pub fn update_output(&mut self, bytes_written: u64) {
62 self.output_written = bytes_written;
63 self.bar
64 .set_message(HumanBytes(self.output_written).to_string());
65 }
66
67 /// Finish the progress bar
68 pub fn finish(&self) {
69 self.bar.finish();
70 }
71}