Skip to content
Permalink
Browse files

Re-implement `chalk.enabled` (#160)

  • Loading branch information...
Qix- authored and sindresorhus committed Jun 29, 2017
1 parent 608242a commit 09fb2d86060e6c46e57a696c5bf682be0245b47b
Showing with 70 additions and 5 deletions.
  1. +14 −2 index.js
  2. +13 −1 readme.md
  3. +43 −2 test.js
@@ -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;
}

@@ -115,11 +115,23 @@ Chain [styles](#styles) and call the last one as a method with a string argument

Multiple arguments will be separated by space.

### chalk.enabled

Color support is automatically detected, as is the level (see `chalk.level`). However, if you'd like to simply enable/disable Chalk, you can do so via the `.enabled` property.

Chalk is enabled by default unless expicitly disabled via the constructor or `chalk.level` is `0`.

If you need to change this in a reusable module, create a new instance:

```js
const ctx = new chalk.constructor({enabled: false});
```

### chalk.level

Color support is automatically detected, but you can override it by setting the `level` property. You should however only do this in your own code as it applies globally to all Chalk consumers.

If you need to change this in a reusable module create a new instance:
If you need to change this in a reusable module, create a new instance:

```js
const ctx = new chalk.constructor({level: 0});
45 test.js
@@ -194,10 +194,51 @@ describe('chalk.level', () => {
});
});

describe('chalk.enabled', () => {
it('should not output colors when manually disabled', () => {
chalk.enabled = false;
assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true;
});

it('should enable/disable colors based on overall chalk enabled property, not individual instances', () => {
chalk.enabled = false;
const red = chalk.red;
assert.equal(red.enabled, false);
chalk.enabled = true;
assert.equal(red.enabled, true);
chalk.enabled = true;
});

it('should propagate enable/disable changes from child colors', () => {
chalk.enabled = false;
const red = chalk.red;
assert.equal(red.enabled, false);
assert.equal(chalk.enabled, false);
red.enabled = true;
assert.equal(red.enabled, true);
assert.equal(chalk.enabled, true);
chalk.enabled = false;
assert.equal(red.enabled, false);
assert.equal(chalk.enabled, false);
chalk.enabled = true;
});
});

describe('chalk.constructor', () => {
it('should create a isolated context where colors can be disabled', () => {
const ctx = new chalk.constructor({level: 0});
it('should create an isolated context where colors can be disabled (by level)', () => {
const ctx = new chalk.constructor({level: 0, enabled: true});
assert.equal(ctx.red('foo'), 'foo');
assert.equal(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.level = 2;
assert.equal(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
});

it('should create an isolated context where colors can be disabled (by enabled flag)', () => {
const ctx = new chalk.constructor({enabled: false});
assert.equal(ctx.red('foo'), 'foo');
assert.equal(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.enabled = true;
assert.equal(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
});
});

0 comments on commit 09fb2d8

Please sign in to comment.
You can’t perform that action at this time.