Remove the .enabled property in favor of .level (#356)

This commit is contained in:
Qix 2019-07-13 07:45:31 +02:00 committed by Sindre Sorhus
parent 87156ce8e2
commit 1f77953f1a
9 changed files with 17 additions and 101 deletions

16
index.d.ts vendored
View file

@ -24,13 +24,6 @@ declare namespace chalk {
type Level = LevelEnum; type Level = LevelEnum;
interface Options { interface Options {
/**
Enable or disable Chalk.
@default true
*/
enabled?: boolean;
/** /**
Specify the color support for Chalk. Specify the color support for Chalk.
By default, color support is automatically detected based on the environment. By default, color support is automatically detected based on the environment.
@ -98,13 +91,6 @@ declare namespace chalk {
*/ */
Instance: Instance; Instance: Instance;
/**
Enable or disable Chalk.
@default true
*/
enabled: boolean;
/** /**
The color support for Chalk. The color support for Chalk.
By default, color support is automatically detected based on the environment. By default, color support is automatically detected based on the environment.
@ -248,7 +234,7 @@ declare namespace chalk {
readonly strikethrough: Chalk; readonly strikethrough: Chalk;
/** /**
Modifier: Prints the text only when Chalk is enabled. Modifier: Prints the text only when Chalk has a color support level > 0.
Can be useful for things that are purely cosmetic. Can be useful for things that are purely cosmetic.
*/ */
readonly visible: Chalk; readonly visible: Chalk;

View file

@ -24,7 +24,6 @@ expectError(chalk.reset.supportsColor);
expectType<chalk.Chalk>(new chalk.Instance({level: 1})); expectType<chalk.Chalk>(new chalk.Instance({level: 1}));
// -- Properties -- // -- Properties --
expectType<boolean>(chalk.enabled);
expectType<chalk.Level>(chalk.level); expectType<chalk.Level>(chalk.level);
// -- Template literal -- // -- Template literal --

View file

@ -120,21 +120,11 @@ Chain [styles](#styles) and call the last one as a method with a string argument
Multiple arguments will be separated by space. 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. When `chalk.enabled` is `true`, `chalk.level` must *also* be greater than `0` for colored output to be produced.
Chalk is enabled by default unless explicitly disabled via `new chalk.Instance()` or `chalk.level` is `0`.
If you need to change this in a reusable module, create a new instance:
```js
const ctx = new chalk.Instance({enabled: false});
```
### chalk.level ### 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. When `chalk.level` is greater than `0`, `chalk.enabled` must *also* be `true` for colored output to be produced. Specifies the level of color support.
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:
@ -170,7 +160,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
- `inverse`- Inverse background and foreground colors. - `inverse`- Inverse background and foreground colors.
- `hidden` - Prints the text, but makes it invisible. - `hidden` - Prints the text, but makes it invisible.
- `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)* - `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
- `visible`- Prints the text only when Chalk is enabled. Can be useful for things that are purely cosmetic. - `visible`- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
### Colors ### Colors

View file

@ -28,7 +28,6 @@ const applyOptions = (object, options = {}) => {
// Detect level if not set manually // Detect level if not set manually
const colorLevel = stdoutColor ? stdoutColor.level : 0; const colorLevel = stdoutColor ? stdoutColor.level : 0;
object.level = options.level === undefined ? colorLevel : options.level; object.level = options.level === undefined ? colorLevel : options.level;
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
}; };
class ChalkClass { class ChalkClass {
@ -120,15 +119,6 @@ const proto = Object.defineProperties(() => {}, {
set(level) { set(level) {
this._generator.level = level; this._generator.level = level;
} }
},
enabled: {
enumerable: true,
get() {
return this._generator.enabled;
},
set(enabled) {
this._generator.enabled = enabled;
}
} }
}); });
@ -171,7 +161,7 @@ const createBuilder = (self, _styler, _isEmpty) => {
}; };
const applyStyle = (self, string) => { const applyStyle = (self, string) => {
if (!self.enabled || self.level <= 0 || !string) { if (self.level <= 0 || !string) {
return self._isEmpty ? '' : string; return self._isEmpty ? '' : string;
} }

View file

@ -1,35 +0,0 @@
import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const chalk = require('../source');
test('don\'t output colors when manually disabled', t => {
chalk.enabled = false;
t.is(chalk.red('foo'), 'foo');
chalk.enabled = true;
});
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
chalk.enabled = true;
t.true(red.enabled);
chalk.enabled = true;
});
test('propagate enable/disable changes from child colors', t => {
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
t.false(chalk.enabled);
red.enabled = true;
t.true(red.enabled);
t.true(chalk.enabled);
chalk.enabled = false;
t.false(red.enabled);
t.false(chalk.enabled);
chalk.enabled = true;
});

View file

@ -6,21 +6,13 @@ require('./_supports-color')(__dirname);
const chalk = require('../source'); const chalk = require('../source');
test('create an isolated context where colors can be disabled (by level)', t => { test('create an isolated context where colors can be disabled (by level)', t => {
const instance = new chalk.Instance({level: 0, enabled: true}); const instance = new chalk.Instance({level: 0});
t.is(instance.red('foo'), 'foo'); t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.level = 2; instance.level = 2;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
}); });
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
const instance = new chalk.Instance({enabled: false});
t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = true;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});
test('the `level` option should be a number from 0 to 3', t => { test('the `level` option should be a number from 0 to 3', t => {
/* eslint-disable no-new */ /* eslint-disable no-new */
t.throws(() => { t.throws(() => {

View file

@ -14,7 +14,7 @@ test('don\'t output colors when manually disabled', t => {
chalk.level = oldLevel; chalk.level = oldLevel;
}); });
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => { test('enable/disable colors based on overall chalk .level property, not individual instances', t => {
const oldLevel = chalk.level; const oldLevel = chalk.level;
chalk.level = 1; chalk.level = 1;
const {red} = chalk; const {red} = chalk;

View file

@ -10,7 +10,7 @@ require('./_supports-color')(__dirname, {
const chalk = require('../source'); const chalk = require('../source');
test.failing('colors can be forced by using chalk.enabled', t => { test('colors can be forced by using chalk.level', t => {
chalk.enabled = true; chalk.level = 1;
t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m'); t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m');
}); });

View file

@ -5,40 +5,34 @@ require('./_supports-color')(__dirname);
const chalk = require('../source'); const chalk = require('../source');
test('visible: normal output when enabled', t => { test('visible: normal output when level > 0', t => {
const instance = new chalk.Instance({level: 3, enabled: true}); const instance = new chalk.Instance({level: 3});
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
}); });
test('visible: no output when disabled', t => {
const instance = new chalk.Instance({level: 3, enabled: false});
t.is(instance.red.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
});
test('visible: no output when level is too low', t => { test('visible: no output when level is too low', t => {
const instance = new chalk.Instance({level: 0, enabled: true}); const instance = new chalk.Instance({level: 0});
t.is(instance.visible.red('foo'), ''); t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), ''); t.is(instance.red.visible('foo'), '');
}); });
test('test switching back and forth between enabled and disabled', t => { test('test switching back and forth between level == 0 and level > 0', t => {
const instance = new chalk.Instance({level: 3, enabled: true}); const instance = new chalk.Instance({level: 3});
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible('foo'), 'foo'); t.is(instance.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = false; instance.level = 0;
t.is(instance.red('foo'), 'foo'); t.is(instance.red('foo'), 'foo');
t.is(instance.visible('foo'), ''); t.is(instance.visible('foo'), '');
t.is(instance.visible.red('foo'), ''); t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), ''); t.is(instance.red.visible('foo'), '');
t.is(instance.red('foo'), 'foo'); t.is(instance.red('foo'), 'foo');
instance.enabled = true; instance.level = 3;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m'); t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');