From a47562787cfc79ba3f12ceb782a13f907c91d255 Mon Sep 17 00:00:00 2001 From: mi_sawa Date: Sun, 30 Jun 2024 13:56:43 +0900 Subject: [PATCH 1/2] Support NO_COLOR environment variable --- src/bin/main.rs | 5 ++++- tests/cli/color_output.stdout | 6 ++++++ tests/cli/color_output.toml | 9 +++++++++ tests/cli/no_color_and_color.stdout | 0 tests/cli/no_color_and_color.toml | 12 ++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/cli/color_output.stdout create mode 100644 tests/cli/color_output.toml create mode 100644 tests/cli/no_color_and_color.stdout create mode 100644 tests/cli/no_color_and_color.toml diff --git a/src/bin/main.rs b/src/bin/main.rs index 597e58a..62abcf1 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -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("NO_COLOR").is_ok_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 { diff --git a/tests/cli/color_output.stdout b/tests/cli/color_output.stdout new file mode 100644 index 0000000..e70b7fe --- /dev/null +++ b/tests/cli/color_output.stdout @@ -0,0 +1,6 @@ +1 +"a" +[ + "a", + "b" +] diff --git a/tests/cli/color_output.toml b/tests/cli/color_output.toml new file mode 100644 index 0000000..fa74998 --- /dev/null +++ b/tests/cli/color_output.toml @@ -0,0 +1,9 @@ +args = [ "-C" ] + +stdin = ''' +1 +"a" +["a", "b"] +''' + +# stdout in .stdout file diff --git a/tests/cli/no_color_and_color.stdout b/tests/cli/no_color_and_color.stdout new file mode 100644 index 0000000..e69de29 diff --git a/tests/cli/no_color_and_color.toml b/tests/cli/no_color_and_color.toml new file mode 100644 index 0000000..d29ff8c --- /dev/null +++ b/tests/cli/no_color_and_color.toml @@ -0,0 +1,12 @@ +args = [ "-C" ] + +[env.add] +NO_COLOR = "1" + +stdin = ''' +1 +"a" +["a", "b"] +''' + +# stdout in .stdout file From ca8ccda114bf4e8624c91a57ec9e1a128f0cc7f0 Mon Sep 17 00:00:00 2001 From: mi_sawa Date: Sun, 30 Jun 2024 14:03:28 +0900 Subject: [PATCH 2/2] Use var_os instead of var --- src/bin/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 62abcf1..cc14bc0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -231,7 +231,7 @@ 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("NO_COLOR").is_ok_and(|s| !s.is_empty()); + 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