Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ const { Console } = console;

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/60082
description: The `inspectOptions` option can be a `Map` from stream to options.
- version:
- v14.2.0
- v12.17.0
Expand Down Expand Up @@ -131,8 +134,9 @@ changes:
and the value returned by `getColorDepth()` on the respective stream. This
option can not be used, if `inspectOptions.colors` is set as well.
**Default:** `'auto'`.
* `inspectOptions` {Object} Specifies options that are passed along to
[`util.inspect()`][].
Comment on lines -134 to -135

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0000

* `inspectOptions` {Object|Map} Specifies options that are passed along to
[`util.inspect()`][]. Can be an options object or, if different options
for stdout and stderr are desired, a `Map` from stream objects to options.
* `groupIndentation` {number} Set group indentation.
**Default:** `2`.

Expand Down
23 changes: 17 additions & 6 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
Boolean,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
MapPrototypeGet,
MapPrototypeValues,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectKeys,
Expand Down Expand Up @@ -139,12 +141,20 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
if (inspectOptions !== undefined) {
validateObject(inspectOptions, 'options.inspectOptions');

if (inspectOptions.colors !== undefined &&
options.colorMode !== undefined) {
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
'options.inspectOptions.color', 'colorMode');
Comment on lines -142 to -145

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1

const inspectOptionsMap = isMap(inspectOptions) ?
inspectOptions : new SafeMap([
[stdout, inspectOptions],
[stderr, inspectOptions],
]);

for (const inspectOptions of MapPrototypeValues(inspectOptionsMap)) {
if (inspectOptions.colors !== undefined &&
options.colorMode !== undefined) {
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
'options.inspectOptions.color', 'colorMode');
}
}
optionsMap.set(this, inspectOptions);
optionsMap.set(this, inspectOptionsMap);
}

// Bind the prototype functions to this Console instance
Expand Down Expand Up @@ -316,7 +326,8 @@ ObjectDefineProperties(Console.prototype, {
color = lazyUtilColors().shouldColorize(stream);
}

const options = optionsMap.get(this);
const inspectOptionsMap = optionsMap.get(this);
const options = inspectOptionsMap ? MapPrototypeGet(inspectOptionsMap, stream) : undefined;
if (options) {
if (options.colors === undefined) {
options.colors = color;
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-console-tty-colors-per-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
require('../common');
const { Console } = require('console');
const { PassThrough } = require('stream');
const { strict: assert } = require('assert');

const stdout = new PassThrough().setEncoding('utf8');
const stderr = new PassThrough().setEncoding('utf8');

const console = new Console({
stdout,
stderr,
inspectOptions: new Map([
[stdout, { colors: true }],
[stderr, { colors: false }],
]),
});

console.log('Hello', 42);
console.warn('Hello', 42);

assert.strictEqual(stdout.read(), 'Hello \x1B[33m42\x1B[39m\n');
assert.strictEqual(stderr.read(), 'Hello 42\n');
Loading