Don't use colors when stdout or stderr are being redirected

This commit is contained in:
Vinícius Rodrigues Miguel 2021-10-05 21:12:03 -03:00
parent f7139548f6
commit e7dd6f2d4f
4 changed files with 24 additions and 2 deletions

21
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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)
};
}

View File

@ -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;