Skip to content

Commit

Permalink
Merge pull request #185 from MiSawa/feat/NO_COLOR
Browse files Browse the repository at this point in the history
Support NO_COLOR environment variable
  • Loading branch information
MiSawa authored Jun 30, 2024
2 parents 74838f1 + ca8ccda commit 0653cdf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ fn run_with_input(cli: Cli, input: impl Input) -> Result<()> {
match output_format {
SerializationFormat::Json => {
let is_stdout_terminal = stdout().is_terminal();
let no_color_from_env = || std::env::var_os("NO_COLOR").is_some_and(|s| !s.is_empty());
let should_colorize_output = cli.output_format.color_output
|| (is_stdout_terminal && !cli.output_format.monochrome_output);
|| (is_stdout_terminal
&& !cli.output_format.monochrome_output
&& !no_color_from_env());
let color_styler = should_colorize_output.then(get_json_style);

for value in result_iterator {
Expand Down
6 changes: 6 additions & 0 deletions tests/cli/color_output.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
"a"
[
"a",
"b"
]
9 changes: 9 additions & 0 deletions tests/cli/color_output.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
args = [ "-C" ]

stdin = '''
1
"a"
["a", "b"]
'''

# stdout in .stdout file
Empty file.
12 changes: 12 additions & 0 deletions tests/cli/no_color_and_color.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
args = [ "-C" ]

[env.add]
NO_COLOR = "1"

stdin = '''
1
"a"
["a", "b"]
'''

# stdout in .stdout file

0 comments on commit 0653cdf

Please sign in to comment.