this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add argument to control progress bar

+54 -21
+15 -1
src/main.rs
··· 7 7 8 8 use clap::{Args, Parser, Subcommand}; 9 9 use is_terminal::IsTerminal; 10 + use progress::ProgressDisplay; 10 11 use std::io; 11 12 use std::path::{Path, PathBuf}; 12 13 use utils::*; ··· 41 42 struct TarArgs { 42 43 #[clap(flatten)] 43 44 common_args: CommonArgs, 45 + } 46 + 47 + #[derive(Args, Debug)] 48 + struct ProgressArgs { 49 + /// Show progress. 50 + #[arg(long, value_enum, default_value = "auto")] 51 + progress: ProgressDisplay, 44 52 } 45 53 46 54 #[derive(Args, Debug)] ··· 95 103 struct XzArgs { 96 104 #[clap(flatten)] 97 105 common_args: CommonArgs, 106 + 107 + #[clap(flatten)] 108 + progress_args: ProgressArgs, 98 109 99 110 /// Level of compression 100 111 /// ··· 292 303 } 293 304 294 305 fn parse_xz(args: &XzArgs) -> xz::Xz { 295 - xz::Xz { level: args.level } 306 + xz::Xz { 307 + level: args.level, 308 + progress: args.progress_args.progress, 309 + } 296 310 } 297 311 298 312 fn parse_bzip2(args: &Bzip2Args) -> bzip2::Bzip2 {
+22
src/progress.rs
··· 1 + use crate::utils::CmprssOutput; 1 2 use indicatif::{HumanBytes, ProgressBar}; 2 3 4 + #[derive(clap::ValueEnum, Clone, Copy, Debug)] 5 + pub enum ProgressDisplay { 6 + Auto, 7 + On, 8 + Off, 9 + } 10 + 3 11 /// Progress bar for the compress process 4 12 pub struct Progress { 5 13 /// The progress bar ··· 10 18 output_written: u64, 11 19 } 12 20 21 + /// Create a progress bar if necessary 22 + pub 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 + 13 34 impl Progress { 14 35 /// Create a new progress bar 36 + /// Draws to stderr by default 15 37 pub fn new(input_size: Option<u64>) -> Self { 16 38 let bar = match input_size { 17 39 Some(size) => ProgressBar::new(size),
+17 -20
src/xz.rs
··· 1 - use crate::{progress::Progress, utils::*}; 1 + use crate::{ 2 + progress::{progress_bar, ProgressDisplay}, 3 + utils::*, 4 + }; 2 5 use std::{ 3 6 fs::File, 4 7 io::{self, Read, Write}, ··· 7 10 8 11 pub struct Xz { 9 12 pub level: u32, 13 + pub progress: ProgressDisplay, 10 14 } 11 15 12 16 impl Default for Xz { 13 17 fn default() -> Self { 14 - Xz { level: 6 } 18 + Xz { 19 + level: 6, 20 + progress: ProgressDisplay::Auto, 21 + } 15 22 } 16 23 } 17 24 ··· 42 49 } 43 50 CmprssInput::Pipe(pipe) => Box::new(pipe) as Box<dyn Read + Send>, 44 51 }; 45 - // We want to use the progress bar unless this is in the middle of a pipe 46 - let mut progress_bar = false; 47 - let output_stream: Box<dyn Write + Send> = match output { 48 - CmprssOutput::Path(path) => { 49 - progress_bar = true; 50 - Box::new(File::create(path)?) 51 - } 52 + let output_stream: Box<dyn Write + Send> = match &output { 53 + CmprssOutput::Path(path) => Box::new(File::create(path)?), 52 54 CmprssOutput::Pipe(pipe) => Box::new(pipe) as Box<dyn Write + Send>, 53 55 }; 54 56 let mut encoder = XzEncoder::new(output_stream, self.level); 55 - if progress_bar { 56 - let mut progress = Progress::new(file_size); 57 + let mut bar = progress_bar(file_size, self.progress, &output); 58 + if let Some(progress) = &mut bar { 57 59 // Copy the input to the output in 8k chunks 58 60 let mut buffer = [0; 8192]; 59 61 loop { ··· 88 90 } 89 91 CmprssInput::Pipe(pipe) => Box::new(pipe) as Box<dyn Read + Send>, 90 92 }; 91 - // We want to use the progress bar unless this is in the middle of a pipe 92 - let mut progress_bar = false; 93 - let output_stream: Box<dyn Write + Send> = match output { 94 - CmprssOutput::Path(path) => { 95 - progress_bar = true; 96 - Box::new(File::create(path)?) 97 - } 93 + let output_stream: Box<dyn Write + Send> = match &output { 94 + CmprssOutput::Path(path) => Box::new(File::create(path)?), 98 95 CmprssOutput::Pipe(pipe) => Box::new(pipe) as Box<dyn Write + Send>, 99 96 }; 100 97 let mut decoder = XzDecoder::new(output_stream); 101 - if progress_bar { 102 - let mut progress = Progress::new(file_size); 98 + let mut bar = progress_bar(file_size, self.progress, &output); 99 + if let Some(progress) = &mut bar { 103 100 // Copy the input to the output in 8k chunks 104 101 let mut buffer = [0; 8192]; 105 102 loop {