From 69e5e3291cf6d90d79698d17c231be8217306432 Mon Sep 17 00:00:00 2001 From: figsoda Date: Thu, 14 Oct 2021 19:00:40 -0400 Subject: [PATCH 1/5] Fix single file decompression with specified output directory (#93) --- src/commands.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 05db7a6..c8289fa 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -305,6 +305,8 @@ fn decompress_file( reader = chain_reader_decoder(format, reader)?; } + utils::create_dir_if_non_existent(output_folder)?; + match formats[0] { Gzip | Bzip | Lzma | Zstd => { reader = chain_reader_decoder(&formats[0], reader)?; @@ -316,19 +318,15 @@ fn decompress_file( info!("Successfully uncompressed archive in '{}'.", to_utf(output_path)); } Tar => { - utils::create_dir_if_non_existent(output_folder)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder)); } Tgz => { - utils::create_dir_if_non_existent(output_folder)?; let reader = chain_reader_decoder(&Gzip, reader)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder)); } Zip => { - utils::create_dir_if_non_existent(output_folder)?; - eprintln!("Compressing first into .zip."); eprintln!("Warning: .zip archives with extra extensions have a downside."); eprintln!( From 16fbebe8fe9b1d771ebd6c2d4ce5b3fb5f5b7e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Bezerra?= Date: Thu, 14 Oct 2021 20:12:42 -0300 Subject: [PATCH 2/5] Updating Cargo.lock to newer dependencies (#92) bitflags v1.2.1 -> v1.3.2 cc v1.0.69 -> v1.0.71 filetime v0.2.14 -> v0.2.15 pkg-config v0.3.19 -> v0.3.20 ppv-lite86 v0.2.10 -> v0.2.14 proc-macro2 v1.0.28 -> v1.0.30 quote v1.0.9 -> v1.0.10 redox_syscall v0.2.9 -> v0.2.10 syn v1.0.74 -> v1.0.80 thiserror v1.0.26 -> v1.0.30 thiserror-impl v1.0.26 -> v1.0.30 --- Cargo.lock | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc1dcff..2a9d63a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,9 +27,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "byteorder" @@ -60,9 +60,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.69" +version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" +checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd" dependencies = [ "jobserver", ] @@ -94,9 +94,9 @@ dependencies = [ [[package]] name = "filetime" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" +checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98" dependencies = [ "cfg-if", "libc", @@ -221,30 +221,30 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.19" +version = "0.3.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +checksum = "7c9b1041b4387893b91ee6746cddfc28516aff326a3519fb2adf820932c5e6cb" [[package]] name = "ppv-lite86" -version = "0.2.10" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +checksum = "c3ca011bd0129ff4ae15cd04c4eef202cadf6c51c21e47aba319b4e0501db741" [[package]] name = "proc-macro2" -version = "1.0.28" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" dependencies = [ "proc-macro2", ] @@ -291,9 +291,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" dependencies = [ "bitflags", ] @@ -324,9 +324,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.74" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194" dependencies = [ "proc-macro2", "quote", @@ -360,18 +360,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.26" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.26" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2", "quote", From 161b8d0d66ef63d44ba682ee693b9a57b7d7c68f Mon Sep 17 00:00:00 2001 From: figsoda Date: Thu, 14 Oct 2021 21:25:57 -0400 Subject: [PATCH 3/5] refactor: better NO_COLOR support --- Cargo.lock | 14 +++++------ Cargo.toml | 2 +- src/commands.rs | 6 ++--- src/dialogs.rs | 2 +- src/error.rs | 12 ++++----- src/lib.rs | 24 ++++++------------ src/macros.rs | 10 ++------ src/utils.rs | 67 +++++++++++++++++-------------------------------- 8 files changed, 50 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a9d63a..a800ba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,12 +155,6 @@ dependencies = [ "libc", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.103" @@ -199,6 +193,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" + [[package]] name = "ouch" version = "0.2.0" @@ -207,8 +207,8 @@ dependencies = [ "bzip2", "flate2", "infer", - "lazy_static", "libc", + "once_cell", "rand", "strsim", "tar", diff --git a/Cargo.toml b/Cargo.toml index f1d3a9f..071672a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f [dependencies] atty = "0.2.14" -lazy_static = "1.4.0" +once_cell = "1.8.0" walkdir = "2.3.2" strsim = "0.10.0" bzip2 = "0.4.3" diff --git a/src/commands.rs b/src/commands.rs index c8289fa..11e345a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -109,10 +109,10 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> { // Print an extra alert message pointing out that we left a possibly // CORRUPTED FILE at `output_path` if let Err(err) = fs::remove_file(&output_path) { - eprintln!("{red}FATAL ERROR:\n", red = colors::red()); + eprintln!("{red}FATAL ERROR:\n", red = *colors::RED); eprintln!(" Please manually delete '{}'.", to_utf(&output_path)); eprintln!(" Compression failed and we could not delete '{}'.", to_utf(&output_path),); - eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = colors::reset(), red = colors::red()); + eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = *colors::RESET, red = *colors::RED); } } else { info!("Successfully compressed '{}'.", to_utf(output_path)); @@ -229,7 +229,7 @@ fn compress_files( writer.flush()?; } Zip => { - eprintln!("{yellow}Warning:{reset}", yellow = colors::yellow(), reset = colors::reset()); + eprintln!("{yellow}Warning:{reset}", yellow = *colors::YELLOW, reset = *colors::RESET); eprintln!("\tCompressing .zip entirely in memory."); eprintln!("\tIf the file is too big, your PC might freeze!"); eprintln!( diff --git a/src/dialogs.rs b/src/dialogs.rs index 95db53f..4f4d7b2 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -29,7 +29,7 @@ impl<'a> Confirmation<'a> { }; loop { - print!("{} [{}Y{}/{}n{}] ", message, colors::green(), colors::reset(), colors::red(), colors::reset()); + print!("{} [{}Y{}/{}n{}] ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET); io::stdout().flush()?; let mut answer = String::new(); diff --git a/src/error.rs b/src/error.rs index caf7e6c..9a7e673 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,11 +45,11 @@ pub struct FinalError { impl Display for FinalError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Title - writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?; + writeln!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?; // Details for detail in &self.details { - writeln!(f, " {}-{} {}", white(), yellow(), detail)?; + writeln!(f, " {}-{} {}", *WHITE, *YELLOW, detail)?; } // Hints @@ -57,11 +57,11 @@ impl Display for FinalError { // Separate by one blank line. writeln!(f)?; for hint in &self.hints { - writeln!(f, "{}hint:{} {}", green(), reset(), hint)?; + writeln!(f, "{}hint:{} {}", *GREEN, *RESET, hint)?; } } - write!(f, "{}", reset()) + write!(f, "{}", *RESET) } } @@ -139,7 +139,7 @@ impl fmt::Display for Error { .detail("This should not have happened") .detail("It's probably our fault") .detail("Please help us improve by reporting the issue at:") - .detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan())) + .detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", *CYAN)) .clone(); error @@ -147,7 +147,7 @@ impl fmt::Display for Error { Error::OofError(err) => FinalError::with_title(err), Error::IoError { reason } => FinalError::with_title(reason), Error::CompressionTypo => FinalError::with_title("Possible typo detected") - .hint(format!("Did you mean '{}ouch compress{}'?", magenta(), reset())) + .hint(format!("Did you mean '{}ouch compress{}'?", *MAGENTA, *RESET)) .clone(), Error::UnknownExtensionError(_) => todo!(), Error::AlreadyExists => todo!(), diff --git a/src/lib.rs b/src/lib.rs index 52537a8..3c3830d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,21 +19,11 @@ mod utils; pub use error::{Error, Result}; -use lazy_static::lazy_static; - /// The status code ouch has when an error is encountered pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE; const VERSION: &str = env!("CARGO_PKG_VERSION"); -lazy_static! { - static ref NO_COLOR_IS_SET: bool = { - use std::env; - - env::var("NO_COLOR").is_ok() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr) - }; -} - fn help_command() { use utils::colors::*; @@ -62,17 +52,17 @@ fn help_command() { another folder. Visit https://github.com/ouch-org/ouch for more usage examples.", - magenta = magenta(), - white = white(), - green = green(), - yellow = yellow(), - reset = reset(), - cyan = cyan() + magenta = *MAGENTA, + white = *WHITE, + green = *GREEN, + yellow = *YELLOW, + reset = *RESET, + cyan = *CYAN ); } #[inline] fn version_command() { use utils::colors::*; - println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset()); + println!("{green}ouch{reset} {}", crate::VERSION, green = *GREEN, reset = *RESET); } diff --git a/src/macros.rs b/src/macros.rs index bed8ba9..14996e3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,3 @@ -use crate::NO_COLOR_IS_SET; - #[macro_export] macro_rules! info { ($writer:expr, $($arg:tt)*) => { @@ -14,11 +12,7 @@ macro_rules! info { } pub fn _info_helper() { - use crate::utils::colors::{reset, yellow}; + use crate::utils::colors::{RESET, YELLOW}; - if *NO_COLOR_IS_SET { - print!("[INFO] "); - } else { - print!("{}[INFO]{} ", yellow(), reset()); - } + print!("{}[INFO]{} ", *YELLOW, *RESET); } diff --git a/src/utils.rs b/src/utils.rs index a03d227..74b7210 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -64,52 +64,31 @@ pub struct Bytes { /// Module with a list of bright colors. #[allow(dead_code)] -#[cfg(target_family = "unix")] pub mod colors { - pub const fn reset() -> &'static str { - "\u{1b}[39m" + use once_cell::sync::Lazy; + + static NO_COLOR_IS_SET: Lazy = Lazy::new(|| { + std::env::var_os("NO_COLOR").is_some() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr) + }); + + macro_rules! color { + ($name:ident = $value:literal) => { + #[cfg(target_family = "unix")] + pub static $name: Lazy<&str> = Lazy::new(|| if *NO_COLOR_IS_SET { "" } else { $value }); + #[cfg(not(target_family = "unix"))] + pub static $name: &&str = &""; + }; } - pub const fn black() -> &'static str { - "\u{1b}[38;5;8m" - } - pub const fn blue() -> &'static str { - "\u{1b}[38;5;12m" - } - pub const fn cyan() -> &'static str { - "\u{1b}[38;5;14m" - } - pub const fn green() -> &'static str { - "\u{1b}[38;5;10m" - } - pub const fn magenta() -> &'static str { - "\u{1b}[38;5;13m" - } - pub const fn red() -> &'static str { - "\u{1b}[38;5;9m" - } - pub const fn white() -> &'static str { - "\u{1b}[38;5;15m" - } - pub const fn yellow() -> &'static str { - "\u{1b}[38;5;11m" - } -} -// Windows does not support ANSI escape codes -#[allow(dead_code, non_upper_case_globals)] -#[cfg(not(target_family = "unix"))] -pub mod colors { - pub const fn empty() -> &'static str { - "" - } - pub const reset: fn() -> &'static str = empty; - pub const black: fn() -> &'static str = empty; - pub const blue: fn() -> &'static str = empty; - pub const cyan: fn() -> &'static str = empty; - pub const green: fn() -> &'static str = empty; - pub const magenta: fn() -> &'static str = empty; - pub const red: fn() -> &'static str = empty; - pub const white: fn() -> &'static str = empty; - pub const yellow: fn() -> &'static str = empty; + + color!(RESET = "\u{1b}[39m"); + color!(BLACK = "\u{1b}[38;5;8m"); + color!(BLUE = "\u{1b}[38;5;12m"); + color!(CYAN = "\u{1b}[38;5;14m"); + color!(GREEN = "\u{1b}[38;5;10m"); + color!(MAGENTA = "\u{1b}[38;5;13m"); + color!(RED = "\u{1b}[38;5;9m"); + color!(WHITE = "\u{1b}[38;5;15m"); + color!(YELLOW = "\u{1b}[38;5;11m"); } impl Bytes { From 15c54a615de74ec32ae3cabf26394ac4222faa4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 15 Oct 2021 09:35:07 -0300 Subject: [PATCH 4/5] Renaming NO_COLOR_IS_SET to DISABLE_COLORED_TEXT --- src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 74b7210..a372051 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -67,14 +67,14 @@ pub struct Bytes { pub mod colors { use once_cell::sync::Lazy; - static NO_COLOR_IS_SET: Lazy = Lazy::new(|| { + static DISABLE_COLORED_TEXT: Lazy = Lazy::new(|| { std::env::var_os("NO_COLOR").is_some() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr) }); macro_rules! color { ($name:ident = $value:literal) => { #[cfg(target_family = "unix")] - pub static $name: Lazy<&str> = Lazy::new(|| if *NO_COLOR_IS_SET { "" } else { $value }); + pub static $name: Lazy<&str> = Lazy::new(|| if *DISABLE_COLORED_TEXT { "" } else { $value }); #[cfg(not(target_family = "unix"))] pub static $name: &&str = &""; }; From cf7b3c6e1f1d7324c8f4891d5d208ea66f35eaaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Bezerra?= Date: Fri, 15 Oct 2021 10:47:37 -0300 Subject: [PATCH 5/5] Create CONTRIBUTING.md --- CONTRIBUTING.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..67be18c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,15 @@ +Thanks for your interest in contributing to `ouch`! + +Feel free to open an issue anytime you wish to ask a question, suggest a feature, report a bug, etc. + +# Requirements + +1. Be kind, considerate and respectfull. +2. If editing .rs files, run `rustfmt` on them before commiting. + +Note that we are using `unstable` features of `rustfmt`, so you will need to change your toolchain to nightly. + +# Suggestions + +1. Ask for some guidance before solving an error if you feel like it. +2. If editing Rust code, run `clippy` before commiting.