Reduced repetition caused by [INFO] printing

This commit is contained in:
Vinícius Rodrigues Miguel 2021-09-17 00:22:41 -03:00
parent c4a4792816
commit 55aa65dcea
7 changed files with 32 additions and 34 deletions

View File

@ -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<dyn Read>, output_folder: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>> {
let mut archive = tar::Archive::new(reader);
@ -24,13 +26,7 @@ pub fn unpack_archive(reader: Box<dyn Read>, 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);
}

View File

@ -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<R>(mut archive: ZipArchive<R>, into: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>>
@ -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);
}
}

View File

@ -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));
}
}

View File

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

View File

@ -8,6 +8,7 @@ pub mod archive;
mod dialogs;
mod error;
mod extension;
mod macros;
mod utils;
pub use error::{Error, Result};

13
src/macros.rs Normal file
View File

@ -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);
};
}

View File

@ -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(())
}