From 565fd50b4ac81c8c3fca6a08ffcbc59a4b7b2504 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 15 Mar 2018 14:10:43 +0100 Subject: [PATCH] console: auto-detect color support by default This makes Node pretty-print objects with color by default when `console.log()`-ing them. PR-URL: https://github.com/nodejs/node/pull/19372 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- doc/api/console.md | 2 +- lib/console.js | 2 +- test/pseudo-tty/console_colors.js | 9 +++++++++ test/pseudo-tty/console_colors.out | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/pseudo-tty/console_colors.js create mode 100644 test/pseudo-tty/console_colors.out diff --git a/doc/api/console.md b/doc/api/console.md index cce2a4eb6edf1d..59e5ae368569ff 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -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 diff --git a/lib/console.js b/lib/console.js index 2c35e9822346a7..f4610f745ea870 100644 --- a/lib/console.js +++ b/lib/console.js @@ -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({ diff --git a/test/pseudo-tty/console_colors.js b/test/pseudo-tty/console_colors.js new file mode 100644 index 00000000000000..dd16a0c028dccd --- /dev/null +++ b/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' }); diff --git a/test/pseudo-tty/console_colors.out b/test/pseudo-tty/console_colors.out new file mode 100644 index 00000000000000..f0ee5e42d60db5 --- /dev/null +++ b/test/pseudo-tty/console_colors.out @@ -0,0 +1,3 @@ +{ foo: *[32m'bar'*[39m } +string q +{ foo: *[32m'bar'*[39m } with object format param