Skip to content

Commit

Permalink
console: auto-detect color support by default
Browse files Browse the repository at this point in the history
This makes Node pretty-print objects with color by default
when `console.log()`-ing them.
  • Loading branch information
addaleax committed Apr 12, 2018
1 parent 313f29c commit 0e52d4b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/api/console.md
Expand Up @@ -100,7 +100,7 @@ changes:
Setting to `true` enables coloring while inspecting values, setting to
`'auto'` will make color support depend on the value of the `isTTY` property
and the value returned by `getColorDepth()` on the respective stream.
**Default:** `false`
**Default:** `'auto'`

Creates a new `Console` with one or two writable stream instances. `stdout` is a
writable stream to print log or info output. `stderr` is used for warning or
Expand Down
2 changes: 1 addition & 1 deletion lib/console.js
Expand Up @@ -68,7 +68,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
stdout,
stderr = stdout,
ignoreErrors = true,
colorMode = false
colorMode = 'auto'
} = options);
} else {
return new Console({
Expand Down
9 changes: 9 additions & 0 deletions test/pseudo-tty/console_colors.js
@@ -0,0 +1,9 @@
'use strict';
require('../common');
// Make this test OS-independent by overriding stdio getColorDepth().
process.stdout.getColorDepth = () => 8;
process.stderr.getColorDepth = () => 8;

console.log({ foo: 'bar' });
console.log('%s q', 'string');
console.log('%o with object format param', { foo: 'bar' });
3 changes: 3 additions & 0 deletions test/pseudo-tty/console_colors.out
@@ -0,0 +1,3 @@
{ foo: *[32m'bar'*[39m }
string q
{ foo: *[32m'bar'*[39m } with object format param

0 comments on commit 0e52d4b

Please sign in to comment.