Skip to content

Commit

Permalink
Merge branch 'master' into Improve-FinalError
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 authored Oct 15, 2021
2 parents c44cdf1 + 1bcdfeb commit d33dd7f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 117 deletions.
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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));
Expand Down Expand Up @@ -226,7 +226,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!(
Expand Down Expand Up @@ -302,6 +302,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)?;
Expand All @@ -313,19 +315,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!(
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 7 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ 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
if !self.hints.is_empty() {
// 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)
}
}

Expand Down Expand Up @@ -125,16 +125,13 @@ 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))
}
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()
))
FinalError::with_title("Possible typo detected")
.hint(format!("Did you mean '{}ouch compress{}'?", *MAGENTA, *RESET))
}
Error::UnknownExtensionError(_) => todo!(),
Error::AlreadyExists => todo!(),
Expand Down
24 changes: 7 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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);
}
10 changes: 2 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::NO_COLOR_IS_SET;

#[macro_export]
macro_rules! info {
($writer:expr, $($arg:tt)*) => {
Expand All @@ -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);
}
Loading

0 comments on commit d33dd7f

Please sign in to comment.