From 0026e4d4de6d4fe72b0024b2892d4a09bdc9419a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20Miguel?= Date: Wed, 7 Apr 2021 02:02:50 -0300 Subject: [PATCH] Drop dependency on Colored --- Cargo.toml | 1 - src/commands.rs | 14 ++++++++------ src/compressors/bzip.rs | 7 ++++--- src/compressors/gzip.rs | 7 ++++--- src/compressors/lzma.rs | 7 ++++--- src/compressors/tar.rs | 7 ++++--- src/decompressors/tar.rs | 13 ++++++++----- src/decompressors/to_memory.rs | 8 +++++--- src/decompressors/zip.rs | 14 ++++++++------ src/dialogs.rs | 4 ++-- src/error.rs | 24 ++++++++++++------------ src/utils.rs | 19 ++++++++++--------- 12 files changed, 69 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6d4de99..3cb83f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ description = "A command-line utility for easily compressing and decompressing f # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -colored = "2.0.0" walkdir = "2.3.2" strsim = "0.10.0" flate2 = "1.0.14" diff --git a/src/commands.rs b/src/commands.rs index 2547226..ad5dee8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,7 +4,7 @@ use std::{ path::{Path, PathBuf}, }; -use colored::Colorize; +use utils::colors; use crate::{ cli::Command, @@ -88,8 +88,9 @@ fn get_decompressor(file: &File) -> crate::Result<(Option, Bo None => { // This block *should* be unreachable eprintln!( - "{} reached Evaluator::get_decompressor without known extension.", - "[internal error]".red() + "{}[internal error]{} reached Evaluator::get_decompressor without known extension.", + colors::red(), + colors::reset() ); return Err(crate::Error::InvalidInput); } @@ -143,7 +144,7 @@ fn decompress_file_in_memory( None => { // There is no more processing to be done on the input file (or there is but currently unsupported) // Therefore, we'll save what we have in memory into a file. - println!("{}: saving to {:?}.", "info".yellow(), file_name); + println!("{}[INFO]{} saving to {:?}.", colors::yellow(), colors::reset(), file_name); if file_name.exists() { let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); @@ -203,8 +204,9 @@ fn compress_files( }; println!( - "{}: writing to {:?}. ({})", - "info".yellow(), + "{}[INFO]{} writing to {:?}. ({})", + colors::yellow(), + colors::reset(), output_path, utils::Bytes::new(bytes.len() as u64) ); diff --git a/src/compressors/bzip.rs b/src/compressors/bzip.rs index a08c7c9..102de37 100644 --- a/src/compressors/bzip.rs +++ b/src/compressors/bzip.rs @@ -1,6 +1,6 @@ use std::{fs, io::Write, path::PathBuf}; -use colored::Colorize; +use utils::colors; use super::{Compressor, Entry}; use crate::{extension::CompressionFormat, file::File, utils}; @@ -18,8 +18,9 @@ impl BzipCompressor { }; println!( - "{}: compressed {:?} into memory ({})", - "info".yellow(), + "{}[INFO]{} compressed {:?} into memory ({})", + colors::yellow(), + colors::reset(), &path, utils::Bytes::new(contents.len() as u64) ); diff --git a/src/compressors/gzip.rs b/src/compressors/gzip.rs index 9399469..8e16970 100644 --- a/src/compressors/gzip.rs +++ b/src/compressors/gzip.rs @@ -1,6 +1,6 @@ use std::{fs, io::Write, path::PathBuf}; -use colored::Colorize; +use utils::colors; use super::{Compressor, Entry}; use crate::{extension::CompressionFormat, file::File, utils}; @@ -23,8 +23,9 @@ impl GzipCompressor { }; println!( - "{}: compressed {:?} into memory ({})", - "info".yellow(), + "{}[INFO]{} compressed {:?} into memory ({})", + colors::yellow(), + colors::reset(), &path, utils::Bytes::new(bytes.len() as u64) ); diff --git a/src/compressors/lzma.rs b/src/compressors/lzma.rs index d18fc0d..e8f1855 100644 --- a/src/compressors/lzma.rs +++ b/src/compressors/lzma.rs @@ -1,6 +1,6 @@ use std::{fs, io::Write, path::PathBuf}; -use colored::Colorize; +use utils::colors; use super::{Compressor, Entry}; use crate::{extension::CompressionFormat, file::File, utils}; @@ -23,8 +23,9 @@ impl LzmaCompressor { }; println!( - "{}: compressed {:?} into memory ({})", - "info".yellow(), + "{}[INFO]{} compressed {:?} into memory ({})", + colors::yellow(), + colors::reset(), &path, utils::Bytes::new(bytes.len() as u64) ); diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 0e0c417..60d891d 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -1,7 +1,7 @@ use std::{env, fs, path::PathBuf}; -use colored::Colorize; use tar::Builder; +use utils::colors; use walkdir::WalkDir; use super::compressor::Entry; @@ -13,8 +13,9 @@ impl TarCompressor { // TODO: implement this fn make_archive_from_memory(_input: File) -> crate::Result> { println!( - "{} .tar.tar and .zip.tar is currently unimplemented.", - "[ERROR]".red() + "{}[ERROR]{} .tar.tar and .zip.tar is currently unimplemented.", + colors::red(), + colors::reset() ); Err(crate::Error::InvalidZipArchive("")) } diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 7981750..4d91358 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -4,8 +4,9 @@ use std::{ path::{Path, PathBuf}, }; -use colored::Colorize; + use tar::{self, Archive}; +use utils::colors; use super::decompressor::{DecompressionResult, Decompressor}; use crate::{dialogs::Confirmation, file::File, utils}; @@ -16,8 +17,9 @@ pub struct TarDecompressor; impl TarDecompressor { fn unpack_files(from: File, into: &Path, flags: &oof::Flags) -> crate::Result> { println!( - "{}: attempting to decompress {:?}", - "ouch".bright_blue(), + "{}[INFO]{} attempting to decompress {:?}", + colors::blue(), + colors::reset(), &from.path ); let mut files_unpacked = vec![]; @@ -45,8 +47,9 @@ impl TarDecompressor { file.unpack_in(into)?; println!( - "{}: {:?} extracted. ({})", - "info".yellow(), + "{}[INFO]{} {:?} extracted. ({})", + colors::yellow(), + colors::reset(), into.join(file.path()?), utils::Bytes::new(file.size()) ); diff --git a/src/decompressors/to_memory.rs b/src/decompressors/to_memory.rs index a76e060..d7aaf63 100644 --- a/src/decompressors/to_memory.rs +++ b/src/decompressors/to_memory.rs @@ -3,7 +3,8 @@ use std::{ path::Path, }; -use colored::Colorize; + +use utils::colors; use super::decompressor::{DecompressionResult, Decompressor}; use crate::{extension::CompressionFormat, file::File, utils}; @@ -35,8 +36,9 @@ impl DecompressorToMemory { let bytes_read = reader.read_to_end(&mut buffer)?; println!( - "{}: {:?} extracted into memory ({}).", - "info".yellow(), + "{}[INFO]{} {:?} extracted into memory ({}).", + colors::yellow(), + colors::reset(), path, utils::Bytes::new(bytes_read as u64) ); diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index ac2120c..501f25d 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -4,7 +4,7 @@ use std::{ path::{Path, PathBuf}, }; -use colored::Colorize; +use utils::colors; use zip::{self, read::ZipFile, ZipArchive}; use super::decompressor::{DecompressionResult, Decompressor}; @@ -26,8 +26,9 @@ impl ZipDecompressor { let comment = file.comment(); if !comment.is_empty() { println!( - "{}: Comment in {}: {}", - "info".yellow(), + "{}[INFO]{} Comment in {}: {}", + colors::yellow(), + colors::reset(), file.name(), comment ); @@ -73,8 +74,9 @@ impl ZipDecompressor { } } println!( - "{}: \"{}\" extracted. ({})", - "info".yellow(), + "{}[INFO]{} \"{}\" extracted. ({})", + colors::yellow(), + colors::reset(), file_path.display(), utils::Bytes::new(file.size()) ); @@ -95,7 +97,7 @@ impl ZipDecompressor { } fn unpack_files(from: File, into: &Path, flags: &oof::Flags) -> crate::Result> { - println!("{} decompressing {:?}", "[OUCH]".bright_blue(), &from.path); + println!("{}[INFO]{} decompressing {:?}", colors::blue(), colors::reset(), &from.path); match from.contents_in_memory { Some(bytes) => { diff --git a/src/dialogs.rs b/src/dialogs.rs index 2902a41..645a432 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -1,6 +1,6 @@ use std::io::{self, Write}; -use colored::Colorize; +use crate::utils::colors; pub struct Confirmation<'a> { pub prompt: &'a str, @@ -26,7 +26,7 @@ impl<'a> Confirmation<'a> { }; loop { - print!("{} [{}/{}] ", message, "Y".bright_green(), "n".bright_red()); + print!("{} [{}Y{}/{}n{}] ", message, colors::green(), colors::reset(), colors::red(), colors::reset()); io::stdout().flush()?; let mut answer = String::new(); diff --git a/src/error.rs b/src/error.rs index 3b7c3e0..5656314 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,5 @@ use std::{fmt, path::PathBuf}; - -use colored::Colorize; +use crate::utils::colors; #[derive(PartialEq, Eq)] pub enum Error { @@ -33,7 +32,7 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::MissingExtensionError(filename) => { - write!(f, "{} ", "[ERROR]".red())?; + write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; // TODO: show MIME type of the unsupported file write!(f, "cannot compress to {:?}, likely because it has an unsupported (or missing) extension.", filename) } @@ -42,14 +41,14 @@ impl fmt::Display for Error { write!(f, "") } Error::FileNotFound(file) => { - write!(f, "{} ", "[ERROR]".red())?; + write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; if file == &PathBuf::from("") { return write!(f, "file not found!"); } write!(f, "file {:?} not found!", file) } Error::CompressingRootFolder => { - write!(f, "{} ", "[ERROR]".red())?; + write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; let spacing = " "; writeln!(f, "It seems you're trying to compress the root folder.")?; writeln!( @@ -59,21 +58,22 @@ impl fmt::Display for Error { )?; write!( f, - "{}Use a more appropriate tool for this, such as {}.", + "{}Use a more appropriate tool for this, such as {}rsync{}.", spacing, - "rsync".green() + colors::green(), + colors::reset() ) } Error::MissingArgumentsForCompression => { - write!(f, "{} ", "[ERROR]".red())?; + write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; let spacing = " "; writeln!(f,"The compress subcommands demands at least 2 arguments, an input file and an output file.")?; writeln!(f,"{}Example: `ouch compress img.jpeg img.zip", spacing)?; write!(f,"{}For more information, run `ouch --help`", spacing) } Error::InternalError => { - write!(f, "{} ", "[ERROR]".red())?; - write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}", "https://github.com/vrmiguel/ouch".green()) + write!(f, "{}[ERROR]{} ", colors::red(), colors::reset())?; + write!(f, "You've reached an internal error! This really should not have happened.\nPlease file an issue at {}https://github.com/vrmiguel/ouch{}", colors::green(), colors::reset()) } _err => { // TODO @@ -90,7 +90,7 @@ impl From for Error { std::io::ErrorKind::PermissionDenied => Self::PermissionDenied, std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, _other => { - println!("{} {}", "[IO error]".red(), err); + println!("{}[IO error]{} {}", colors::red(), colors::reset(), err); Self::IoError } } @@ -111,7 +111,7 @@ impl From for Error { impl From for Error { fn from(err: walkdir::Error) -> Self { - eprintln!("{} {}", "[ERROR]".red(), err); + eprintln!("{}[ERROR]{} {}", colors::red(), colors::reset(), err); Self::WalkdirError } } diff --git a/src/utils.rs b/src/utils.rs index a607180..3afc693 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,8 +5,6 @@ use std::{ path::{Path, PathBuf}, }; -use colored::Colorize; - use crate::{dialogs::Confirmation, extension::CompressionFormat, file::File}; #[macro_export] @@ -42,10 +40,11 @@ pub fn check_for_multiple_files( ) -> crate::Result<()> { if files.len() != 1 { eprintln!( - "{}: cannot compress multiple files directly to {:#?}.\n\ + "{}[ERROR]{} cannot compress multiple files directly to {:#?}.\n\ Try using an intermediate archival method such as Tar.\n\ Example: filename.tar{}", - "[ERROR]".red(), + colors::red(), + colors::reset(), format, format ); @@ -58,14 +57,16 @@ pub fn check_for_multiple_files( pub fn create_path_if_non_existent(path: &Path) -> crate::Result<()> { if !path.exists() { println!( - "{}: attempting to create folder {:?}.", - "[INFO]".yellow(), + "{}[INFO]{} attempting to create folder {:?}.", + colors::yellow(), + colors::reset(), &path ); std::fs::create_dir_all(path)?; println!( - "{}: directory {:#?} created.", - "[INFO]".yellow(), + "{}[INFO]{} directory {:#?} created.", + colors::yellow(), + colors::reset(), fs::canonicalize(&path)? ); } @@ -106,7 +107,7 @@ pub fn permission_for_overwriting( ) -> crate::Result { match (flags.is_present("yes"), flags.is_present("false")) { (true, true) => { - unreachable!("This shoul've been cutted out in the ~/src/cli.rs filter flags function.") + unreachable!("This should've been cutted out in the ~/src/cli.rs filter flags function.") } (true, _) => return Ok(true), (_, true) => return Ok(false), -- 2.11.4.GIT