-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
console: add color support #19372
console: add color support #19372
Conversation
ab68eef
to
a49b488
Compare
lib/console.js
Outdated
|
||
let MAX_STACK_MESSAGE; | ||
|
||
function Console(stdout, stderr, ignoreErrors = true) { | ||
function Console(stdout, stderr, ignoreErrors = true, colorMode = 'auto') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the fourth positional argument to the constructor, maybe we should consider an options object at some point (unless it is foreseeable that we won't add more options to the constructor).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I’m happy to do that if you think it’s a good idea.
I don’t think an ergonomic API matters all that much here, because almost nobody is constructing their own console
objects, but I could totally see the argument being made for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shouldn't argue for either side, I never use the constructor, it's just a general concern I'd have with any API. If you type-check colorMode
strictly, we can still replace it with an options object later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we do go for an options argument, it should probably encompass all arguments here – so, new Console({ stdout, stderr, ignoreErrors, colorMode, ... })
.
I’m not sure how well that would work, though, because all of these properties could just be accidentally set on the stdout
object itself…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An options object would be best, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasnell Do you have thoughts on how we would detect an options object?
I guess we could try to see whether it’s a duck and has a write()
property…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Console(stdout, [stderr, [options]])
? That kind of makes sense as ignoreErrors
and colorMode
have reasonable defaults and can be distinguished from an object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If going with the options property I would use the single argument strategy you suggest above and check if the first argument is an object with at least the stdin property set that does not have a write or maybe _write property whose value is a function.
doc/api/util.md
Outdated
--> | ||
|
||
This function is identical to [`util.format()`][], except in that it takes | ||
an `inspectOptions` argument which specifies default options for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: "default"
as in "unless overriden by custom inspect functions"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think default
can be dropped here. :) It’s not really overridden unless it’s a format string with %o
in it, but we already document that.
a49b488
to
f7b435f
Compare
lib/console.js
Outdated
@@ -62,6 +67,7 @@ function Console(stdout, stderr, ignoreErrors = true) { | |||
Object.defineProperty(this, '_stderrErrorHandler', prop); | |||
|
|||
this[kCounts] = new Map(); | |||
this[kColorMode] = colorMode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor should check for invalid values before assigning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 done!
@addaleax Do you think if we should support a subset of the E.g. That is a part of console spec and works in major browsers, and I suppose it could be relatively easy implemented with the specific set of numbered colors that are supported by terminals. |
f7b435f
to
a260101
Compare
I think that would be awesome. But it’s also more or less independent of this PR, right? |
@addaleax Yes, that could be discussed separately. |
a260101
to
c4e417b
Compare
doc/api/console.md
Outdated
* `colorMode` {boolean|string} Set color support for this `Console` instance. | ||
Setting to `true` enables coloring while inspecting values, setting to | ||
`'auto'` will make color support depend on the value of the `isTTY` property | ||
of the respective stream. Defaults to `auto`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: 'auto'
for consistency.
tempStr = inspect(arguments[a++], | ||
{ showHidden: true, showProxy: true }); | ||
{ | ||
const opts = Object.assign({}, inspectOptions, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: how about
const opts = Object.assign({
showHidden: true,
showProxy: true
}, inspectOptions);
Feel free to ignore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lpinca This is kind of intentional … we document that the %o
specifier works this way, so I wouldn’t expect per-call options to override it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what case would it be overridden? It's still a new copy per call no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lpinca I meant, if inspectOptions
happens to contains showProxy: false
, then that would override the behaviour of %o
with your suggestion, whereas right now the behaviour of %o
is left untouched.
If you do think that that is the right thing to do, then I’m okay with that; I’d prefer to keep this as it is currently documented, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, ignore my comment.
c4e417b
to
1e87ee1
Compare
1e87ee1
to
30cc7e0
Compare
30cc7e0
to
77d93e3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, just temporarily requesting changes so that you get a chance to see #19652 (comment) before anyone lands this.
Actual PR content looks good to me. Once you have had a chance to read it (and consider adding a flag maybe?) please feel free to dismiss this review :)
77d93e3
to
466594c
Compare
@benjamingr I’m not sure I quite understand what you are pointing at … are you worried that libraries might have to clean up Node’s output after this patch? I don’t think we need to worry about that, since this feature is only enabled by default if output goes to a TTY that has some indication for color support. In any case, thanks for the ping, I rebased this :) |
Dismissing my review since @addaleax saw the ping.
I'm not familiar with Jest's internals and I was worried that it might be problematic for tooling folk that uses it. If you're sure that's not an issue then I think this is a cool feature to have :D |
@benjamingr I guess it’s not a bad idea to run CITGM on this, since I’m not familiar with Jests internals either, but yes, it seems highly unlikely that this is going to be an issue for them… The coloring is not going to show up if Node’s output is captured from a child process or something like that. |
doc/api/console.md
Outdated
description: The `ignoreErrors` option was introduced. | ||
- version: REPLACEME | ||
pr-url: https://github.com/nodejs/node/pull/19372 | ||
description: The `Console` constructor now supports an options argument, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options
-> `options`
?
doc/api/console.md
Outdated
and the `colorMode` option was introduced. | ||
--> | ||
|
||
* `options` {object} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{object}
-> {Object}
doc/api/console.md
Outdated
* `stdout` {stream.Writable} | ||
* `stderr` {stream.Writable} | ||
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying | ||
streams. Defaults to `true`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to `true`.
-> **Default:** `true`.
doc/api/console.md
Outdated
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. | ||
Defaults to `'auto'`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to `'auto'`.
-> **Default:** `'auto'`.
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is worth to just mention the parameter types:
* `inspectOptions` {Object}
* `format` {string}
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`.
Add a way to tell `Console` instances to either always use, never use or auto-detect color support and inspect objects accordingly.
This makes Node pretty-print objects with color by default when `console.log()`-ing them.
e5daa32
to
0e52d4b
Compare
@vsemozhetbyt Thanks for the help, addressed your nits :) CI: https://ci.nodejs.org/job/node-test-commit/17679/ |
Landed in 039cdeb...565fd50 |
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add a way to tell `Console` instances to either always use, never use or auto-detect color support and inspect objects accordingly. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
This makes Node pretty-print objects with color by default when `console.log()`-ing them. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add a way to tell `Console` instances to either always use, never use or auto-detect color support and inspect objects accordingly. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
This makes Node pretty-print objects with color by default when `console.log()`-ing them. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`. PR-URL: nodejs#19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: nodejs#19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: nodejs#19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
Add a way to tell `Console` instances to either always use, never use or auto-detect color support and inspect objects accordingly. PR-URL: nodejs#19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
This makes Node pretty-print objects with color by default when `console.log()`-ing them. PR-URL: nodejs#19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
is this something we should backport to v8.x? |
util: introduce
formatWithOptions()
Identical to
format()
except that it takes an options argumentthat is passed through to
inspect()
.doc: document
Console(…, ignoreErrors)
optionconsole: add color support
Add a way to tell
Console
instances to either always use, never useor auto-detect color support and inspect objects accordingly.
console: auto-detect color support by default
This makes Node pretty-print objects with color by default
when
console.log()
-ing them.The last commit could be split out into a separate PR if that’s preferred.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes