mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-08 20:45:29 +00:00
Minor formatting and naming changes
This commit is contained in:
parent
588359e9e5
commit
fc2e38a182
@ -33,13 +33,13 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
|||||||
let formats = extension::extensions_from_path(&output_path);
|
let formats = extension::extensions_from_path(&output_path);
|
||||||
|
|
||||||
if formats.is_empty() {
|
if formats.is_empty() {
|
||||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||||
.detail("You shall supply the compression format via the extension.")
|
.detail("You shall supply the compression format via the extension.")
|
||||||
.hint("Try adding something like .tar.gz or .zip to the output file.")
|
.hint("Try adding something like .tar.gz or .zip to the output file.")
|
||||||
.hint("")
|
.hint("")
|
||||||
.hint("Examples:")
|
.hint("Examples:")
|
||||||
.hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
|
.hint(f!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
|
||||||
.hint(format!(" ouch compress ... {}.zip", to_utf(&output_path)));
|
.hint(f!(" ouch compress ... {}.zip", to_utf(&output_path)));
|
||||||
|
|
||||||
return Err(Error::with_reason(reason));
|
return Err(Error::with_reason(reason));
|
||||||
}
|
}
|
||||||
@ -61,23 +61,23 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
|||||||
let mut suggested_output_path = output_path.clone();
|
let mut suggested_output_path = output_path.clone();
|
||||||
suggested_output_path.replace_range(empty_range, ".tar");
|
suggested_output_path.replace_range(empty_range, ".tar");
|
||||||
|
|
||||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||||
.detail("You are trying to compress multiple files.")
|
.detail("You are trying to compress multiple files.")
|
||||||
.detail(format!("The compression format '{}' cannot receive multiple files.", &formats[0]))
|
.detail(f!("The compression format '{}' cannot receive multiple files.", &formats[0]))
|
||||||
.detail("The only supported formats that archive files into an archive are .tar and .zip.")
|
.detail("The only supported formats that archive files into an archive are .tar and .zip.")
|
||||||
.hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
|
.hint(f!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
|
||||||
.hint(format!("From: {}", output_path))
|
.hint(f!("From: {}", output_path))
|
||||||
.hint(format!(" To : {}", suggested_output_path));
|
.hint(f!(" To : {}", suggested_output_path));
|
||||||
|
|
||||||
return Err(Error::with_reason(reason));
|
return Err(Error::with_reason(reason));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(format) = formats.iter().skip(1).position(|format| matches!(format, Tar | Zip)) {
|
if let Some(format) = formats.iter().skip(1).position(|format| matches!(format, Tar | Zip)) {
|
||||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||||
.detail(format!("Found the format '{}' in an incorrect position.", format))
|
.detail(f!("Found the format '{}' in an incorrect position.", format))
|
||||||
.detail(format!("{} can only be used at the start of the file extension.", format))
|
.detail(f!("{} can only be used at the start of the file extension.", format))
|
||||||
.hint(format!("If you wish to compress multiple files, start the extension with {}.", format))
|
.hint(f!("If you wish to compress multiple files, start the extension with {}.", format))
|
||||||
.hint(format!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
|
.hint(f!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
|
||||||
|
|
||||||
return Err(Error::with_reason(reason));
|
return Err(Error::with_reason(reason));
|
||||||
}
|
}
|
||||||
@ -88,12 +88,11 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let output_file = fs::File::create(&output_path).map_err(|err| {
|
let output_file = fs::File::create(&output_path).map_err(|err| {
|
||||||
FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||||
.detail(format!("Could not open file '{}' for writing.", to_utf(&output_path)))
|
.detail(f!("Could not open file '{}' for writing.", to_utf(&output_path)))
|
||||||
.detail(format!("Error: {}.", err))
|
.detail(f!("Error: {}.", err))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// let output_file = fs::File::create(&output_path)?;
|
|
||||||
let compress_result = compress_files(files, formats, output_file, flags);
|
let compress_result = compress_files(files, formats, output_file, flags);
|
||||||
|
|
||||||
// If any error occurred, delete incomplete file
|
// If any error occurred, delete incomplete file
|
||||||
|
50
src/error.rs
50
src/error.rs
@ -43,7 +43,7 @@ pub struct FinalError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Display for FinalError {
|
impl Display for FinalError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
// Title
|
// Title
|
||||||
writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?;
|
writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?;
|
||||||
|
|
||||||
@ -79,73 +79,69 @@ impl FinalError {
|
|||||||
self.hints.push(hint.to_string());
|
self.hints.push(hint.to_string());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// // In case we want to use mutable references in the future
|
||||||
|
// pub fn detail_ref(&mut self, detail: impl ToString) -> Self;
|
||||||
|
// pub fn hint_ref(&mut self, detail: impl ToString) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let err = match self {
|
let err = match self {
|
||||||
Error::MissingExtensionError(filename) => {
|
Error::MissingExtensionError(filename) => {
|
||||||
let error = FinalError::with_title(format!("Cannot compress to {:?}", filename))
|
FinalError::with_title(f!("Cannot compress to {:?}", filename))
|
||||||
.detail("Ouch could not detect the compression format")
|
.detail("Ouch could not detect the compression format")
|
||||||
.hint("Use a supported format extension, like '.zip' or '.tar.gz'")
|
.hint("Use a supported format extension, like '.zip' or '.tar.gz'")
|
||||||
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats");
|
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats")
|
||||||
|
|
||||||
error
|
|
||||||
}
|
}
|
||||||
Error::WalkdirError { reason } => FinalError::with_title(reason),
|
Error::WalkdirError { reason } => FinalError::with_title(reason),
|
||||||
Error::FileNotFound(file) => {
|
Error::FileNotFound(file) => {
|
||||||
let error = if file == Path::new("") {
|
let error = if file == Path::new("") {
|
||||||
FinalError::with_title("file not found!")
|
FinalError::with_title("file not found!")
|
||||||
} else {
|
} else {
|
||||||
FinalError::with_title(format!("file {:?} not found!", file))
|
FinalError::with_title(f!("file {:?} not found!", file))
|
||||||
};
|
};
|
||||||
|
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
Error::CompressingRootFolder => {
|
Error::CompressingRootFolder => {
|
||||||
let error = FinalError::with_title("It seems you're trying to compress the root folder.")
|
FinalError::with_title("It seems you're trying to compress the root folder.")
|
||||||
.detail("This is unadvisable since ouch does compressions in-memory.")
|
.detail("This is unadvisable since ouch does compressions in-memory.")
|
||||||
.hint("Use a more appropriate tool for this, such as rsync.");
|
.hint("Use a more appropriate tool for this, such as rsync.")
|
||||||
|
|
||||||
error
|
|
||||||
}
|
}
|
||||||
Error::MissingArgumentsForCompression => {
|
Error::MissingArgumentsForCompression => {
|
||||||
let error = FinalError::with_title("Could not compress")
|
FinalError::with_title("Could not compress")
|
||||||
.detail("The compress command requires at least 2 arguments")
|
.detail("The compress command requires at least 2 arguments")
|
||||||
.hint("You must provide:")
|
.hint("You must provide:")
|
||||||
.hint(" - At least one input argument.")
|
.hint(" - At least one input argument.")
|
||||||
.hint(" - The output argument.")
|
.hint(" - The output argument.")
|
||||||
.hint("")
|
.hint("")
|
||||||
.hint("Example: `ouch compress image.png img.zip`");
|
.hint("Example: `ouch compress image.png img.zip`")
|
||||||
|
|
||||||
error
|
|
||||||
}
|
}
|
||||||
Error::MissingArgumentsForDecompression => {
|
Error::MissingArgumentsForDecompression => {
|
||||||
let error = FinalError::with_title("Could not decompress")
|
FinalError::with_title("Could not decompress")
|
||||||
.detail("The compress command requires at least one argument")
|
.detail("The compress command requires at least one argument")
|
||||||
.hint("You must provide:")
|
.hint("You must provide:")
|
||||||
.hint(" - At least one input argument.")
|
.hint(" - At least one input argument.")
|
||||||
.hint("")
|
.hint("")
|
||||||
.hint("Example: `ouch decompress imgs.tar.gz`");
|
.hint("Example: `ouch decompress imgs.tar.gz`")
|
||||||
|
|
||||||
error
|
|
||||||
}
|
}
|
||||||
Error::InternalError => {
|
Error::InternalError => {
|
||||||
let error = FinalError::with_title("InternalError :(")
|
FinalError::with_title("InternalError :(")
|
||||||
.detail("This should not have happened")
|
.detail("This should not have happened")
|
||||||
.detail("It's probably our fault")
|
.detail("It's probably our fault")
|
||||||
.detail("Please help us improve by reporting the issue at:")
|
.detail("Please help us improve by reporting the issue at:")
|
||||||
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()));
|
.detail(f!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()))
|
||||||
|
|
||||||
error
|
|
||||||
}
|
}
|
||||||
Error::OofError(err) => FinalError::with_title(err),
|
Error::OofError(err) => FinalError::with_title(err),
|
||||||
Error::IoError { reason } => FinalError::with_title(reason),
|
Error::IoError { reason } => FinalError::with_title(reason),
|
||||||
Error::CompressionTypo => FinalError::with_title("Possible typo detected").hint(format!(
|
Error::CompressionTypo => {
|
||||||
"Did you mean '{}ouch compress{}'?",
|
FinalError::with_title("Possible typo detected").hint(f!(
|
||||||
magenta(),
|
"Did you mean '{}ouch compress{}'?",
|
||||||
reset()
|
magenta(),
|
||||||
)),
|
reset()
|
||||||
|
))
|
||||||
|
}
|
||||||
_err => {
|
_err => {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
//! 1. It's required by `main.rs`, or
|
//! 1. It's required by `main.rs`, or
|
||||||
//! 2. It's required by some integration tests at tests/ folder.
|
//! 2. It's required by some integration tests at tests/ folder.
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
// Public modules
|
// Public modules
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
@ -14,7 +17,6 @@ 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};
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
use crate::NO_COLOR_IS_SET;
|
use crate::NO_COLOR_IS_SET;
|
||||||
|
|
||||||
|
// f!() is an alias to f!()
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! f {
|
||||||
|
{ $($tokens:tt)* } => {
|
||||||
|
format!( $($tokens)* )
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! info {
|
macro_rules! info {
|
||||||
($writer:expr, $($arg:tt)*) => {
|
($writer:expr, $($arg:tt)*) => {
|
||||||
|
@ -31,18 +31,22 @@ impl fmt::Display for OofError {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
// TODO: implement proper debug messages
|
// TODO: implement proper debug messages
|
||||||
match self {
|
match self {
|
||||||
OofError::FlagValueConflict { flag, previous_value, new_value } => write!(
|
OofError::FlagValueConflict { flag, previous_value, new_value } => {
|
||||||
f,
|
write!(
|
||||||
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
|
f,
|
||||||
flag.long, previous_value, new_value
|
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
|
||||||
),
|
flag.long, previous_value, new_value
|
||||||
|
)
|
||||||
|
}
|
||||||
OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag),
|
OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag),
|
||||||
OofError::UnknownShortFlag(ch) => write!(f, "Unknown argument '-{}'", ch),
|
OofError::UnknownShortFlag(ch) => write!(f, "Unknown argument '-{}'", ch),
|
||||||
OofError::MisplacedShortArgFlagError(ch) => write!(
|
OofError::MisplacedShortArgFlagError(ch) => {
|
||||||
f,
|
write!(
|
||||||
"Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.",
|
f,
|
||||||
ch
|
"Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.",
|
||||||
),
|
ch
|
||||||
|
)
|
||||||
|
}
|
||||||
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag),
|
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag),
|
||||||
OofError::DuplicatedFlag(flag) => write!(f, "Duplicated usage of {}.", flag),
|
OofError::DuplicatedFlag(flag) => write!(f, "Duplicated usage of {}.", flag),
|
||||||
OofError::UnknownLongFlag(flag) => write!(f, "Unknown argument '--{}'", flag),
|
OofError::UnknownLongFlag(flag) => write!(f, "Unknown argument '--{}'", flag),
|
||||||
|
@ -26,7 +26,7 @@ pub struct Flag {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Flag {
|
impl std::fmt::Display for Flag {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self.short {
|
match self.short {
|
||||||
Some(short_flag) => write!(f, "-{}/--{}", short_flag, self.long),
|
Some(short_flag) => write!(f, "-{}/--{}", short_flag, self.long),
|
||||||
None => write!(f, "--{}", self.long),
|
None => write!(f, "--{}", self.long),
|
||||||
|
@ -47,7 +47,7 @@ pub fn user_wants_to_overwrite(path: &Path, flags: &oof::Flags) -> crate::Result
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
|
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
|
||||||
let text = format!("{:?}", os_str.as_ref());
|
let text = f!("{:?}", os_str.as_ref());
|
||||||
text.trim_matches('"').to_string()
|
text.trim_matches('"').to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +114,7 @@ impl Bytes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Bytes {
|
impl std::fmt::Display for Bytes {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
let num = self.bytes;
|
let num = self.bytes;
|
||||||
debug_assert!(num >= 0.0);
|
debug_assert!(num >= 0.0);
|
||||||
if num < 1_f64 {
|
if num < 1_f64 {
|
||||||
@ -135,7 +135,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_pretty_bytes_formatting() {
|
fn test_pretty_bytes_formatting() {
|
||||||
fn format_bytes(bytes: u64) -> String {
|
fn format_bytes(bytes: u64) -> String {
|
||||||
format!("{}", Bytes::new(bytes))
|
f!("{}", Bytes::new(bytes))
|
||||||
}
|
}
|
||||||
let b = 1;
|
let b = 1;
|
||||||
let kb = b * 1000;
|
let kb = b * 1000;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user