mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
Drop dependency on Colored
This commit is contained in:
parent
8a4ac5d6d1
commit
0026e4d4de
@ -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"
|
||||
|
@ -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<BoxedDecompressor>, 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)
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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<Vec<u8>> {
|
||||
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(""))
|
||||
}
|
||||
|
@ -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<Vec<PathBuf>> {
|
||||
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())
|
||||
);
|
||||
|
@ -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)
|
||||
);
|
||||
|
@ -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<Vec<PathBuf>> {
|
||||
println!("{} decompressing {:?}", "[OUCH]".bright_blue(), &from.path);
|
||||
println!("{}[INFO]{} decompressing {:?}", colors::blue(), colors::reset(), &from.path);
|
||||
|
||||
match from.contents_in_memory {
|
||||
Some(bytes) => {
|
||||
|
@ -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();
|
||||
|
24
src/error.rs
24
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<std::io::Error> 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<zip::result::ZipError> for Error {
|
||||
|
||||
impl From<walkdir::Error> for Error {
|
||||
fn from(err: walkdir::Error) -> Self {
|
||||
eprintln!("{} {}", "[ERROR]".red(), err);
|
||||
eprintln!("{}[ERROR]{} {}", colors::red(), colors::reset(), err);
|
||||
Self::WalkdirError
|
||||
}
|
||||
}
|
||||
|
19
src/utils.rs
19
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<bool> {
|
||||
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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user