Skip to content
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 ability to force disable colors with an environment variable #31

Merged
merged 2 commits into from
Jan 22, 2017

Conversation

Qix-
Copy link
Member

@Qix- Qix- commented Sep 2, 2015

So this extends the FORCE_COLOR environment variable to allow FORCE_COLOR=0 (preserving the behavior of just FORCE_COLOR=), which will forcibly disable color environment-wide.

This was inspired by the bug over at electron/electron#1647

@sindresorhus
Copy link
Member

Wouldn't it be better to have a separate variable, like DISABLE_COLOR?

@Qix-
Copy link
Member Author

Qix- commented Sep 2, 2015

I read FORCE_COLOR=0 as force color: off. More than one environment variable would be messy; what happens when both are 1?

@silverwind
Copy link
Contributor

Wouldn't it be better to have a separate variable, like DISABLE_COLOR?

I'd advice not to use generic variable names like that. Something like CHALK_COLOR or even SUPPORTS_COLOR_ENABLE would make it more obvious where that variable belongs and avoid naming collisions with other software. But it's probably too late to change that now, is it?

@Qix-
Copy link
Member Author

Qix- commented Sep 3, 2015

FORCE_COLOR was part of some ANSI movement a while back, which I think is why it's in there. I agree with @silverwind, even though I also think FORCE_COLOR=0 is sufficient, personally.

jhasse added a commit to jhasse/supports-color that referenced this pull request Oct 22, 2015
jhasse added a commit to jhasse/supports-color that referenced this pull request Oct 22, 2015
jhasse added a commit to jhasse/supports-color that referenced this pull request Oct 22, 2015
jhasse added a commit to jhasse/supports-color that referenced this pull request Oct 22, 2015
jhasse added a commit to jhasse/supports-color that referenced this pull request Oct 27, 2015
@sshiman
Copy link

sshiman commented Jan 5, 2016

Are the any plans to merge this pull request yet? Need this with regards to issues with PowerShell on Windows displaying Gulp output (using chalk). Need to disable coloring to workaround that issue. Checking on the value for FORCE_COLOR like FORCE_COLOR=0 will work from my opinion. Any ETA on releasing of this change?

@Qix-
Copy link
Member Author

Qix- commented Jan 5, 2016

I think this PR looks fine and wouldn't mind merging it. @sindresorhus should have the final word.

@sindresorhus
Copy link
Member

I thought #32 superseded this?

@Qix-
Copy link
Member Author

Qix- commented Jan 5, 2016

Perhaps at one point. This could stand on its own, though. This seems more sane than introducing a new env variable. I would personally want to see this one before I saw the other one, and I think the other one could use some improvement whereas this one is good to go.

@sshiman
Copy link

sshiman commented Jan 5, 2016

I agree that no need to introduce the new variable right now. So, any ETA on merging?

@jhasse
Copy link

jhasse commented Jan 5, 2016

Actually I don't think this should be merged, as this introduces a new behavior which is unique to this package. Is there any documentation for the FORCE_COLOR variable besides this README? What if other programs rely on FORCE_COLOR=0?

I think a new variable might be best, maybe CLICOLOR_DISABLE?

@sshiman
Copy link

sshiman commented Jan 5, 2016

To my knowledge the value for FORCE_COLOR is ignored, so I don't think anybody is using this one. But we need to rely on value instead of just existence of the variable in order to do the "switch". Introduction of the new variable will add a lot of complexity to "sync" all those variables' values.

@jbnicolai
Copy link
Contributor

@Qix- Apologies, bit late to the discussion, but I'm with @sindresorhus that this is confusing. I interpreted (and initially implemented) FORCE_COLOR as a boolean flag, i.e.: override everything, or just apply default behaviour.

I agree that similar behaviour like FORCE_NO_COLOR also has it's usecases, but I think separating that into two boolean flags (force-go, force-no) is easier.

Since the original flag name 'FORCE_COLOR' was chosen simply because we weren't aware for a better standard, I'm leaning towards #32 as well, and suggest we close this one. If there's a better standard than suggested in #32, let's discuss there. Thanks for the effort though!

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

Totally disagree. Having two environment variables makes no sense. What happens in the following scenario? The outcome is unclear.

FORCE_NO_COLOR= FORCE_COLOR= some-ansi-program --color

whereas

FORCE_COLOR= some-ansi-program
# or
FORCE_COLOR=1 some-ansi-program

and

FORCE_COLOR=0 some-ansi-program

make perfect sense. This is also analogous to other programs I've seen using environment variables as boolean values (namely, make and cmake).

Environment variables shouldn't be used as a programming construct, they should be used as configurations. Throwing more crap into your environment only waters things down and I firmly believe in the long run it'll confuse people to have two variables.

With one, all we need in the docs is setting it to 0 forces no color.

With two, we'll instead need a whole truth table:

FORCE_COLOR present FORCE_COLOR not present
FORCE_NO_COLOR present ?? no color
FORCE_NO_COLOR not present color default?

@jhasse
Copy link

jhasse commented Jan 6, 2016

FORCE_NO_COLOR= FORCE_COLOR= some-ansi-program --color

I don't think this is a problem as there are cases like this anyway:

FORCE_COLOR=0 some-ansi-program --color

Meaning that we have unclear scenarios anyway, a new environment variable would just introduce some more :D

FORCE_COLOR=0 some-ansi-program

It would not be intuitive to me that this would never output color.

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

But it's clear that FORCE means trump everything. It's clear from our docs and it's clear from context. Using FORCE_COLOR=0 with --color is pretty clear as to what happens.

@jhasse
Copy link

jhasse commented Jan 6, 2016

True. Okay you convinced me: I'm against adding a new env var.

Still not convinced about FORCE_COLOR=0 as I read it as "forcing colors isn't enabled" and not as "never display colors". I think

CLICOLOR=0 some-ansi-program

(as implemented by #34) makes more sense.

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

Im all for renaming it whatever if it doesn't make sense. Functionally though I remain of the opinion it should be one var.

@jhasse
Copy link

jhasse commented Jan 6, 2016

Unfortunately one (boolean) variable doesn't suffice, as there are 3 cases: on, off, auto. The last case could be handled by the TERM variable, but its possible values are a mess. Programs like ansicon need a simple way to tell that they support 16 colors via ANSI escape codes.

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

Shouldn't rely on the TERM var any more than we have to, unless we add termcap capabilities.

As for the auto portion, if the variable doesn't exist then do auto. Otherwise, use its value (or lack thereof) to determine what to force.

@jhasse
Copy link

jhasse commented Jan 6, 2016

As for the auto portion, if the variable doesn't exist then do auto. Otherwise, use its value (or lack thereof) to determine what to force.

That wouldn't work on Window's cmd.exe, since it doesn't support ANSI colors out of the box.

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

@jhasse it works just fine now, what are you talking about? By default supports-color returns 1 (16 colors) on windows unless other flags are present or the environment variable is there.

Windows supports ANSI escapes thanks to LibUV, obviously only within the Node.js environment.

Barring renaming this variable, this is how it would work:

  • FORCE_COLOR not present in system.env: automatic
  • FORCE_COLOR= or FORCE_COLOR=1: force colors
  • FORCE_COLOR=0: force no colors

It literally cannot get any simpler than that. This is a cross platform way to do it.

@jhasse
Copy link

jhasse commented Jan 6, 2016

@Qix- As you said yourself this is only within the Node.js environment. I was hoping for an universal variable name, and the Mac tools are already using CLICOLOR(_FORCE).

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

@jhasse

Barring renaming this variable

@jhasse
Copy link

jhasse commented Jan 6, 2016

Than it still stands that outside of the Node.js environment, cmd.exe doesn't support ANSI escape codes out of the box. Therefore programs shouldn't output them when the variable isn't defined.

@Qix-
Copy link
Member Author

Qix- commented Jan 6, 2016

@jhasse I'm not sure what you mean by that. Will supports-color be used outside of a Node.js environment?

@jhasse
Copy link

jhasse commented Jan 6, 2016

No. I'm looking for variable names which wouldn't only be used by supports-color though. CLICOLOR_FORCE for example is used by Mac's cmd tools and CMake. I'm trying to get the ball rolling, so that one day, I only have to define one env variable in my CI and all programs output ANSI escape codes.

@Qix-
Copy link
Member Author

Qix- commented May 31, 2016

I'm still a 'yes' for this, after giving it some time @sindresorhus. I've run into a multitude of situations where FORCE_COLOR=0 would have been nice and easy to use and understand.

Any more discussion on this is bikeshedding - if I'm defining environment variables just to exist I'm not going to be setting them to =0. Alternatively, if I see SOME_VAR=0 I'm going to assume it's being shut off in an explicit matter.

@jhasse
Copy link

jhasse commented May 31, 2016

Any more discussion on this is bikeshedding - if I'm defining environment variables just to exist I'm not going to be setting them to =0. Alternatively, if I see SOME_VAR=0 I'm going to assume it's being shut off in an explicit matter.

+1

In order to keep variable names which control ANSI colors to a minimum I still hope for CLICOLOR(_FORCE)=0 instead of FORCE_COLOR :)

@Qix-
Copy link
Member Author

Qix- commented Jan 16, 2017

@sindresorhus

I would have preferred a binary behavior CLICOLOR=0 for off and CLICOLOR=1 for on, if nothing is set it would behave as normal.

You said it yourself. I'm still on board with this PR and would like to see some movement on it. I'm tempted to merge this PR at the moment since we've been sitting on it for so long and it's becoming a point of bike-shed at this point.

@sindresorhus sindresorhus changed the title added ability to force disabling of all colors Add ability to force disable colors with an environment variable Jan 21, 2017
supportLevel = 1;
if ('FORCE_COLOR' in process.env) {
var forceColor = parseInt(process.env.FORCE_COLOR, 10);
supportLevel = forceColor === 0 ? 0 : supportLevel || 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap supportLevel || 1 in parens to make the precedence clear.

@@ -44,7 +44,7 @@ The returned object specifies a level of support for color through a `.level` pr

It obeys the `--color` and `--no-color` CLI flags.

For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. Trumps `--no-color`.
For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. If `FORCE_COLOR` is set to `0`, colors are force disabled. Trumps `--no-color` / color flags, respectively.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should drop with any value, and instead say FORCE_COLOR=1 to force color, FORCE_COLOR=0 to force disable colors, and don't use it for auto-detection (for clarity).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've re-worded that whole section.

var result = requireUncached('./');
assert.equal(Boolean(result), true);
assert.equal(result.level, 2);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, can you add a test for process.env.FORCE_COLOR = '1'; too?

@sindresorhus
Copy link
Member

I've been thinking long and hard about this issue and I've decided to go with this solution. I really don't want to introduce another environment variable, and from looking through the proposals about CLICOLOR(_FORCE) on other issue tracker, the response is generally lukewarm: http://bixense.com/clicolors/#bug-reports We can reconsider adding support for CLICOLOR at a later point if it ends up getting more adoption, but I'm not a big fan of the proposal, so I don't intend on being the person to push it forward.

@Qix-
Copy link
Member Author

Qix- commented Jan 21, 2017

@sindresorhus the requested changes have been made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants