use supportsColor.level instead of an enabled bool

This commit is contained in:
Josh Junon 2017-01-17 00:52:59 -08:00
parent 5a69476142
commit 7d69b22f29
3 changed files with 11 additions and 10 deletions

View file

@ -7,8 +7,8 @@ var defineProps = Object.defineProperties;
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
function Chalk(options) {
// detect mode if not set manually
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
// detect level if not set manually
this.level = !options || options.level === undefined ? supportsColor.level : options.level;
}
// use bright blue on Windows as the normal blue color is illegible
@ -37,7 +37,7 @@ function build(_styles) {
};
builder._styles = _styles;
builder.enabled = this.enabled;
builder.level = this.level;
// __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype.
/* eslint-disable no-proto */
@ -59,7 +59,7 @@ function applyStyle() {
}
}
if (!this.enabled || !str) {
if (!this.level || !str) {
return str;
}

View file

@ -44,9 +44,9 @@
"text"
],
"dependencies": {
"ansi-styles": "^2.1.0",
"ansi-styles": "^3.0.0",
"escape-string-regexp": "^1.0.2",
"supports-color": "^3.1.2"
"supports-color": "^3.2.3"
},
"devDependencies": {
"coveralls": "^2.11.2",

View file

@ -132,17 +132,18 @@ describe('chalk on windows', function () {
});
});
describe('chalk.enabled', function () {
describe('chalk.level', function () {
it('should not output colors when manually disabled', function () {
chalk.enabled = false;
var oldLevel = chalk.level;
chalk.level = 0;
assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true;
chalk.level = oldLevel;
});
});
describe('chalk.constructor', function () {
it('should create a isolated context where colors can be disabled', function () {
var ctx = new chalk.constructor({enabled: false});
var ctx = new chalk.constructor({level: 0});
assert.equal(ctx.red('foo'), 'foo');
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
});