Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support NO_COLOR environment variable #185

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading