diff --git a/index.d.ts b/index.d.ts index c650eb9..52dd5ae 100644 --- a/index.d.ts +++ b/index.d.ts @@ -378,6 +378,7 @@ declare const chalk: chalk.Chalk & chalk.ChalkFunction & { ForegroundColor: ForegroundColor; BackgroundColor: BackgroundColor; Modifiers: Modifiers; + stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false}; }; export = chalk; diff --git a/index.test-d.ts b/index.test-d.ts index 1d2df98..bc3d17a 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -16,6 +16,16 @@ expectType((chalk.supportsColor as chalk.ColorSupport).hasBasic); expectType((chalk.supportsColor as chalk.ColorSupport).has256); expectType((chalk.supportsColor as chalk.ColorSupport).has16m); +// - stderr - +expectType(chalk.stderr); +expectType(chalk.stderr.supportsColor); +expectType((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic); +expectType((chalk.stderr.supportsColor as chalk.ColorSupport).has256); +expectType((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 -- expectError(chalk.reset.supportsColor); diff --git a/readme.md b/readme.md index 0de3051..aaa8b47 100644 --- a/readme.md +++ b/readme.md @@ -147,6 +147,10 @@ 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. +### 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 convenience. + ## Styles diff --git a/source/index.js b/source/index.js index 9b4b46b..686758b 100644 --- a/source/index.js +++ b/source/index.js @@ -1,6 +1,6 @@ 'use strict'; const ansiStyles = require('ansi-styles'); -const {stdout: stdoutColor} = require('supports-color'); +const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color'); const template = require('./templates'); const { stringReplaceAll, @@ -218,5 +218,9 @@ const chalkTag = (chalk, ...strings) => { Object.defineProperties(Chalk.prototype, styles); -module.exports = Chalk(); // eslint-disable-line new-cap -module.exports.supportsColor = stdoutColor; +const chalk = Chalk(); // eslint-disable-line new-cap +chalk.supportsColor = stdoutColor; +chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap +chalk.stderr.supportsColor = stderrColor; + +module.exports = chalk; diff --git a/test/_fixture.js b/test/_fixture.js index 214a86c..29f34b0 100644 --- a/test/_fixture.js +++ b/test/_fixture.js @@ -1,4 +1,4 @@ 'use strict'; const chalk = require('../source'); -console.log(chalk.hex('#ff6159')('test')); +console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`); diff --git a/test/_supports-color.js b/test/_supports-color.js index 490c323..b5c0f78 100644 --- a/test/_supports-color.js +++ b/test/_supports-color.js @@ -7,6 +7,12 @@ const DEFAULT = { hasBasic: true, has256: true, has16m: true + }, + stderr: { + level: 3, + hasBasic: true, + has256: true, + has16m: true } }; diff --git a/test/chalk.js b/test/chalk.js index 2992a09..5e33f5e 100644 --- a/test/chalk.js +++ b/test/chalk.js @@ -105,3 +105,8 @@ test('don\'t emit RGB codes if level is 0', t => { test('supports blackBright color', t => { t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m'); }); + +test('sets correct level for chalk.stderr and respects it', t => { + t.is(chalk.stderr.level, 3); + t.is(chalk.stderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m'); +}); diff --git a/test/level.js b/test/level.js index 828de24..65d4720 100644 --- a/test/level.js +++ b/test/level.js @@ -41,5 +41,5 @@ test('propagate enable/disable changes from child colors', t => { test('disable colors if they are not supported', async t => { const {stdout} = await execa.node(path.join(__dirname, '_fixture')); - t.is(stdout, 'test'); + t.is(stdout, 'testout testerr'); }); diff --git a/test/no-color-support.js b/test/no-color-support.js index 7318452..ae88ac9 100644 --- a/test/no-color-support.js +++ b/test/no-color-support.js @@ -2,10 +2,18 @@ import test from 'ava'; // Spoof supports-color require('./_supports-color')(__dirname, { - level: 0, - hasBasic: false, - has256: false, - has16m: false + stdout: { + level: 0, + hasBasic: false, + has256: false, + has16m: false + }, + stderr: { + level: 0, + hasBasic: false, + has256: false, + has16m: false + } }); const chalk = require('../source');