Skip to content

Commit 1f77953

Browse files
Qix-sindresorhus
authored andcommitted
Remove the .enabled property in favor of .level (#356)
1 parent 87156ce commit 1f77953

9 files changed

Lines changed: 17 additions & 101 deletions

File tree

index.d.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ declare namespace chalk {
2424
type Level = LevelEnum;
2525

2626
interface Options {
27-
/**
28-
Enable or disable Chalk.
29-
30-
@default true
31-
*/
32-
enabled?: boolean;
33-
3427
/**
3528
Specify the color support for Chalk.
3629
By default, color support is automatically detected based on the environment.
@@ -98,13 +91,6 @@ declare namespace chalk {
9891
*/
9992
Instance: Instance;
10093

101-
/**
102-
Enable or disable Chalk.
103-
104-
@default true
105-
*/
106-
enabled: boolean;
107-
10894
/**
10995
The color support for Chalk.
11096
By default, color support is automatically detected based on the environment.
@@ -248,7 +234,7 @@ declare namespace chalk {
248234
readonly strikethrough: Chalk;
249235

250236
/**
251-
Modifier: Prints the text only when Chalk is enabled.
237+
Modifier: Prints the text only when Chalk has a color support level > 0.
252238
Can be useful for things that are purely cosmetic.
253239
*/
254240
readonly visible: Chalk;

index.test-d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ expectError(chalk.reset.supportsColor);
2424
expectType<chalk.Chalk>(new chalk.Instance({level: 1}));
2525

2626
// -- Properties --
27-
expectType<boolean>(chalk.enabled);
2827
expectType<chalk.Level>(chalk.level);
2928

3029
// -- Template literal --

readme.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,11 @@ Chain [styles](#styles) and call the last one as a method with a string argument
120120

121121
Multiple arguments will be separated by space.
122122

123-
### chalk.enabled
124-
125-
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.
126-
127-
Chalk is enabled by default unless explicitly disabled via `new chalk.Instance()` or `chalk.level` is `0`.
128-
129-
If you need to change this in a reusable module, create a new instance:
130-
131-
```js
132-
const ctx = new chalk.Instance({enabled: false});
133-
```
134-
135123
### chalk.level
136124

137-
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.
125+
Specifies the level of color support.
126+
127+
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.
138128

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

@@ -170,7 +160,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
170160
- `inverse`- Inverse background and foreground colors.
171161
- `hidden` - Prints the text, but makes it invisible.
172162
- `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
173-
- `visible`- Prints the text only when Chalk is enabled. Can be useful for things that are purely cosmetic.
163+
- `visible`- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
174164

175165
### Colors
176166

source/index.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const applyOptions = (object, options = {}) => {
2828
// Detect level if not set manually
2929
const colorLevel = stdoutColor ? stdoutColor.level : 0;
3030
object.level = options.level === undefined ? colorLevel : options.level;
31-
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
3231
};
3332

3433
class ChalkClass {
@@ -120,15 +119,6 @@ const proto = Object.defineProperties(() => {}, {
120119
set(level) {
121120
this._generator.level = level;
122121
}
123-
},
124-
enabled: {
125-
enumerable: true,
126-
get() {
127-
return this._generator.enabled;
128-
},
129-
set(enabled) {
130-
this._generator.enabled = enabled;
131-
}
132122
}
133123
});
134124

@@ -171,7 +161,7 @@ const createBuilder = (self, _styler, _isEmpty) => {
171161
};
172162

173163
const applyStyle = (self, string) => {
174-
if (!self.enabled || self.level <= 0 || !string) {
164+
if (self.level <= 0 || !string) {
175165
return self._isEmpty ? '' : string;
176166
}
177167

test/enabled.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/instance.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@ require('./_supports-color')(__dirname);
66
const chalk = require('../source');
77

88
test('create an isolated context where colors can be disabled (by level)', t => {
9-
const instance = new chalk.Instance({level: 0, enabled: true});
9+
const instance = new chalk.Instance({level: 0});
1010
t.is(instance.red('foo'), 'foo');
1111
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
1212
instance.level = 2;
1313
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
1414
});
1515

16-
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
17-
const instance = new chalk.Instance({enabled: false});
18-
t.is(instance.red('foo'), 'foo');
19-
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
20-
instance.enabled = true;
21-
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
22-
});
23-
2416
test('the `level` option should be a number from 0 to 3', t => {
2517
/* eslint-disable no-new */
2618
t.throws(() => {

test/level.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('don\'t output colors when manually disabled', t => {
1414
chalk.level = oldLevel;
1515
});
1616

17-
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
17+
test('enable/disable colors based on overall chalk .level property, not individual instances', t => {
1818
const oldLevel = chalk.level;
1919
chalk.level = 1;
2020
const {red} = chalk;

test/no-color-support.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require('./_supports-color')(__dirname, {
1010

1111
const chalk = require('../source');
1212

13-
test.failing('colors can be forced by using chalk.enabled', t => {
14-
chalk.enabled = true;
13+
test('colors can be forced by using chalk.level', t => {
14+
chalk.level = 1;
1515
t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m');
1616
});

test/visible.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,34 @@ require('./_supports-color')(__dirname);
55

66
const chalk = require('../source');
77

8-
test('visible: normal output when enabled', t => {
9-
const instance = new chalk.Instance({level: 3, enabled: true});
8+
test('visible: normal output when level > 0', t => {
9+
const instance = new chalk.Instance({level: 3});
1010
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
1111
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
1212
});
1313

14-
test('visible: no output when disabled', t => {
15-
const instance = new chalk.Instance({level: 3, enabled: false});
16-
t.is(instance.red.visible('foo'), '');
17-
t.is(instance.visible.red('foo'), '');
18-
});
19-
2014
test('visible: no output when level is too low', t => {
21-
const instance = new chalk.Instance({level: 0, enabled: true});
15+
const instance = new chalk.Instance({level: 0});
2216
t.is(instance.visible.red('foo'), '');
2317
t.is(instance.red.visible('foo'), '');
2418
});
2519

26-
test('test switching back and forth between enabled and disabled', t => {
27-
const instance = new chalk.Instance({level: 3, enabled: true});
20+
test('test switching back and forth between level == 0 and level > 0', t => {
21+
const instance = new chalk.Instance({level: 3});
2822
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
2923
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
3024
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
3125
t.is(instance.visible('foo'), 'foo');
3226
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
3327

34-
instance.enabled = false;
28+
instance.level = 0;
3529
t.is(instance.red('foo'), 'foo');
3630
t.is(instance.visible('foo'), '');
3731
t.is(instance.visible.red('foo'), '');
3832
t.is(instance.red.visible('foo'), '');
3933
t.is(instance.red('foo'), 'foo');
4034

41-
instance.enabled = true;
35+
instance.level = 3;
4236
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
4337
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
4438
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');

0 commit comments

Comments
 (0)