Support FORCE_COLOR and NO_COLOR#813
Conversation
adrienverge
left a comment
There was a problem hiding this comment.
Hello Antoine,
Thanks for this proposal. NO_COLOR and FORCE_COLOR seem pretty standard, so it looks like a good idea 👍
Please see my comments in the code, and the tests must be reworked:
- Do no test the internal function
cli.supports_color(), instead, test the real yamllint program. See inside inCommandLineTestCase, there are alreadytest_run_piped_output_nocolor(),test_run_format_colored(),test_run_format_colored_warning(). Please add the new tests there. - Also test
NO_COLORandFORCE_COLORin conjunction with--format standardand--format colored(the command line argument must win). - No need for comments
# Assertion helpersand# Tests: the readers will infer that 😉
| self.assertNotSupportsColor({'NO_COLOR': '1', 'FORCE_COLOR': '1'}) | ||
|
|
||
| def test_force_color_set(self): | ||
| self.assertSupportsColor({'FORCE_COLOR': ''}) |
There was a problem hiding this comment.
This looks like an error, right?
From https://force-color.org/, if FORCE_COLOR is empty, it should be ignored:
When this variable is present and not an empty string, it should force the addition of ANSI color.
There was a problem hiding this comment.
It is definitely an error which has two causes:
-
I did not read the spec, despite it being written in bold, it is again a case of Read The Fine Manual. Thank you for having quoted it.
-
no_color[0] != '\0', I have misread that as the first character is zero and my implementation does not check that. I took a few courses at C language 30 years ago went for pascal and assembly instead and never looked back. That\0isNULLor the end of a string eg it is an empty string :-]
I'll fix it later today. thank you.
There was a problem hiding this comment.
I have changed the logic to check the absence of the env variable and it NOT being an empty string:
if (no_color is not None and no_color != ''):
return False
if (force_color is not None and force_color != ''):
return True
|
TODO address the items mentioned in the cover message which I have missed:
That will be for after the lunch break :] |
When running on CI, the process stdout is not a tty and coloring is disabled unless one uses `--format colored`. Add support for forcing colored output via the environment variable `FORCE_COLOR` and add `NO_COLOR` (which takes precedence). The spec from https://force-color.org/ states an environment variable is taken in account when: - it is present - AND NOT an empty string > Command-line software which outputs colored text should check for a > FORCE_COLOR environment variable. When this variable is present and not > an empty string (regardless of its value), it should force the > addition of ANSI color.
I have kept the unit tests covering
I have added test cases for the following combinations:
I have removed them :-] |
When running on CI the process stdout is not a tty and coloring is disabled unless one uses
--format colored.Add support for forcing colored output via the environment variable
FORCE_COLORand addNO_COLOR(which takes precedence).The spec from https://force-color.org/ states an environment variable is taken in account when: