Re-implement chalk.enabled (#160)

This commit is contained in:
Josh Junon 2017-06-29 13:34:00 -07:00 committed by Sindre Sorhus
parent 608242a4fc
commit 09fb2d8606
3 changed files with 73 additions and 8 deletions

View file

@ -13,7 +13,8 @@ const skipModels = new Set(['gray']);
function Chalk(options) {
// Detect level if not set manually
this.level = !options || options.level === undefined ? supportsColor.level : options.level;
this.level = Number(!options || options.level === undefined ? supportsColor.level : options.level);
this.enabled = options && 'enabled' in options ? options.enabled : this.level > 0;
}
// Use bright blue on Windows as the normal blue color is illegible
@ -89,6 +90,7 @@ function build(_styles, key) {
builder._styles = _styles;
const self = this;
Object.defineProperty(builder, 'level', {
enumerable: true,
get() {
@ -99,6 +101,16 @@ function build(_styles, key) {
}
});
Object.defineProperty(builder, 'enabled', {
enumerable: true,
get() {
return self.enabled;
},
set(enabled) {
self.enabled = enabled;
}
});
// See below for fix regarding invisible grey/dim combination on Windows
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
@ -122,7 +134,7 @@ function applyStyle() {
}
}
if (!this.level || !str) {
if (!this.enabled || this.level <= 0 || !str) {
return str;
}