Skip to content

Commit

Permalink
Merge pull request #242 from mearns/issue/21
Browse files Browse the repository at this point in the history
#21 Add support for FORCE_NO_COLOR env var
  • Loading branch information
DABH authored Sep 26, 2018
2 parents 4a6d75d + c19308d commit 1d56088
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ I prefer the first way. Some people seem to be afraid of extending `String.proto

If you are writing good code you will never have an issue with the first approach. If you really don't want to touch `String.prototype`, the second usage will not touch `String` native object.

## Disabling Colors
## Forcing Colors to be Enabled/Disabled

To disable colors you can pass the following arguments in the command line to your application:

```bash
node myapp.js --no-color
```

Alternatively, you can define an environment variable named `FORCE_COLOR` to force colors to be enabled (e.g. in a complex environment where colors support is not detected properly), or `FORCE_NO_COLOR` to force colors to be disabled; the environment variables will take effect if any non-zero value is specified.

## Console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data)

```js
Expand Down
31 changes: 31 additions & 0 deletions lib/system/has-env-flag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
MIT License
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

'use strict';

module.exports = function(flag, env) {
env = env || process.env;
return (flag in env) &&
(env[flag].length === 0 || parseInt(env[flag], 10) !== 0);
};
17 changes: 14 additions & 3 deletions lib/system/supports-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ THE SOFTWARE.

var os = require('os');
var hasFlag = require('./has-flag.js');
var hasEnvFlag = require('./has-env-flag.js');

var env = process.env;

Expand All @@ -37,9 +38,19 @@ if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) {
|| hasFlag('color=always')) {
forceColor = true;
}
if ('FORCE_COLOR' in env) {
forceColor = env.FORCE_COLOR.length === 0
|| parseInt(env.FORCE_COLOR, 10) !== 0;

var forceColorFromEnv = hasEnvFlag('FORCE_COLOR', env);
var forceNoColorFromEnv = hasEnvFlag('FORCE_NO_COLOR', env);
if (forceColorFromEnv) {
if (forceNoColorFromEnv && process.emitWarning) {
process.emitWarning(
'FORCE_COLOR and FORCE_NO_COLOR env vars are both set and conflicting, ' +
'going with FORCE_COLOR.'
);
}
forceColor = true;
} else if (forceNoColorFromEnv) {
forceColor = false;
}

function translateLevel(level) {
Expand Down
2 changes: 1 addition & 1 deletion tests/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ assert.equal(colors.custom(s),
colors.setTheme({custom: ['red', 'italic', 'inverse']});
assert.equal(colors.custom(s),
'\x1b[31m' + '\x1b[3m' + '\x1b[7m' + s +
'\x1b[27m' + '\x1b[23m' + '\x1b[39m' );
'\x1b[27m' + '\x1b[23m' + '\x1b[39m' );

0 comments on commit 1d56088

Please sign in to comment.