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

console: add color support #19372

Closed
wants to merge 5 commits into from
Closed

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Mar 15, 2018

  • util: introduce formatWithOptions()

    Identical to format() except that it takes an options argument
    that is passed through to inspect().

  • doc: document Console(…, ignoreErrors) option

  • console: add color support

    Add a way to tell Console instances to either always use, never use
    or 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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@addaleax addaleax added util Issues and PRs related to the built-in util module. semver-minor PRs that contain new features and should be released in the next minor version. console Issues and PRs related to the console subsystem. labels Mar 15, 2018
@nodejs-github-bot nodejs-github-bot added the util Issues and PRs related to the built-in util module. label Mar 15, 2018
@addaleax addaleax force-pushed the console-tty-colors branch 2 times, most recently from ab68eef to a49b488 Compare March 15, 2018 13:23
lib/console.js Outdated

let MAX_STACK_MESSAGE;

function Console(stdout, stderr, ignoreErrors = true) {
function Console(stdout, stderr, ignoreErrors = true, colorMode = 'auto') {
Copy link
Member

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).

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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…

Copy link
Member

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.

Copy link
Member Author

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…

Copy link
Member

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.

Copy link
Member

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
Copy link
Member

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"?

Copy link
Member Author

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.

@addaleax addaleax force-pushed the console-tty-colors branch from a49b488 to f7b435f Compare March 15, 2018 18:45
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;
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 done!

@ChALkeR
Copy link
Member

ChALkeR commented Mar 15, 2018

@addaleax Do you think if we should support a subset of the %c feature?

E.g. console.log('%cgreen%c, normal', 'color:green', '').
In this example, first %c sets the green style, second %c resets by applying empty styles.

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.

@addaleax addaleax force-pushed the console-tty-colors branch from f7b435f to a260101 Compare March 15, 2018 20:35
@addaleax
Copy link
Member Author

Do you think if we should support a subset of the %c feature?

I think that would be awesome. But it’s also more or less independent of this PR, right?

@ChALkeR
Copy link
Member

ChALkeR commented Mar 15, 2018

@addaleax Yes, that could be discussed separately.

@addaleax
Copy link
Member Author

@jasnell @tniessen Okay, updated this to use an options object. PTAL

* `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`.
Copy link
Member

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, {
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@addaleax addaleax force-pushed the console-tty-colors branch from c4e417b to 1e87ee1 Compare March 18, 2018 16:33
@BridgeAR BridgeAR self-requested a review March 20, 2018 23:32
@addaleax addaleax force-pushed the console-tty-colors branch from 1e87ee1 to 30cc7e0 Compare March 23, 2018 12:29
@addaleax
Copy link
Member Author

addaleax commented Mar 23, 2018

@addaleax addaleax force-pushed the console-tty-colors branch from 30cc7e0 to 77d93e3 Compare March 23, 2018 13:20
Copy link
Member

@benjamingr benjamingr left a 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 :)

@addaleax addaleax force-pushed the console-tty-colors branch from 77d93e3 to 466594c Compare March 28, 2018 12:37
@addaleax
Copy link
Member Author

@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 :)

@benjamingr benjamingr dismissed their stale review March 28, 2018 12:42

Dismissing my review since @addaleax saw the ping.

@benjamingr
Copy link
Member

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

@addaleax
Copy link
Member Author

@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.

@addaleax addaleax added the blocked PRs that are blocked by other issues or PRs. label Mar 28, 2018
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

options -> `options`?

and the `colorMode` option was introduced.
-->

* `options` {object}
Copy link
Contributor

Choose a reason for hiding this comment

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

{object} -> {Object}

* `stdout` {stream.Writable}
* `stderr` {stream.Writable}
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying
streams. Defaults to `true`.
Copy link
Contributor

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`.

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'`.
Copy link
Contributor

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
-->

Copy link
Contributor

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.
@addaleax addaleax force-pushed the console-tty-colors branch from e5daa32 to 0e52d4b Compare April 12, 2018 14:19
@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 12, 2018
@addaleax
Copy link
Member Author

addaleax commented Apr 12, 2018

@vsemozhetbyt Thanks for the help, addressed your nits :)

CI: https://ci.nodejs.org/job/node-test-commit/17679/
CI²: https://ci.nodejs.org/job/node-test-commit/17685/

@addaleax
Copy link
Member Author

Landed in 039cdeb...565fd50

@addaleax addaleax closed this Apr 12, 2018
@addaleax addaleax deleted the console-tty-colors branch April 12, 2018 21:32
@addaleax addaleax removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 12, 2018
addaleax added a commit that referenced this pull request Apr 12, 2018
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]>
addaleax added a commit that referenced this pull request Apr 12, 2018
PR-URL: #19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
addaleax added a commit that referenced this pull request Apr 12, 2018
PR-URL: #19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
addaleax added a commit that referenced this pull request Apr 12, 2018
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]>
addaleax added a commit that referenced this pull request Apr 12, 2018
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]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
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]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
PR-URL: #19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
PR-URL: #19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
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]>
jasnell pushed a commit that referenced this pull request Apr 16, 2018
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]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
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]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
PR-URL: nodejs#19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
PR-URL: nodejs#19372
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
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]>
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 1, 2018
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]>
@MylesBorins
Copy link
Contributor

is this something we should backport to v8.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. semver-minor PRs that contain new features and should be released in the next minor version. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants