From bc04e1ac0062c0593f05c6ea9445e7419242c751 Mon Sep 17 00:00:00 2001 From: Lovecraftian Horror Date: Thu, 9 Apr 2020 15:14:16 -0400 Subject: [PATCH] Disable styling for `--color auto` if the output is being piped --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 11 ++++++++--- tests/lib.rs | 4 ++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c23d2d35e..feeec7df1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1178,6 +1178,7 @@ dependencies = [ "ansi_term 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", "app_dirs2 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "assert_cmd 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "docopt 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "escargot 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index f82ae84e1..7246dbab1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ path = "src/main.rs" [dependencies] ansi_term = "0.12.0" app_dirs = { version = "2", package = "app_dirs2" } +atty = "0.2" docopt = "1" env_logger = { version = "0.7", optional = true } flate2 = "1" diff --git a/src/main.rs b/src/main.rs index f1673e9a9..98e14076b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use std::process; use ansi_term::{Color, Style}; use app_dirs::AppInfo; +use atty::Stream; use docopt::Docopt; #[cfg(not(target_os = "windows"))] use pager::Pager; @@ -323,9 +324,13 @@ fn main() { let enable_styles = match args.flag_color { // Always use styling as long as there is `ansi_support` ColorOptions::Always => ansi_support, - // Disable if: - // * NO_COLOR env var is set to anything: https://no-color.org/ - ColorOptions::Auto => ansi_support && env::var_os("NO_COLOR").is_none(), + // Enable styling if: + // * There is `ansi_support` + // * NO_COLOR env var isn't set: https://no-color.org/ + // * The output stream is stdout (not being piped) + ColorOptions::Auto => { + ansi_support && env::var_os("NO_COLOR").is_none() && atty::is(Stream::Stdout) + } // Disable styling ColorOptions::Never => false, }; diff --git a/tests/lib.rs b/tests/lib.rs index 856d537b0..b2b2aa541 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -253,7 +253,7 @@ fn _test_correct_rendering(input_file: &str, filename: &str) { let expected = include_str!("inkscape-default.expected"); testenv .command() - .args(&["-f", &file_path.to_str().unwrap()]) + .args(&["--color", "always", "-f", &file_path.to_str().unwrap()]) .assert() .success() .stdout(similar(expected)); @@ -299,7 +299,7 @@ fn test_correct_rendering_with_config() { testenv .command() - .args(&["-f", &file_path.to_str().unwrap()]) + .args(&["--color", "always", "-f", &file_path.to_str().unwrap()]) .assert() .success() .stdout(similar(expected));