Skip to content

Commit

Permalink
Re-implement chalk.enabled (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- authored and sindresorhus committed Jun 29, 2017
1 parent 608242a commit 09fb2d8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
16 changes: 14 additions & 2 deletions index.js
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +90,7 @@ function build(_styles, key) {
builder._styles = _styles;

const self = this;

Object.defineProperty(builder, 'level', {
enumerable: true,
get() {
Expand All @@ -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';

Expand All @@ -122,7 +134,7 @@ function applyStyle() {
}
}

if (!this.level || !str) {
if (!this.enabled || this.level <= 0 || !str) {
return str;
}

Expand Down
14 changes: 13 additions & 1 deletion readme.md
Expand Up @@ -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});
Expand Down
45 changes: 43 additions & 2 deletions test.js
Expand Up @@ -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.