From e7dd6f2d4fc95ef63185006dcdfb66b7798d20f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Tue, 5 Oct 2021 21:12:03 -0300 Subject: [PATCH] Don't use colors when `stdout` or `stderr` are being redirected --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 2 +- src/utils.rs | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ef9c38aa..894015c48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -104,6 +115,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -152,6 +172,7 @@ dependencies = [ name = "ouch" version = "0.1.6" dependencies = [ + "atty", "bzip2", "flate2", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 5eddf3f8f..30531fb53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ 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] +atty = "0.2.14" lazy_static = "1.4.0" walkdir = "2.3.2" strsim = "0.10.0" diff --git a/src/lib.rs b/src/lib.rs index 67db5e915..bc5cb2ede 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ lazy_static! { static ref NO_COLOR_IS_SET: bool = { use std::env; - env::var("NO_COLOR").is_ok() + env::var("NO_COLOR").is_ok() || atty::isnt(atty::Stream::Stdout) || atty::isnt(atty::Stream::Stderr) }; } diff --git a/src/utils.rs b/src/utils.rs index 5116ccb2e..a56220d25 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -91,7 +91,7 @@ pub mod colors { #[allow(dead_code, non_upper_case_globals)] #[cfg(not(target_family = "unix"))] pub mod colors { - pub fn empty() -> &'static str { + pub const fn empty() -> &'static str { "" } pub const reset: fn() -> &'static str = empty;