this repo has no description
0
fork

Configure Feed

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

feat: add an option to set the chunk size for monitoring progress

+46 -4
+40
src/main.rs
··· 10 10 use progress::ProgressDisplay; 11 11 use std::io; 12 12 use std::path::{Path, PathBuf}; 13 + use std::str::FromStr; 13 14 use utils::*; 14 15 15 16 /// A compression multi-tool ··· 44 45 common_args: CommonArgs, 45 46 } 46 47 48 + #[derive(Debug, Clone)] 49 + struct ChunkSize { 50 + size_in_bytes: usize, 51 + } 52 + 53 + impl FromStr for ChunkSize { 54 + type Err = &'static str; 55 + 56 + fn from_str(s: &str) -> Result<Self, Self::Err> { 57 + // Try to parse s as just a number 58 + if let Ok(num) = s.parse::<usize>() { 59 + return Ok(ChunkSize { size_in_bytes: num }); 60 + } 61 + // Simplify so that we always assume base 2, regardless of whether we see 62 + // 'kb' or 'kib' 63 + let mut s = s.to_lowercase(); 64 + if s.ends_with("ib") { 65 + s.truncate(s.len() - 2); 66 + s.push('b'); 67 + }; 68 + let (num_str, unit) = s.split_at(s.len() - 2); 69 + let num = num_str.parse::<usize>().map_err(|_| "Invalid number")?; 70 + 71 + let size_in_bytes = match unit { 72 + "kb" => num * 1024, 73 + "mb" => num * 1024 * 1024, 74 + "gb" => num * 1024 * 1024 * 1024, 75 + _ => return Err("Invalid unit"), 76 + }; 77 + 78 + Ok(ChunkSize { size_in_bytes }) 79 + } 80 + } 81 + 47 82 #[derive(Args, Debug)] 48 83 struct ProgressArgs { 49 84 /// Show progress. 50 85 #[arg(long, value_enum, default_value = "auto")] 51 86 progress: ProgressDisplay, 87 + 88 + /// Chunk size to use during the copy when showing the progress bar. 89 + #[arg(long, default_value = "8kib")] 90 + chunk_size: ChunkSize, 52 91 } 53 92 54 93 #[derive(Args, Debug)] ··· 306 345 xz::Xz { 307 346 level: args.level, 308 347 progress: args.progress_args.progress, 348 + chunk_size: args.progress_args.chunk_size.size_in_bytes, 309 349 } 310 350 } 311 351
+6 -4
src/xz.rs
··· 11 11 pub struct Xz { 12 12 pub level: u32, 13 13 pub progress: ProgressDisplay, 14 + pub chunk_size: usize, 14 15 } 15 16 16 17 impl Default for Xz { ··· 18 19 Xz { 19 20 level: 6, 20 21 progress: ProgressDisplay::Auto, 22 + chunk_size: 8192, 21 23 } 22 24 } 23 25 } ··· 56 58 let mut encoder = XzEncoder::new(output_stream, self.level); 57 59 let mut bar = progress_bar(file_size, self.progress, &output); 58 60 if let Some(progress) = &mut bar { 59 - // Copy the input to the output in 8k chunks 60 - let mut buffer = [0; 8192]; 61 + // Copy the input to the output in chunks so that we can update the progress bar 62 + let mut buffer = vec![0; self.chunk_size]; 61 63 loop { 62 64 let bytes_read = input_stream.read(&mut buffer)?; 63 65 if bytes_read == 0 { ··· 99 101 let mut decoder = XzDecoder::new(output_stream); 100 102 let mut bar = progress_bar(file_size, self.progress, &output); 101 103 if let Some(progress) = &mut bar { 102 - // Copy the input to the output in 8k chunks 103 - let mut buffer = [0; 8192]; 104 + // Copy the input to the output in chunks so that we can update the progress bar 105 + let mut buffer = vec![0; self.chunk_size]; 104 106 loop { 105 107 let bytes_read = input_stream.read(&mut buffer)?; 106 108 if bytes_read == 0 {