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 tar;
use utils::colors;
use walkdir::WalkDir; 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>> { 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); 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)?; file.unpack_in(output_folder)?;
println!( info!("{:?} extracted. ({})", output_folder.join(file.path()?), Bytes::new(file.size()));
"{}[INFO]{} {:?} extracted. ({})",
colors::yellow(),
colors::reset(),
output_folder.join(file.path()?),
utils::Bytes::new(file.size())
);
files_unpacked.push(file_path); files_unpacked.push(file_path);
} }

View File

@ -8,8 +8,8 @@ use walkdir::WalkDir;
use zip::{self, read::ZipFile, ZipArchive}; use zip::{self, read::ZipFile, ZipArchive};
use crate::{ use crate::{
oof, info, oof,
utils::{self, colors}, utils::{self, Bytes},
}; };
pub fn unpack_archive<R>(mut archive: ZipArchive<R>, into: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>> 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)?; fs::create_dir_all(&path)?;
} }
} }
println!(
"{}[INFO]{} \"{}\" extracted. ({})", info!("{:?} extracted. ({})", file_path.display(), Bytes::new(file.size()));
colors::yellow(),
colors::reset(),
file_path.display(),
utils::Bytes::new(file.size())
);
let mut output_file = fs::File::create(&file_path)?; let mut output_file = fs::File::create(&file_path)?;
io::copy(&mut file, &mut output_file)?; io::copy(&mut file, &mut output_file)?;
@ -116,7 +111,7 @@ where
fn check_for_comments(file: &ZipFile) { fn check_for_comments(file: &ZipFile) {
let comment = file.comment(); let comment = file.comment();
if !comment.is_empty() { 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, self,
CompressionFormat::{self, *}, CompressionFormat::{self, *},
}, },
oof, utils, info, oof, utils,
utils::to_utf, 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()); eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = colors::reset(), red = colors::red());
} }
} else { } else {
println!( info!("Successfully compressed '{}'.", to_utf(output_path));
"{}[INFO]{} Successfully compressed '{}'.",
colors::yellow(),
colors::reset(),
to_utf(output_path),
);
} }
compress_result?; compress_result?;
@ -243,7 +238,7 @@ fn decompress_file(
utils::create_dir_if_non_existent(output_folder)?; utils::create_dir_if_non_existent(output_folder)?;
let zip_archive = zip::ZipArchive::new(reader)?; let zip_archive = zip::ZipArchive::new(reader)?;
let _files = crate::archive::zip::unpack_archive(zip_archive, output_folder, flags)?; 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(()); return Ok(());
} }
@ -274,12 +269,12 @@ fn decompress_file(
let mut writer = fs::File::create(&output_path)?; let mut writer = fs::File::create(&output_path)?;
io::copy(&mut reader, &mut writer)?; 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 => { Tar => {
utils::create_dir_if_non_existent(output_folder)?; 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)?;
println!("[INFO]: Successfully uncompressed bundle at '{}'.", to_utf(output_folder)); info!("Successfully uncompressed bundle in '{}'.", to_utf(output_folder));
} }
Zip => { Zip => {
utils::create_dir_if_non_existent(output_folder)?; 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)?; 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>, pub placeholder: Option<&'a str>,
} }
impl<'a> Confirmation<'a> { impl<'a> Confirmation<'a> {
pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
Self { prompt, placeholder: pattern } Self { prompt, placeholder: pattern }

View File

@ -8,6 +8,7 @@ pub mod archive;
mod dialogs; mod dialogs;
mod error; mod error;
mod extension; mod extension;
mod macros;
mod utils; mod utils;
pub use error::{Error, Result}; 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}, 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<()> { pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
if !path.exists() { if !path.exists() {
println!("{}[INFO]{} attempting to create folder {:?}.", colors::yellow(), colors::reset(), &path);
fs::create_dir_all(path)?; fs::create_dir_all(path)?;
println!("{}[INFO]{} directory {:#?} created.", colors::yellow(), colors::reset(), fs::canonicalize(&path)?); info!("directory {:#?} created.", to_utf(path));
} }
Ok(()) Ok(())
} }