diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 6f94270..010bb45 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -5,10 +5,12 @@ use std::{ }; use tar; -use utils::colors; use walkdir::WalkDir; -use crate::{oof, utils}; +use crate::{ + info, oof, + utils::{self, Bytes}, +}; pub fn unpack_archive(reader: Box, output_folder: &Path, flags: &oof::Flags) -> crate::Result> { let mut archive = tar::Archive::new(reader); @@ -24,13 +26,7 @@ pub fn unpack_archive(reader: Box, output_folder: &Path, flags: &oof:: file.unpack_in(output_folder)?; - println!( - "{}[INFO]{} {:?} extracted. ({})", - colors::yellow(), - colors::reset(), - output_folder.join(file.path()?), - utils::Bytes::new(file.size()) - ); + info!("{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size())); files_unpacked.push(file_path); } diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 62b4fda..28df9fd 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -8,8 +8,8 @@ use walkdir::WalkDir; use zip::{self, read::ZipFile, ZipArchive}; use crate::{ - oof, - utils::{self, colors}, + info, oof, + utils::{self, Bytes}, }; pub fn unpack_archive(mut archive: ZipArchive, into: &Path, flags: &oof::Flags) -> crate::Result> @@ -42,13 +42,8 @@ where fs::create_dir_all(&path)?; } } - println!( - "{}[INFO]{} \"{}\" extracted. ({})", - colors::yellow(), - colors::reset(), - file_path.display(), - utils::Bytes::new(file.size()) - ); + + info!("{:?} extracted. ({})", file_path.display(), Bytes::new(file.size())); let mut output_file = fs::File::create(&file_path)?; io::copy(&mut file, &mut output_file)?; @@ -116,7 +111,7 @@ where fn check_for_comments(file: &ZipFile) { let comment = file.comment(); if !comment.is_empty() { - println!("{}[INFO]{} Comment in {}: {}", colors::yellow(), colors::reset(), file.name(), comment); + info!("Found comment in {}: {}", file.name(), comment); } } diff --git a/src/commands.rs b/src/commands.rs index ca5f0d4..1e1a4b0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -14,7 +14,7 @@ use crate::{ self, CompressionFormat::{self, *}, }, - oof, utils, + info, oof, utils, utils::to_utf, }; @@ -94,12 +94,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> { eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = colors::reset(), red = colors::red()); } } else { - println!( - "{}[INFO]{} Successfully compressed '{}'.", - colors::yellow(), - colors::reset(), - to_utf(output_path), - ); + info!("Successfully compressed '{}'.", to_utf(output_path)); } compress_result?; @@ -243,7 +238,7 @@ fn decompress_file( utils::create_dir_if_non_existent(output_folder)?; let zip_archive = zip::ZipArchive::new(reader)?; let _files = crate::archive::zip::unpack_archive(zip_archive, output_folder, flags)?; - println!("[INFO]: Successfully uncompressed bundle at '{}'.", to_utf(output_folder)); + info!("Successfully uncompressed bundle in '{}'.", to_utf(output_folder)); return Ok(()); } @@ -274,12 +269,12 @@ fn decompress_file( let mut writer = fs::File::create(&output_path)?; io::copy(&mut reader, &mut writer)?; - println!("[INFO]: Successfully uncompressed file at '{}'.", to_utf(output_path)); + info!("Successfully uncompressed bundle in '{}'.", to_utf(output_path)); } Tar => { utils::create_dir_if_non_existent(output_folder)?; let _ = crate::archive::tar::unpack_archive(reader, output_folder, flags)?; - println!("[INFO]: Successfully uncompressed bundle at '{}'.", to_utf(output_folder)); + info!("Successfully uncompressed bundle in '{}'.", to_utf(output_folder)); } Zip => { utils::create_dir_if_non_existent(output_folder)?; @@ -297,7 +292,7 @@ fn decompress_file( let _ = crate::archive::zip::unpack_archive(zip_archive, output_folder, flags)?; - println!("[INFO]: Successfully uncompressed bundle at '{}'.", to_utf(output_folder)); + info!("Successfully uncompressed bundle in '{}'.", to_utf(output_folder)); } } diff --git a/src/dialogs.rs b/src/dialogs.rs index d434907..714dcc1 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -8,7 +8,6 @@ pub struct Confirmation<'a> { pub placeholder: Option<&'a str>, } - impl<'a> Confirmation<'a> { pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { Self { prompt, placeholder: pattern } diff --git a/src/lib.rs b/src/lib.rs index 02d4a9e..2a1e4d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub mod archive; mod dialogs; mod error; mod extension; +mod macros; mod utils; pub use error::{Error, Result}; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..eb3fb02 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,13 @@ +#[macro_export] +macro_rules! info { + + ($writer:expr, $($arg:tt)*) => { + use crate::utils::colors::{reset, yellow}; + print!("{}[INFO]{} ", yellow(), reset()); + println!($writer, $($arg)*); + }; + ($writer:expr) => { + print!("{}[INFO]{} ", yellow(), reset()); + println!($writer); + }; +} diff --git a/src/utils.rs b/src/utils.rs index f0485e5..e0cbcaf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,13 +5,12 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{dialogs::Confirmation, oof}; +use crate::{dialogs::Confirmation, info, oof}; pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> { if !path.exists() { - println!("{}[INFO]{} attempting to create folder {:?}.", colors::yellow(), colors::reset(), &path); fs::create_dir_all(path)?; - println!("{}[INFO]{} directory {:#?} created.", colors::yellow(), colors::reset(), fs::canonicalize(&path)?); + info!("directory {:#?} created.", to_utf(path)); } Ok(()) }