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

Add chalk.stderr #359

Merged
merged 4 commits into from Sep 22, 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
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -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;
10 changes: 10 additions & 0 deletions index.test-d.ts
Expand Up @@ -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).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 --
expectError(chalk.reset.supportsColor);

Expand Down
4 changes: 4 additions & 0 deletions readme.md
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions 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,
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion 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')}`);
6 changes: 6 additions & 0 deletions test/_supports-color.js
Expand Up @@ -7,6 +7,12 @@ const DEFAULT = {
hasBasic: true,
has256: true,
has16m: true
},
stderr: {
level: 3,
hasBasic: true,
has256: true,
has16m: true
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/chalk.js
Expand Up @@ -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');
});
stroncium marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion test/level.js
Expand Up @@ -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');
});
16 changes: 12 additions & 4 deletions test/no-color-support.js
Expand Up @@ -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');
Expand Down