Skip to content

Commit 0653cdf

Browse files
authored
Merge pull request #185 from MiSawa/feat/NO_COLOR
Support NO_COLOR environment variable
2 parents 74838f1 + ca8ccda commit 0653cdf

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

src/bin/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ fn run_with_input(cli: Cli, input: impl Input) -> Result<()> {
231231
match output_format {
232232
SerializationFormat::Json => {
233233
let is_stdout_terminal = stdout().is_terminal();
234+
let no_color_from_env = || std::env::var_os("NO_COLOR").is_some_and(|s| !s.is_empty());
234235
let should_colorize_output = cli.output_format.color_output
235-
|| (is_stdout_terminal && !cli.output_format.monochrome_output);
236+
|| (is_stdout_terminal
237+
&& !cli.output_format.monochrome_output
238+
&& !no_color_from_env());
236239
let color_styler = should_colorize_output.then(get_json_style);
237240

238241
for value in result_iterator {

tests/cli/color_output.stdout

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1
2+
"a"
3+
[
4+
"a",
5+
"b"
6+
]

tests/cli/color_output.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
args = [ "-C" ]
2+
3+
stdin = '''
4+
1
5+
"a"
6+
["a", "b"]
7+
'''
8+
9+
# stdout in .stdout file

tests/cli/no_color_and_color.stdout

Whitespace-only changes.

tests/cli/no_color_and_color.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
args = [ "-C" ]
2+
3+
[env.add]
4+
NO_COLOR = "1"
5+
6+
stdin = '''
7+
1
8+
"a"
9+
["a", "b"]
10+
'''
11+
12+
# stdout in .stdout file

0 commit comments

Comments
 (0)