mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 19:45:29 +00:00
Merge pull request #95 from figsoda/refactor-colors
refactor: better NO_COLOR support
This commit is contained in:
commit
8224aabb2a
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -155,12 +155,6 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazy_static"
|
|
||||||
version = "1.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.103"
|
version = "0.2.103"
|
||||||
@ -199,6 +193,12 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ouch"
|
name = "ouch"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@ -207,8 +207,8 @@ dependencies = [
|
|||||||
"bzip2",
|
"bzip2",
|
||||||
"flate2",
|
"flate2",
|
||||||
"infer",
|
"infer",
|
||||||
"lazy_static",
|
|
||||||
"libc",
|
"libc",
|
||||||
|
"once_cell",
|
||||||
"rand",
|
"rand",
|
||||||
"strsim",
|
"strsim",
|
||||||
"tar",
|
"tar",
|
||||||
|
@ -14,7 +14,7 @@ description = "A command-line utility for easily compressing and decompressing f
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
atty = "0.2.14"
|
atty = "0.2.14"
|
||||||
lazy_static = "1.4.0"
|
once_cell = "1.8.0"
|
||||||
walkdir = "2.3.2"
|
walkdir = "2.3.2"
|
||||||
strsim = "0.10.0"
|
strsim = "0.10.0"
|
||||||
bzip2 = "0.4.3"
|
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
|
// Print an extra alert message pointing out that we left a possibly
|
||||||
// CORRUPTED FILE at `output_path`
|
// CORRUPTED FILE at `output_path`
|
||||||
if let Err(err) = fs::remove_file(&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!(" Please manually delete '{}'.", to_utf(&output_path));
|
||||||
eprintln!(" Compression failed and we could not 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 {
|
} else {
|
||||||
info!("Successfully compressed '{}'.", to_utf(output_path));
|
info!("Successfully compressed '{}'.", to_utf(output_path));
|
||||||
@ -229,7 +229,7 @@ fn compress_files(
|
|||||||
writer.flush()?;
|
writer.flush()?;
|
||||||
}
|
}
|
||||||
Zip => {
|
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!("\tCompressing .zip entirely in memory.");
|
||||||
eprintln!("\tIf the file is too big, your PC might freeze!");
|
eprintln!("\tIf the file is too big, your PC might freeze!");
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
@ -29,7 +29,7 @@ impl<'a> Confirmation<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
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()?;
|
io::stdout().flush()?;
|
||||||
|
|
||||||
let mut answer = String::new();
|
let mut answer = String::new();
|
||||||
|
12
src/error.rs
12
src/error.rs
@ -45,11 +45,11 @@ 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)?;
|
||||||
|
|
||||||
// Details
|
// Details
|
||||||
for detail in &self.details {
|
for detail in &self.details {
|
||||||
writeln!(f, " {}-{} {}", white(), yellow(), detail)?;
|
writeln!(f, " {}-{} {}", *WHITE, *YELLOW, detail)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hints
|
// Hints
|
||||||
@ -57,11 +57,11 @@ impl Display for FinalError {
|
|||||||
// Separate by one blank line.
|
// Separate by one blank line.
|
||||||
writeln!(f)?;
|
writeln!(f)?;
|
||||||
for hint in &self.hints {
|
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("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(format!(" {}https://github.com/vrmiguel/ouch/issues ", *CYAN))
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
error
|
error
|
||||||
@ -147,7 +147,7 @@ impl fmt::Display for 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")
|
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(),
|
.clone(),
|
||||||
Error::UnknownExtensionError(_) => todo!(),
|
Error::UnknownExtensionError(_) => todo!(),
|
||||||
Error::AlreadyExists => todo!(),
|
Error::AlreadyExists => todo!(),
|
||||||
|
24
src/lib.rs
24
src/lib.rs
@ -19,21 +19,11 @@ mod utils;
|
|||||||
|
|
||||||
pub use error::{Error, Result};
|
pub use error::{Error, Result};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
/// The status code ouch has when an error is encountered
|
/// The status code ouch has when an error is encountered
|
||||||
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
|
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
|
||||||
|
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
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() {
|
fn help_command() {
|
||||||
use utils::colors::*;
|
use utils::colors::*;
|
||||||
|
|
||||||
@ -62,17 +52,17 @@ fn help_command() {
|
|||||||
another folder.
|
another folder.
|
||||||
|
|
||||||
Visit https://github.com/ouch-org/ouch for more usage examples.",
|
Visit https://github.com/ouch-org/ouch for more usage examples.",
|
||||||
magenta = magenta(),
|
magenta = *MAGENTA,
|
||||||
white = white(),
|
white = *WHITE,
|
||||||
green = green(),
|
green = *GREEN,
|
||||||
yellow = yellow(),
|
yellow = *YELLOW,
|
||||||
reset = reset(),
|
reset = *RESET,
|
||||||
cyan = cyan()
|
cyan = *CYAN
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn version_command() {
|
fn version_command() {
|
||||||
use utils::colors::*;
|
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_export]
|
||||||
macro_rules! info {
|
macro_rules! info {
|
||||||
($writer:expr, $($arg:tt)*) => {
|
($writer:expr, $($arg:tt)*) => {
|
||||||
@ -14,11 +12,7 @@ macro_rules! info {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn _info_helper() {
|
pub fn _info_helper() {
|
||||||
use crate::utils::colors::{reset, yellow};
|
use crate::utils::colors::{RESET, YELLOW};
|
||||||
|
|
||||||
if *NO_COLOR_IS_SET {
|
print!("{}[INFO]{} ", *YELLOW, *RESET);
|
||||||
print!("[INFO] ");
|
|
||||||
} else {
|
|
||||||
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.
|
/// Module with a list of bright colors.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[cfg(target_family = "unix")]
|
|
||||||
pub mod colors {
|
pub mod colors {
|
||||||
pub const fn reset() -> &'static str {
|
use once_cell::sync::Lazy;
|
||||||
"\u{1b}[39m"
|
|
||||||
|
static DISABLE_COLORED_TEXT: 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 *DISABLE_COLORED_TEXT { "" } else { $value });
|
||||||
|
#[cfg(not(target_family = "unix"))]
|
||||||
|
pub static $name: &&str = &"";
|
||||||
|
};
|
||||||
}
|
}
|
||||||
pub const fn black() -> &'static str {
|
|
||||||
"\u{1b}[38;5;8m"
|
color!(RESET = "\u{1b}[39m");
|
||||||
}
|
color!(BLACK = "\u{1b}[38;5;8m");
|
||||||
pub const fn blue() -> &'static str {
|
color!(BLUE = "\u{1b}[38;5;12m");
|
||||||
"\u{1b}[38;5;12m"
|
color!(CYAN = "\u{1b}[38;5;14m");
|
||||||
}
|
color!(GREEN = "\u{1b}[38;5;10m");
|
||||||
pub const fn cyan() -> &'static str {
|
color!(MAGENTA = "\u{1b}[38;5;13m");
|
||||||
"\u{1b}[38;5;14m"
|
color!(RED = "\u{1b}[38;5;9m");
|
||||||
}
|
color!(WHITE = "\u{1b}[38;5;15m");
|
||||||
pub const fn green() -> &'static str {
|
color!(YELLOW = "\u{1b}[38;5;11m");
|
||||||
"\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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bytes {
|
impl Bytes {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user