add chalk.stderr

This commit is contained in:
Yanis Benson 2019-07-22 06:20:27 +03:00
parent 6b4d20683f
commit a0a5cc536c
9 changed files with 47 additions and 9 deletions

1
index.d.ts vendored
View file

@ -378,6 +378,7 @@ declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
ForegroundColor: ForegroundColor; ForegroundColor: ForegroundColor;
BackgroundColor: BackgroundColor; BackgroundColor: BackgroundColor;
Modifiers: Modifiers; Modifiers: Modifiers;
stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false};
}; };
export = chalk; export = chalk;

View file

@ -16,6 +16,16 @@ expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).hasBasic);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has256); expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has256);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has16m); expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has16m);
// - stderr -
expectType<chalk.Chalk>(chalk.stderr);
expectType<chalk.ColorSupport | false>(chalk.stderr.supportsColor);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has256);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has16m);
// -- `stderr` is not a member of the Chalk interface --
expectError(chalk.reset.stderr);
// -- `supportsColor` is not a member of the Chalk interface -- // -- `supportsColor` is not a member of the Chalk interface --
expectError(chalk.reset.supportsColor); expectError(chalk.reset.supportsColor);

View file

@ -147,6 +147,11 @@ Can be overridden by the user with the flags `--color` and `--no-color`. For sit
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively. Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
### chalk.stderr and chalk.stderr.supportsColor
`chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`.
Override rules from `chalk.supportsColor` apply to this too.
`chalk.stderr.supportsColor` is exposed for convinience.
## Styles ## Styles

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const ansiStyles = require('ansi-styles'); const ansiStyles = require('ansi-styles');
const {stdout: stdoutColor} = require('supports-color'); const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
const template = require('./templates'); const template = require('./templates');
const { const {
stringReplaceAll, stringReplaceAll,
@ -218,5 +218,9 @@ const chalkTag = (chalk, ...strings) => {
Object.defineProperties(Chalk.prototype, styles); Object.defineProperties(Chalk.prototype, styles);
module.exports = Chalk(); // eslint-disable-line new-cap const chalk = Chalk(); // eslint-disable-line new-cap
module.exports.supportsColor = stdoutColor; chalk.supportsColor = stdoutColor;
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
chalk.stderr.supportsColor = stderrColor;
module.exports = chalk;

View file

@ -1,4 +1,4 @@
'use strict'; 'use strict';
const chalk = require('../source'); const chalk = require('../source');
console.log(chalk.hex('#ff6159')('test')); console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`);

View file

@ -7,6 +7,12 @@ const DEFAULT = {
hasBasic: true, hasBasic: true,
has256: true, has256: true,
has16m: true has16m: true
},
stderr: {
level: 3,
hasBasic: true,
has256: true,
has16m: true
} }
}; };

View file

@ -105,3 +105,7 @@ test('don\'t emit RGB codes if level is 0', t => {
test('supports blackBright color', t => { test('supports blackBright color', t => {
t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m'); t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m');
}); });
test('sets correct level for chalk.stderr', t => {
t.is(chalk.stderr.level, 3);
});

View file

@ -41,5 +41,5 @@ test('propagate enable/disable changes from child colors', t => {
test('disable colors if they are not supported', async t => { test('disable colors if they are not supported', async t => {
const {stdout} = await execa.node(path.join(__dirname, '_fixture')); const {stdout} = await execa.node(path.join(__dirname, '_fixture'));
t.is(stdout, 'test'); t.is(stdout, 'testout testerr');
}); });

View file

@ -2,10 +2,18 @@ import test from 'ava';
// Spoof supports-color // Spoof supports-color
require('./_supports-color')(__dirname, { require('./_supports-color')(__dirname, {
level: 0, stdout: {
hasBasic: false, level: 0,
has256: false, hasBasic: false,
has16m: false has256: false,
has16m: false
},
stderr: {
level: 0,
hasBasic: false,
has256: false,
has16m: false
}
}); });
const chalk = require('../source'); const chalk = require('../source');