Skip to content

Commit

Permalink
Disable styling for --color auto if the output is being piped
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev authored and dbrgn committed Aug 30, 2020
1 parent be79c92 commit bc04e1a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down
4 changes: 2 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit bc04e1a

Please sign in to comment.