Skip to content

Commit 2a53389

Browse files
stronciumsindresorhus
authored andcommitted
Add chalk.stderr (#359)
1 parent 6b4d206 commit 2a53389

9 files changed

Lines changed: 47 additions & 9 deletions

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
378378
ForegroundColor: ForegroundColor;
379379
BackgroundColor: BackgroundColor;
380380
Modifiers: Modifiers;
381+
stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false};
381382
};
382383

383384
export = chalk;

index.test-d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).hasBasic);
1616
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has256);
1717
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has16m);
1818

19+
// - stderr -
20+
expectType<chalk.Chalk>(chalk.stderr);
21+
expectType<chalk.ColorSupport | false>(chalk.stderr.supportsColor);
22+
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic);
23+
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has256);
24+
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has16m);
25+
26+
// -- `stderr` is not a member of the Chalk interface --
27+
expectError(chalk.reset.stderr);
28+
1929
// -- `supportsColor` is not a member of the Chalk interface --
2030
expectError(chalk.reset.supportsColor);
2131

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ Can be overridden by the user with the flags `--color` and `--no-color`. For sit
147147

148148
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
149149

150+
### chalk.stderr and chalk.stderr.supportsColor
151+
152+
`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.
153+
150154

151155
## Styles
152156

source/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const ansiStyles = require('ansi-styles');
3-
const {stdout: stdoutColor} = require('supports-color');
3+
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
44
const template = require('./templates');
55
const {
66
stringReplaceAll,
@@ -218,5 +218,9 @@ const chalkTag = (chalk, ...strings) => {
218218

219219
Object.defineProperties(Chalk.prototype, styles);
220220

221-
module.exports = Chalk(); // eslint-disable-line new-cap
222-
module.exports.supportsColor = stdoutColor;
221+
const chalk = Chalk(); // eslint-disable-line new-cap
222+
chalk.supportsColor = stdoutColor;
223+
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
224+
chalk.stderr.supportsColor = stderrColor;
225+
226+
module.exports = chalk;

test/_fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
22
const chalk = require('../source');
33

4-
console.log(chalk.hex('#ff6159')('test'));
4+
console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`);

test/_supports-color.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const DEFAULT = {
77
hasBasic: true,
88
has256: true,
99
has16m: true
10+
},
11+
stderr: {
12+
level: 3,
13+
hasBasic: true,
14+
has256: true,
15+
has16m: true
1016
}
1117
};
1218

test/chalk.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,8 @@ test('don\'t emit RGB codes if level is 0', t => {
105105
test('supports blackBright color', t => {
106106
t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m');
107107
});
108+
109+
test('sets correct level for chalk.stderr and respects it', t => {
110+
t.is(chalk.stderr.level, 3);
111+
t.is(chalk.stderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m');
112+
});

test/level.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ test('propagate enable/disable changes from child colors', t => {
4141

4242
test('disable colors if they are not supported', async t => {
4343
const {stdout} = await execa.node(path.join(__dirname, '_fixture'));
44-
t.is(stdout, 'test');
44+
t.is(stdout, 'testout testerr');
4545
});

test/no-color-support.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import test from 'ava';
22

33
// Spoof supports-color
44
require('./_supports-color')(__dirname, {
5-
level: 0,
6-
hasBasic: false,
7-
has256: false,
8-
has16m: false
5+
stdout: {
6+
level: 0,
7+
hasBasic: false,
8+
has256: false,
9+
has16m: false
10+
},
11+
stderr: {
12+
level: 0,
13+
hasBasic: false,
14+
has256: false,
15+
has16m: false
16+
}
917
});
1018

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

0 commit comments

Comments
 (0)