Skip to content

Commit

Permalink
Fix ups
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Nov 10, 2021
1 parent ac1f720 commit 9eb07f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
self, concatenate_list_of_os_str, dir_is_empty, nice_directory_display, to_utf, try_infer,
user_wants_to_continue_decompressing,
},
Opts, QuestionPolicy, Subcommand,
warning, Opts, QuestionPolicy, Subcommand,
};

// Used in BufReader and BufWriter to perform less syscalls
Expand Down Expand Up @@ -508,7 +508,11 @@ fn check_mime_type(
// Try to detect the extension and warn the user if it differs from the written one
let outer_ext = format.iter().next().unwrap();
if outer_ext != &detected_format {
info!("The file extension: `{}` differ from the detected extension: `{}`", outer_ext, detected_format);
warning!(
"The file extension: `{}` differ from the detected extension: `{}`",
outer_ext,
detected_format
);
if !user_wants_to_continue_decompressing(path, question_policy)? {
return Ok(ControlFlow::Break(()));
}
Expand Down
16 changes: 16 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ pub fn _info_helper() {

print!("{}[INFO]{} ", *YELLOW, *RESET);
}

/// Macro that prints [WARNING] messages, wraps [`println`].
#[macro_export]
macro_rules! warning {
($($arg:tt)*) => {
$crate::macros::_warning_helper();
println!($($arg)*);
};
}

/// Helper to display "[INFO]", colored yellow
pub fn _warning_helper() {
use crate::utils::colors::{ORANGE, RESET};

print!("{}[WARNING]{} ", *ORANGE, *RESET);
}
7 changes: 5 additions & 2 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> Cow<'static, str> {
}

/// Try to detect the file extension by looking for known magic strings
/// Source: https://en.wikipedia.org/wiki/List_of_file_signatures
pub fn try_infer(path: &Path) -> Option<Extension> {
fn is_zip(buf: &[u8]) -> bool {
buf.len() > 3
Expand Down Expand Up @@ -124,8 +125,8 @@ pub fn try_infer(path: &Path) -> Option<Extension> {

let buf = {
let mut b = [0; 270];
// Reading errors will just make the inferring fail
let _rn = std::fs::File::open(&path).map(|mut f| std::io::Read::read(&mut f, &mut b));
// Reading errors will just make the inferring fail so its safe to ignore
let _ = std::fs::File::open(&path).map(|mut f| std::io::Read::read(&mut f, &mut b));
b
};

Expand Down Expand Up @@ -179,6 +180,8 @@ pub mod colors {
color!(RED = "\u{1b}[38;5;9m");
color!(WHITE = "\u{1b}[38;5;15m");
color!(YELLOW = "\u{1b}[38;5;11m");
// Requires true color support
color!(ORANGE = "\u{1b}[38;2;255;165;0m");
color!(STYLE_BOLD = "\u{1b}[1m");
color!(STYLE_RESET = "\u{1b}[0m");
color!(ALL_RESET = "\u{1b}[0;39m");
Expand Down

0 comments on commit 9eb07f0

Please sign in to comment.