Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 1 addition & 15 deletions index.d.ts
Expand Up @@ -24,13 +24,6 @@ declare namespace chalk {
type Level = LevelEnum;

interface Options {
/**
Enable or disable Chalk.

@default true
*/
enabled?: boolean;

/**
Specify the color support for Chalk.
By default, color support is automatically detected based on the environment.
Expand Down Expand Up @@ -98,13 +91,6 @@ declare namespace chalk {
*/
Instance: Instance;

/**
Enable or disable Chalk.

@default true
*/
enabled: boolean;

/**
The color support for Chalk.
By default, color support is automatically detected based on the environment.
Expand Down Expand Up @@ -248,7 +234,7 @@ declare namespace 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.
*/
readonly visible: Chalk;
Expand Down
1 change: 0 additions & 1 deletion index.test-d.ts
Expand Up @@ -24,7 +24,6 @@ expectError(chalk.reset.supportsColor);
expectType<chalk.Chalk>(new chalk.Instance({level: 1}));

// -- Properties --
expectType<boolean>(chalk.enabled);
expectType<chalk.Level>(chalk.level);

// -- Template literal --
Expand Down
18 changes: 4 additions & 14 deletions readme.md
Expand Up @@ -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.

### 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});
```
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

### 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:

Expand Down Expand Up @@ -170,7 +160,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
- `inverse`- Inverse background and foreground colors.
- `hidden` - Prints the text, but makes it invisible.
- `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

Expand Down
12 changes: 1 addition & 11 deletions source/index.js
Expand Up @@ -28,7 +28,6 @@ const applyOptions = (object, options = {}) => {
// Detect level if not set manually
const colorLevel = stdoutColor ? stdoutColor.level : 0;
object.level = options.level === undefined ? colorLevel : options.level;
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
};

class ChalkClass {
Expand Down Expand Up @@ -120,15 +119,6 @@ const proto = Object.defineProperties(() => {}, {
set(level) {
this._generator.level = level;
}
},
enabled: {
enumerable: true,
get() {
return this._generator.enabled;
},
set(enabled) {
this._generator.enabled = enabled;
}
}
});

Expand Down Expand Up @@ -171,7 +161,7 @@ const createBuilder = (self, _styler, _isEmpty) => {
};

const applyStyle = (self, string) => {
if (!self.enabled || self.level <= 0 || !string) {
if (self.level <= 0 || !string) {
return self._isEmpty ? '' : string;
}

Expand Down
35 changes: 0 additions & 35 deletions test/enabled.js

This file was deleted.

10 changes: 1 addition & 9 deletions test/instance.js
Expand Up @@ -6,21 +6,13 @@ require('./_supports-color')(__dirname);
const chalk = require('../source');

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(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.level = 2;
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 => {
/* eslint-disable no-new */
t.throws(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/level.js
Expand Up @@ -14,7 +14,7 @@ test('don\'t output colors when manually disabled', t => {
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;
chalk.level = 1;
const {red} = chalk;
Expand Down
4 changes: 2 additions & 2 deletions test/no-color-support.js
Expand Up @@ -10,7 +10,7 @@ require('./_supports-color')(__dirname, {

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

test.failing('colors can be forced by using chalk.enabled', t => {
chalk.enabled = true;
test('colors can be forced by using chalk.level', t => {
chalk.level = 1;
t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m');
});
20 changes: 7 additions & 13 deletions test/visible.js
Expand Up @@ -5,40 +5,34 @@ require('./_supports-color')(__dirname);

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

test('visible: normal output when enabled', t => {
const instance = new chalk.Instance({level: 3, enabled: true});
test('visible: normal output when level > 0', t => {
const instance = new chalk.Instance({level: 3});
t.is(instance.visible.red('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 => {
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.red.visible('foo'), '');
});

test('test switching back and forth between enabled and disabled', t => {
const instance = new chalk.Instance({level: 3, enabled: true});
test('test switching back and forth between level == 0 and level > 0', t => {
const instance = new chalk.Instance({level: 3});
t.is(instance.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.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');

instance.enabled = false;
instance.level = 0;
t.is(instance.red('foo'), 'foo');
t.is(instance.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('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.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
Expand Down