-
Notifications
You must be signed in to change notification settings - Fork 989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for CLICOLOR/CLICOLOR_FORCE/NO_COLOR output controls #7154
Conversation
The tests are broken, but because something was broken in develop due to last 1.26 release, don't worry about it, we will relaunch it. |
I rebased onto cbc88e0 if that helps any. |
These are checked in addition to the current color control variables. The CLICOLOR/CLICOLOR_FORCE variables are implemented according to https://bixense.com/clicolors/ The NO_COLOR variable is implemented according to https://no-color.org/ The colorization behavior falls back to the original logic if these variables are unset or if CLICOLOR_FORCE=0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @kchmck, very nice job.
I would like to comment that maybe the tests code could be improved by grouping by the result of the test and also parameterising them to reduce all the repeated code.
Maybe like:
@parameterized.expand([(False, {}), (True, {})...
def test_output_no_color(self, isatty, env):
with mock.patch("colorama.init") as init:
with mock.patch("sys.stdout.isatty", return_value=isatty), \
mock.patch.dict("os.environ", env, clear=True):
assert not colorama_initialize()
init.assert_not_called()
def test_output_color...
return False | ||
|
||
clicolor_force = get_env("CLICOLOR_FORCE") | ||
if clicolor_force is not None and clicolor_force != "0": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thy not using just get_env()
here https://docs.conan.io/en/latest/reference/tools.html#tools-get-env
if clicolor_force is not None and clicolor_force != "0": | |
if get_env("CLICOLOR_FORCE"): |
clicolor = get_env("CLICOLOR") | ||
if clicolor is not None: | ||
if clicolor == "0" or not isatty: | ||
return False | ||
import colorama | ||
colorama.init() | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work as well
clicolor = get_env("CLICOLOR") | |
if clicolor is not None: | |
if clicolor == "0" or not isatty: | |
return False | |
import colorama | |
colorama.init() | |
return True | |
if not get_env("CLICOLOR", False) or not isatty: | |
return False | |
if get_env("CLICOLOR", False); | |
import colorama | |
colorama.init() | |
return True |
Thanks all! |
Funny thing: you were so afraid to break customers with my PRs so you finally broke us :( By introducing a lot of new variables with extremely complicated interacton and overriding... As PR mentiones those variables are used by other tools used by Conan so we set We explicitly set |
Changelog: Feature: Add support for the
CLICOLOR
/CLICOLOR_FORCE
/NO_COLOR
output colorization control variables.Docs: conan-io/docs#1728
develop
branch, documenting this one.Note: By default this PR will skip the slower tests and will use a limited set of python versions. Check here how to increase the testing level by writing some tags in the current PR body text.
This was motivated on my part due to wanting colorized output in Gitlab CI displays. I found CMake could be forced to output colors with
CLICOLOR_FORCE=1
, and similar to the final comments of #4718, I found Conan could be forced to output colors with the combination ofCONAN_COLOR_DISPLAY=1
andPYCHARM_HOSTED=1
, resulting in the following at the top of the config:This works because in the current output logic
CONAN_COLOR_DISPLAY=1
enables the generation of color codes even through the output is not a tty, andPYCHARM_HOSTED=1
sets up colorama so that it doesn't strip these color codes when it detects that the output is not going to a tty. This setup is a little awkward though, because the CI project otherwise has nothing to do with pycharm. This PR enables the following config to have the same effect:It turns out the
CLICOLOR_FORCE
variable used by CMake has some historical usage on UNIX systems, and there is also a "specification" that describes howCLICOLOR
andCLICOLOR_FORCE
should be expected to work. Another notable user of this is Ninja.This PR implements
CLICOLOR
andCLICOLOR_FORCE
alongside the current color control variables. It also adds support forNO_COLOR
, which force disables color output, even in cases it would normally be enabled. Some notable users of this include npm and Homebrew.The first commit adds some tests for the current logic, to help ensure that there is no change in behavior when only the current controls are used. These tests use some mocks over colorama initialization rather than any sort of output testing, as it seems like those sorts of tests could get messy if they need to run on both Linux and Windows. Also AFAICT, how colorama is initialized is what ultimately determines the behavior of colorized output.
In the current implementation, the new variables are checked before the current variables and, if they are used, take precedence. For example,
NO_COLOR=1
overridesCONAN_COLOR_DISPLAY=1 & PYCHARM_HOSTED=1
, andCLICOLOR_FORCE=1
overridesCONAN_COLOR_DISPLAY=0
. Note that the output falls back to the original behavior whenNO_COLOR
andCLICOLOR
are unset and eitherCLICOLOR_FORCE
is unset orCLICOLOR_FORCE=0
.The second commit also extends the tests with various combinations of the current variables and new variables to hopefully cover all of the effective logic branches.
This should provide a solution for #4718 and should also handle the concern in #4718 (comment), as something like
NO_COLOR=1 conan --version
could be used to disable colors for parsing needs even if running in an environment with a globalCLICOLOR_FORCE=1
.