Merge branch 'master' into Improve-FinalError

This commit is contained in:
João Marcos Bezerra 2021-10-15 11:23:25 -03:00 committed by GitHub
commit d33dd7f445
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 117 deletions

15
CONTRIBUTING.md Normal file
View File

@ -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.

58
Cargo.lock generated
View File

@ -27,9 +27,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -60,9 +60,9 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.69" version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd"
dependencies = [ dependencies = [
"jobserver", "jobserver",
] ]
@ -94,9 +94,9 @@ dependencies = [
[[package]] [[package]]
name = "filetime" name = "filetime"
version = "0.2.14" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
@ -155,12 +155,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.103" version = "0.2.103"
@ -199,6 +193,12 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "once_cell"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
[[package]] [[package]]
name = "ouch" name = "ouch"
version = "0.2.0" version = "0.2.0"
@ -207,8 +207,8 @@ dependencies = [
"bzip2", "bzip2",
"flate2", "flate2",
"infer", "infer",
"lazy_static",
"libc", "libc",
"once_cell",
"rand", "rand",
"strsim", "strsim",
"tar", "tar",
@ -221,30 +221,30 @@ dependencies = [
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.19" version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" checksum = "7c9b1041b4387893b91ee6746cddfc28516aff326a3519fb2adf820932c5e6cb"
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.10" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" checksum = "c3ca011bd0129ff4ae15cd04c4eef202cadf6c51c21e47aba319b4e0501db741"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.28" version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.9" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -291,9 +291,9 @@ dependencies = [
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.9" version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
dependencies = [ dependencies = [
"bitflags", "bitflags",
] ]
@ -324,9 +324,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.74" version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -360,18 +360,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.26" version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.26" version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -14,7 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f
[dependencies] [dependencies]
atty = "0.2.14" atty = "0.2.14"
lazy_static = "1.4.0" once_cell = "1.8.0"
walkdir = "2.3.2" walkdir = "2.3.2"
strsim = "0.10.0" strsim = "0.10.0"
bzip2 = "0.4.3" bzip2 = "0.4.3"

View File

@ -106,10 +106,10 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
// Print an extra alert message pointing out that we left a possibly // Print an extra alert message pointing out that we left a possibly
// CORRUPTED FILE at `output_path` // CORRUPTED FILE at `output_path`
if let Err(err) = fs::remove_file(&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!(" Please manually delete '{}'.", to_utf(&output_path));
eprintln!(" Compression failed and we could not 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 { } else {
info!("Successfully compressed '{}'.", to_utf(output_path)); info!("Successfully compressed '{}'.", to_utf(output_path));
@ -226,7 +226,7 @@ fn compress_files(
writer.flush()?; writer.flush()?;
} }
Zip => { 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!("\tCompressing .zip entirely in memory.");
eprintln!("\tIf the file is too big, your PC might freeze!"); eprintln!("\tIf the file is too big, your PC might freeze!");
eprintln!( eprintln!(
@ -302,6 +302,8 @@ fn decompress_file(
reader = chain_reader_decoder(format, reader)?; reader = chain_reader_decoder(format, reader)?;
} }
utils::create_dir_if_non_existent(output_folder)?;
match formats[0] { match formats[0] {
Gzip | Bzip | Lzma | Zstd => { Gzip | Bzip | Lzma | Zstd => {
reader = chain_reader_decoder(&formats[0], reader)?; reader = chain_reader_decoder(&formats[0], reader)?;
@ -313,19 +315,15 @@ fn decompress_file(
info!("Successfully uncompressed archive in '{}'.", to_utf(output_path)); info!("Successfully uncompressed archive in '{}'.", to_utf(output_path));
} }
Tar => { Tar => {
utils::create_dir_if_non_existent(output_folder)?;
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder)); info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
} }
Tgz => { Tgz => {
utils::create_dir_if_non_existent(output_folder)?;
let reader = chain_reader_decoder(&Gzip, reader)?; let reader = chain_reader_decoder(&Gzip, reader)?;
let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?;
info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder)); info!("Successfully uncompressed archive in '{}'.", to_utf(output_folder));
} }
Zip => { Zip => {
utils::create_dir_if_non_existent(output_folder)?;
eprintln!("Compressing first into .zip."); eprintln!("Compressing first into .zip.");
eprintln!("Warning: .zip archives with extra extensions have a downside."); eprintln!("Warning: .zip archives with extra extensions have a downside.");
eprintln!( eprintln!(

View File

@ -29,7 +29,7 @@ impl<'a> Confirmation<'a> {
}; };
loop { 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()?; io::stdout().flush()?;
let mut answer = String::new(); let mut answer = String::new();

View File

@ -45,11 +45,11 @@ pub struct FinalError {
impl Display for FinalError { impl Display for FinalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Title // Title
writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?; writeln!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?;
// Details // Details
for detail in &self.details { for detail in &self.details {
writeln!(f, " {}-{} {}", white(), yellow(), detail)?; writeln!(f, " {}-{} {}", *WHITE, *YELLOW, detail)?;
} }
// Hints // Hints
@ -57,11 +57,11 @@ impl Display for FinalError {
// Separate by one blank line. // Separate by one blank line.
writeln!(f)?; writeln!(f)?;
for hint in &self.hints { for hint in &self.hints {
writeln!(f, "{}hint:{} {}", green(), reset(), hint)?; writeln!(f, "{}hint:{} {}", *GREEN, *RESET, hint)?;
} }
} }
write!(f, "{}", reset()) write!(f, "{}", *RESET)
} }
} }
@ -125,16 +125,13 @@ impl fmt::Display for Error {
.detail("This should not have happened") .detail("This should not have happened")
.detail("It's probably our fault") .detail("It's probably our fault")
.detail("Please help us improve by reporting the issue at:") .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))
} }
Error::OofError(err) => FinalError::with_title(err), Error::OofError(err) => FinalError::with_title(err),
Error::IoError { reason } => FinalError::with_title(reason), Error::IoError { reason } => FinalError::with_title(reason),
Error::CompressionTypo => { Error::CompressionTypo => {
FinalError::with_title("Possible typo detected").hint(format!( FinalError::with_title("Possible typo detected")
"Did you mean '{}ouch compress{}'?", .hint(format!("Did you mean '{}ouch compress{}'?", *MAGENTA, *RESET))
magenta(),
reset()
))
} }
Error::UnknownExtensionError(_) => todo!(), Error::UnknownExtensionError(_) => todo!(),
Error::AlreadyExists => todo!(), Error::AlreadyExists => todo!(),

View File

@ -19,21 +19,11 @@ mod utils;
pub use error::{Error, Result}; pub use error::{Error, Result};
use lazy_static::lazy_static;
/// The status code ouch has when an error is encountered /// The status code ouch has when an error is encountered
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE; pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
const VERSION: &str = env!("CARGO_PKG_VERSION"); 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() { fn help_command() {
use utils::colors::*; use utils::colors::*;
@ -62,17 +52,17 @@ fn help_command() {
another folder. another folder.
Visit https://github.com/ouch-org/ouch for more usage examples.", Visit https://github.com/ouch-org/ouch for more usage examples.",
magenta = magenta(), magenta = *MAGENTA,
white = white(), white = *WHITE,
green = green(), green = *GREEN,
yellow = yellow(), yellow = *YELLOW,
reset = reset(), reset = *RESET,
cyan = cyan() cyan = *CYAN
); );
} }
#[inline] #[inline]
fn version_command() { fn version_command() {
use utils::colors::*; use utils::colors::*;
println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset()); println!("{green}ouch{reset} {}", crate::VERSION, green = *GREEN, reset = *RESET);
} }

View File

@ -1,5 +1,3 @@
use crate::NO_COLOR_IS_SET;
#[macro_export] #[macro_export]
macro_rules! info { macro_rules! info {
($writer:expr, $($arg:tt)*) => { ($writer:expr, $($arg:tt)*) => {
@ -14,11 +12,7 @@ macro_rules! info {
} }
pub fn _info_helper() { pub fn _info_helper() {
use crate::utils::colors::{reset, yellow}; use crate::utils::colors::{RESET, YELLOW};
if *NO_COLOR_IS_SET { print!("{}[INFO]{} ", *YELLOW, *RESET);
print!("[INFO] ");
} else {
print!("{}[INFO]{} ", yellow(), reset());
}
} }

View File

@ -64,52 +64,31 @@ pub struct Bytes {
/// Module with a list of bright colors. /// Module with a list of bright colors.
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(target_family = "unix")]
pub mod colors { pub mod colors {
pub const fn reset() -> &'static str { use once_cell::sync::Lazy;
"\u{1b}[39m"
static DISABLE_COLORED_TEXT: Lazy<bool> = 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 *DISABLE_COLORED_TEXT { "" } else { $value });
#[cfg(not(target_family = "unix"))]
pub static $name: &&str = &"";
};
} }
pub const fn black() -> &'static str {
"\u{1b}[38;5;8m" color!(RESET = "\u{1b}[39m");
} color!(BLACK = "\u{1b}[38;5;8m");
pub const fn blue() -> &'static str { color!(BLUE = "\u{1b}[38;5;12m");
"\u{1b}[38;5;12m" color!(CYAN = "\u{1b}[38;5;14m");
} color!(GREEN = "\u{1b}[38;5;10m");
pub const fn cyan() -> &'static str { color!(MAGENTA = "\u{1b}[38;5;13m");
"\u{1b}[38;5;14m" color!(RED = "\u{1b}[38;5;9m");
} color!(WHITE = "\u{1b}[38;5;15m");
pub const fn green() -> &'static str { color!(YELLOW = "\u{1b}[38;5;11m");
"\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;
} }
impl Bytes { impl Bytes {