Skip to content

Commit

Permalink
Add ability to force disable colors with an environment variable (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- authored and sindresorhus committed Jan 22, 2017
1 parent c2394c1 commit a6b8b6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ var supportLevel = (function () {
return 0;
})();

if (supportLevel === 0 && 'FORCE_COLOR' in process.env) {
supportLevel = 1;
if ('FORCE_COLOR' in process.env) {
var forceColor = parseInt(process.env.FORCE_COLOR, 10);
supportLevel = forceColor === 0 ? 0 : (supportLevel || 1);
}

module.exports = process && support(supportLevel);
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=1` to forcefully enable color. Alternatively, `FORCE_COLOR=0` will be forcefully disabled. The use of `FORCE_COLOR` negates all other color checks performed by this module.

Explicit 256/truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.

Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ it('should return true if `FORCE_COLOR` is in env', function () {
assert.equal(result.level, 1);
});

it('should return true if `FORCE_COLOR` is in env, but honor 256', function () {
process.argv = ['--color=256'];
process.env.FORCE_COLOR = true;
var result = requireUncached('./');
assert.equal(Boolean(result), true);
assert.equal(result.level, 2);
});

it('should return true if `FORCE_COLOR` is in env, but honor 256', function () {
process.argv = ['--color=256'];
process.env.FORCE_COLOR = '1';
var result = requireUncached('./');
assert.equal(Boolean(result), true);
assert.equal(result.level, 2);
});

it('should return false if `FORCE_COLOR` is in env and is 0', function () {
process.env.FORCE_COLOR = '0';
var result = requireUncached('./');
assert.equal(Boolean(result), false);
});

it('should return false if not TTY', function () {
process.stdout.isTTY = false;
var result = requireUncached('./');
Expand Down

0 comments on commit a6b8b6d

Please sign in to comment.