From e26323bd9991754a537baccbabc5df31053fdc1c Mon Sep 17 00:00:00 2001 From: a-moreira Date: Sun, 18 Dec 2022 22:49:37 -0300 Subject: [PATCH 1/4] add cli option to (de)compress quietly --- src/archive/tar.rs | 22 +++++++++++++--------- src/archive/zip.rs | 25 ++++++++++++++++--------- src/commands/compress.rs | 5 +++-- src/commands/decompress.rs | 7 ++++--- src/commands/mod.rs | 3 ++- src/opts.rs | 4 ++++ 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index f7f3763..81f6694 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -22,7 +22,7 @@ use crate::{ /// Unpacks the archive given by `archive` into the folder given by `into`. /// Assumes that output_folder is empty -pub fn unpack_archive(reader: Box, output_folder: &Path) -> crate::Result> { +pub fn unpack_archive(reader: Box, output_folder: &Path, quiet: bool) -> crate::Result> { assert!(output_folder.read_dir().expect("dir exists").count() == 0); let mut archive = tar::Archive::new(reader); @@ -37,15 +37,16 @@ pub fn unpack_archive(reader: Box, output_folder: &Path) -> crate::Res // importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - - info!( - inaccessible, - "{:?} extracted. ({})", - utils::strip_cur_dir(&output_folder.join(file.path()?)), - format_size(file.size(), DECIMAL), - ); + if !quiet { + info!( + inaccessible, + "{:?} extracted. ({})", + utils::strip_cur_dir(&output_folder.join(file.path()?)), + format_size(file.size(), DECIMAL), + ); files_unpacked.push(file_path); + } } Ok(files_unpacked) @@ -85,6 +86,7 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], output_path: &Path, writer: W, + quiet: bool, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result where @@ -118,7 +120,9 @@ where // little importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(inaccessible, "Compressing '{}'.", utils::to_utf(path)); + if !quiet { + info!(inaccessible, "Compressing '{}'.", utils::to_utf(path)); + } if path.is_dir() { builder.append_dir(path, path)?; diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 585acf7..917aaff 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -29,7 +29,7 @@ use crate::{ /// Unpacks the archive given by `archive` into the folder given by `output_folder`. /// Assumes that output_folder is empty -pub fn unpack_archive(mut archive: ZipArchive, output_folder: &Path) -> crate::Result> +pub fn unpack_archive(mut archive: ZipArchive, output_folder: &Path, quiet: bool) -> crate::Result> where R: Read + Seek, { @@ -54,7 +54,9 @@ where // importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(inaccessible, "File {} extracted to \"{}\"", idx, file_path.display()); + if !quiet { + info!(inaccessible, "File {} extracted to \"{}\"", idx, file_path.display()); + } fs::create_dir_all(&file_path)?; } _is_file @ false => { @@ -66,12 +68,14 @@ where let file_path = strip_cur_dir(file_path.as_path()); // same reason is in _is_dir: long, often not needed text - info!( - inaccessible, - "{:?} extracted. ({})", - file_path.display(), - format_size(file.size(), DECIMAL), - ); + if !quiet { + info!( + inaccessible, + "{:?} extracted. ({})", + file_path.display(), + format_size(file.size(), DECIMAL), + ); + } let mut output_file = fs::File::create(file_path)?; io::copy(&mut file, &mut output_file)?; @@ -134,6 +138,7 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], output_path: &Path, writer: W, + quiet: bool, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result where @@ -185,7 +190,9 @@ where // little importance for most users, but would generate lots of // spoken text for users using screen readers, braille displays // and so on - info!(inaccessible, "Compressing '{}'.", to_utf(path)); + if !quiet { + info!(inaccessible, "Compressing '{}'.", to_utf(path)); + } let metadata = match path.metadata() { Ok(metadata) => metadata, diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 1936036..c38b869 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -32,6 +32,7 @@ pub fn compress_files( extensions: Vec, output_file: fs::File, output_path: &Path, + quiet: bool, question_policy: QuestionPolicy, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result { @@ -74,7 +75,7 @@ pub fn compress_files( io::copy(&mut reader, &mut writer)?; } Tar => { - archive::tar::build_archive_from_paths(&files, output_path, &mut writer, file_visibility_policy)?; + archive::tar::build_archive_from_paths(&files, output_path, &mut writer, quiet, file_visibility_policy)?; writer.flush()?; } Zip => { @@ -88,7 +89,7 @@ pub fn compress_files( let mut vec_buffer = Cursor::new(vec![]); - archive::zip::build_archive_from_paths(&files, output_path, &mut vec_buffer, file_visibility_policy)?; + archive::zip::build_archive_from_paths(&files, output_path, &mut vec_buffer, quiet, file_visibility_policy)?; vec_buffer.rewind()?; io::copy(&mut vec_buffer, &mut writer)?; } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 937902d..d45307e 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -30,6 +30,7 @@ pub fn decompress_file( output_dir: &Path, output_file_path: PathBuf, question_policy: QuestionPolicy, + quiet: bool ) -> crate::Result<()> { assert!(output_dir.exists()); let reader = fs::File::open(input_file_path)?; @@ -48,7 +49,7 @@ pub fn decompress_file( { let zip_archive = zip::ZipArchive::new(reader)?; let files = if let ControlFlow::Continue(files) = smart_unpack( - |output_dir| crate::archive::zip::unpack_archive(zip_archive, output_dir), + |output_dir| crate::archive::zip::unpack_archive(zip_archive, output_dir, quiet), output_dir, &output_file_path, question_policy, @@ -111,7 +112,7 @@ pub fn decompress_file( } Tar => { if let ControlFlow::Continue(files) = smart_unpack( - |output_dir| crate::archive::tar::unpack_archive(reader, output_dir), + |output_dir| crate::archive::tar::unpack_archive(reader, output_dir, quiet), output_dir, &output_file_path, question_policy, @@ -135,7 +136,7 @@ pub fn decompress_file( let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; if let ControlFlow::Continue(files) = smart_unpack( - |output_dir| crate::archive::zip::unpack_archive(zip_archive, output_dir), + |output_dir| crate::archive::zip::unpack_archive(zip_archive, output_dir, quiet), output_dir, &output_file_path, question_policy, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d4bfea3..27bc1ac 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -164,6 +164,7 @@ pub fn run( formats, output_file, &output_path, + args.quiet, question_policy, file_visibility_policy, ); @@ -244,7 +245,7 @@ pub fn run( for ((input_path, formats), file_name) in files.iter().zip(formats).zip(output_paths) { let output_file_path = output_dir.join(file_name); // Path used by single file format archives - decompress_file(input_path, formats, &output_dir, output_file_path, question_policy)?; + decompress_file(input_path, formats, &output_dir, output_file_path, question_policy, args.quiet)?; } } Subcommand::List { archives: files, tree } => { diff --git a/src/opts.rs b/src/opts.rs index 7adc014..331eae3 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -29,6 +29,10 @@ pub struct Opts { #[arg(short = 'H', long)] pub hidden: bool, + /// Silences output + #[arg(short = 'Q', long)] + pub quiet: bool, + /// Ignores files matched by git's ignore files #[arg(short = 'g', long)] pub gitignore: bool, From 0ceb84d5cf53cfdbb22c044fe79106f663d9b180 Mon Sep 17 00:00:00 2001 From: a-moreira Date: Sun, 18 Dec 2022 22:56:13 -0300 Subject: [PATCH 2/4] better args order, fmt --- src/archive/tar.rs | 4 ++-- src/archive/zip.rs | 2 +- src/commands/compress.rs | 10 ++++++++-- src/commands/decompress.rs | 2 +- src/commands/mod.rs | 9 ++++++++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 81f6694..b48d221 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -45,7 +45,7 @@ pub fn unpack_archive(reader: Box, output_folder: &Path, quiet: bool) format_size(file.size(), DECIMAL), ); - files_unpacked.push(file_path); + files_unpacked.push(file_path); } } @@ -86,8 +86,8 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], output_path: &Path, writer: W, - quiet: bool, file_visibility_policy: FileVisibilityPolicy, + quiet: bool, ) -> crate::Result where W: Write, diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 917aaff..e48df7b 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -138,8 +138,8 @@ pub fn build_archive_from_paths( input_filenames: &[PathBuf], output_path: &Path, writer: W, - quiet: bool, file_visibility_policy: FileVisibilityPolicy, + quiet: bool, ) -> crate::Result where W: Write + Seek, diff --git a/src/commands/compress.rs b/src/commands/compress.rs index c38b869..12f6eef 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -75,7 +75,7 @@ pub fn compress_files( io::copy(&mut reader, &mut writer)?; } Tar => { - archive::tar::build_archive_from_paths(&files, output_path, &mut writer, quiet, file_visibility_policy)?; + archive::tar::build_archive_from_paths(&files, output_path, &mut writer, file_visibility_policy, quiet)?; writer.flush()?; } Zip => { @@ -89,7 +89,13 @@ pub fn compress_files( let mut vec_buffer = Cursor::new(vec![]); - archive::zip::build_archive_from_paths(&files, output_path, &mut vec_buffer, quiet, file_visibility_policy)?; + archive::zip::build_archive_from_paths( + &files, + output_path, + &mut vec_buffer, + file_visibility_policy, + quiet, + )?; vec_buffer.rewind()?; io::copy(&mut vec_buffer, &mut writer)?; } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index d45307e..d7a0167 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -30,7 +30,7 @@ pub fn decompress_file( output_dir: &Path, output_file_path: PathBuf, question_policy: QuestionPolicy, - quiet: bool + quiet: bool, ) -> crate::Result<()> { assert!(output_dir.exists()); let reader = fs::File::open(input_file_path)?; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 27bc1ac..85534a2 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -245,7 +245,14 @@ pub fn run( for ((input_path, formats), file_name) in files.iter().zip(formats).zip(output_paths) { let output_file_path = output_dir.join(file_name); // Path used by single file format archives - decompress_file(input_path, formats, &output_dir, output_file_path, question_policy, args.quiet)?; + decompress_file( + input_path, + formats, + &output_dir, + output_file_path, + question_policy, + args.quiet, + )?; } } Subcommand::List { archives: files, tree } => { From 71026287a331ca56831f1872ff770d4ae1cb6e00 Mon Sep 17 00:00:00 2001 From: afm Date: Sun, 18 Dec 2022 23:32:12 -0300 Subject: [PATCH 3/4] flag: q instead of Q MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: João Marcos Bezerra --- src/opts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opts.rs b/src/opts.rs index 331eae3..d478552 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -30,7 +30,7 @@ pub struct Opts { pub hidden: bool, /// Silences output - #[arg(short = 'Q', long)] + #[arg(short = 'q', long)] pub quiet: bool, /// Ignores files matched by git's ignore files From 7c0881b782fd47aa537c03e5be1d06b3c3d029dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 24 Dec 2022 03:12:25 -0300 Subject: [PATCH 4/4] add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b898f1d..d7336cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ Categories Used: ## [Unreleased](https://github.com/ouch-org/ouch/compare/0.4.0...HEAD) +### New Features + +- Add cli option to (de)compress quietly [\#325](https://github.com/ouch-org/ouch/pull/325) ([a-moreira](https://github.com/a-moreira)) + ### Improvements - Allow ouch to decompress archive into existing folder [\#321](https://github.com/ouch-org/ouch/pull/321) ([a-moreira](https://github.com/a-moreira))