mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
refactor: better NO_COLOR support
This commit is contained in:
parent
16fbebe8fe
commit
161b8d0d66
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -155,12 +155,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.103"
|
||||
@ -199,6 +193,12 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||
|
||||
[[package]]
|
||||
name = "ouch"
|
||||
version = "0.2.0"
|
||||
@ -207,8 +207,8 @@ dependencies = [
|
||||
"bzip2",
|
||||
"flate2",
|
||||
"infer",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"once_cell",
|
||||
"rand",
|
||||
"strsim",
|
||||
"tar",
|
||||
|
@ -14,7 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f
|
||||
|
||||
[dependencies]
|
||||
atty = "0.2.14"
|
||||
lazy_static = "1.4.0"
|
||||
once_cell = "1.8.0"
|
||||
walkdir = "2.3.2"
|
||||
strsim = "0.10.0"
|
||||
bzip2 = "0.4.3"
|
||||
|
@ -109,10 +109,10 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
||||
// Print an extra alert message pointing out that we left a possibly
|
||||
// CORRUPTED FILE at `output_path`
|
||||
if let Err(err) = fs::remove_file(&output_path) {
|
||||
eprintln!("{red}FATAL ERROR:\n", red = colors::red());
|
||||
eprintln!("{red}FATAL ERROR:\n", red = *colors::RED);
|
||||
eprintln!(" Please manually delete '{}'.", to_utf(&output_path));
|
||||
eprintln!(" Compression failed and we could not delete '{}'.", to_utf(&output_path),);
|
||||
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 {
|
||||
info!("Successfully compressed '{}'.", to_utf(output_path));
|
||||
@ -229,7 +229,7 @@ fn compress_files(
|
||||
writer.flush()?;
|
||||
}
|
||||
Zip => {
|
||||
eprintln!("{yellow}Warning:{reset}", yellow = colors::yellow(), reset = colors::reset());
|
||||
eprintln!("{yellow}Warning:{reset}", yellow = *colors::YELLOW, reset = *colors::RESET);
|
||||
eprintln!("\tCompressing .zip entirely in memory.");
|
||||
eprintln!("\tIf the file is too big, your PC might freeze!");
|
||||
eprintln!(
|
||||
|
@ -29,7 +29,7 @@ impl<'a> Confirmation<'a> {
|
||||
};
|
||||
|
||||
loop {
|
||||
print!("{} [{}Y{}/{}n{}] ", message, colors::green(), colors::reset(), colors::red(), colors::reset());
|
||||
print!("{} [{}Y{}/{}n{}] ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET);
|
||||
io::stdout().flush()?;
|
||||
|
||||
let mut answer = String::new();
|
||||
|
12
src/error.rs
12
src/error.rs
@ -45,11 +45,11 @@ pub struct FinalError {
|
||||
impl Display for FinalError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// Title
|
||||
writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?;
|
||||
writeln!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?;
|
||||
|
||||
// Details
|
||||
for detail in &self.details {
|
||||
writeln!(f, " {}-{} {}", white(), yellow(), detail)?;
|
||||
writeln!(f, " {}-{} {}", *WHITE, *YELLOW, detail)?;
|
||||
}
|
||||
|
||||
// Hints
|
||||
@ -57,11 +57,11 @@ impl Display for FinalError {
|
||||
// Separate by one blank line.
|
||||
writeln!(f)?;
|
||||
for hint in &self.hints {
|
||||
writeln!(f, "{}hint:{} {}", green(), reset(), hint)?;
|
||||
writeln!(f, "{}hint:{} {}", *GREEN, *RESET, hint)?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "{}", reset())
|
||||
write!(f, "{}", *RESET)
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,7 +139,7 @@ impl fmt::Display for Error {
|
||||
.detail("This should not have happened")
|
||||
.detail("It's probably our fault")
|
||||
.detail("Please help us improve by reporting the issue at:")
|
||||
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()))
|
||||
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", *CYAN))
|
||||
.clone();
|
||||
|
||||
error
|
||||
@ -147,7 +147,7 @@ impl fmt::Display for Error {
|
||||
Error::OofError(err) => FinalError::with_title(err),
|
||||
Error::IoError { reason } => FinalError::with_title(reason),
|
||||
Error::CompressionTypo => FinalError::with_title("Possible typo detected")
|
||||
.hint(format!("Did you mean '{}ouch compress{}'?", magenta(), reset()))
|
||||
.hint(format!("Did you mean '{}ouch compress{}'?", *MAGENTA, *RESET))
|
||||
.clone(),
|
||||
Error::UnknownExtensionError(_) => todo!(),
|
||||
Error::AlreadyExists => todo!(),
|
||||
|
24
src/lib.rs
24
src/lib.rs
@ -19,21 +19,11 @@ mod utils;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
/// The status code ouch has when an error is encountered
|
||||
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
lazy_static! {
|
||||
static ref NO_COLOR_IS_SET: bool = {
|
||||
use std::env;
|
||||
|
||||
env::var("NO_COLOR").is_ok() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr)
|
||||
};
|
||||
}
|
||||
|
||||
fn help_command() {
|
||||
use utils::colors::*;
|
||||
|
||||
@ -62,17 +52,17 @@ fn help_command() {
|
||||
another folder.
|
||||
|
||||
Visit https://github.com/ouch-org/ouch for more usage examples.",
|
||||
magenta = magenta(),
|
||||
white = white(),
|
||||
green = green(),
|
||||
yellow = yellow(),
|
||||
reset = reset(),
|
||||
cyan = cyan()
|
||||
magenta = *MAGENTA,
|
||||
white = *WHITE,
|
||||
green = *GREEN,
|
||||
yellow = *YELLOW,
|
||||
reset = *RESET,
|
||||
cyan = *CYAN
|
||||
);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn version_command() {
|
||||
use utils::colors::*;
|
||||
println!("{green}ouch{reset} {}", crate::VERSION, green = green(), reset = reset());
|
||||
println!("{green}ouch{reset} {}", crate::VERSION, green = *GREEN, reset = *RESET);
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
use crate::NO_COLOR_IS_SET;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($writer:expr, $($arg:tt)*) => {
|
||||
@ -14,11 +12,7 @@ macro_rules! info {
|
||||
}
|
||||
|
||||
pub fn _info_helper() {
|
||||
use crate::utils::colors::{reset, yellow};
|
||||
use crate::utils::colors::{RESET, YELLOW};
|
||||
|
||||
if *NO_COLOR_IS_SET {
|
||||
print!("[INFO] ");
|
||||
} else {
|
||||
print!("{}[INFO]{} ", yellow(), reset());
|
||||
}
|
||||
print!("{}[INFO]{} ", *YELLOW, *RESET);
|
||||
}
|
||||
|
67
src/utils.rs
67
src/utils.rs
@ -64,52 +64,31 @@ pub struct Bytes {
|
||||
|
||||
/// Module with a list of bright colors.
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_family = "unix")]
|
||||
pub mod colors {
|
||||
pub const fn reset() -> &'static str {
|
||||
"\u{1b}[39m"
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static NO_COLOR_IS_SET: Lazy<bool> = Lazy::new(|| {
|
||||
std::env::var_os("NO_COLOR").is_some() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr)
|
||||
});
|
||||
|
||||
macro_rules! color {
|
||||
($name:ident = $value:literal) => {
|
||||
#[cfg(target_family = "unix")]
|
||||
pub static $name: Lazy<&str> = Lazy::new(|| if *NO_COLOR_IS_SET { "" } else { $value });
|
||||
#[cfg(not(target_family = "unix"))]
|
||||
pub static $name: &&str = &"";
|
||||
};
|
||||
}
|
||||
pub const fn black() -> &'static str {
|
||||
"\u{1b}[38;5;8m"
|
||||
}
|
||||
pub const fn blue() -> &'static str {
|
||||
"\u{1b}[38;5;12m"
|
||||
}
|
||||
pub const fn cyan() -> &'static str {
|
||||
"\u{1b}[38;5;14m"
|
||||
}
|
||||
pub const fn green() -> &'static str {
|
||||
"\u{1b}[38;5;10m"
|
||||
}
|
||||
pub const fn magenta() -> &'static str {
|
||||
"\u{1b}[38;5;13m"
|
||||
}
|
||||
pub const fn red() -> &'static str {
|
||||
"\u{1b}[38;5;9m"
|
||||
}
|
||||
pub const fn white() -> &'static str {
|
||||
"\u{1b}[38;5;15m"
|
||||
}
|
||||
pub const fn yellow() -> &'static str {
|
||||
"\u{1b}[38;5;11m"
|
||||
}
|
||||
}
|
||||
// Windows does not support ANSI escape codes
|
||||
#[allow(dead_code, non_upper_case_globals)]
|
||||
#[cfg(not(target_family = "unix"))]
|
||||
pub mod colors {
|
||||
pub const fn empty() -> &'static str {
|
||||
""
|
||||
}
|
||||
pub const reset: fn() -> &'static str = empty;
|
||||
pub const black: fn() -> &'static str = empty;
|
||||
pub const blue: fn() -> &'static str = empty;
|
||||
pub const cyan: fn() -> &'static str = empty;
|
||||
pub const green: fn() -> &'static str = empty;
|
||||
pub const magenta: fn() -> &'static str = empty;
|
||||
pub const red: fn() -> &'static str = empty;
|
||||
pub const white: fn() -> &'static str = empty;
|
||||
pub const yellow: fn() -> &'static str = empty;
|
||||
|
||||
color!(RESET = "\u{1b}[39m");
|
||||
color!(BLACK = "\u{1b}[38;5;8m");
|
||||
color!(BLUE = "\u{1b}[38;5;12m");
|
||||
color!(CYAN = "\u{1b}[38;5;14m");
|
||||
color!(GREEN = "\u{1b}[38;5;10m");
|
||||
color!(MAGENTA = "\u{1b}[38;5;13m");
|
||||
color!(RED = "\u{1b}[38;5;9m");
|
||||
color!(WHITE = "\u{1b}[38;5;15m");
|
||||
color!(YELLOW = "\u{1b}[38;5;11m");
|
||||
}
|
||||
|
||||
impl Bytes {
|
||||
|
Loading…
x
Reference in New Issue
Block a user