node icon indicating copy to clipboard operation
node copied to clipboard

console: colorize console error and warn

Open MrJithil opened this issue 1 year ago • 31 comments

Related to : 40361

Colorise string type args of console.error and console.warn. We are skipping the non-string types.

Tasks:

  • [ ] Run CITGM

MrJithil avatar Feb 01 '24 08:02 MrJithil

Will add test cases if this feature is acceptable

MrJithil avatar Feb 01 '24 08:02 MrJithil

@MoLow I have changed the value of clear from '\u001bc' to '\u001b[0m'. Please check.

MrJithil avatar Feb 05 '24 11:02 MrJithil

I believe it's important to run through citgm to gauge the impact of this change.

ruyadorno avatar Feb 05 '24 18:02 ruyadorno

I once again believe this should be marked as semver major change: https://github.com/nodejs/node/pull/51503

marco-ippolito avatar Mar 15 '24 15:03 marco-ippolito

There are some values which look quite broken with this change:

  • ./node -e 'console.warn(new Error);console.error(new Error)' shows that that color stays until the first greyed line in the stack.
  • ./node -e 'console.error(Promise.resolve(2))'
  • ./node -e 'console.error(new ArrayBuffer)'

Also worth noting other primitive values (number, BigInt, functions, symbols):

  • ./node -e 'console.error(0)' is still yellow.
  • ./node -e 'console.error(Symbol("test"))' is still green.

I wonder if it would make more sense to apply the style only to strings, and leave the other types of value untouched.

aduh95 avatar Mar 15 '24 16:03 aduh95

There are some values which look quite broken with this change:

  • ./node -e 'console.warn(new Error);console.error(new Error)' shows that that color stays until the first greyed line in the stack.
  • ./node -e 'console.error(Promise.resolve(2))'
  • ./node -e 'console.error(new ArrayBuffer)'

Also worth noting other primitive values (number, BigInt, functions, symbols):

  • ./node -e 'console.error(0)' is still yellow.
  • ./node -e 'console.error(Symbol("test"))' is still green.

I wonder if it would make more sense to apply the style only to strings, and leave the other types of value untouched.

Thanks for the suggestions. Now, we will colorise only for string types. The other improvements mentioned here for existing cases, will handle in a different issue or PR.

MrJithil avatar Mar 20 '24 10:03 MrJithil

How to run CITGM?

MrJithil avatar Mar 22 '24 02:03 MrJithil

@aduh95 Please review.

MrJithil avatar Mar 22 '24 08:03 MrJithil

@ruyadorno how to run the citgm? Using the citgm-smoker jenkin?

MrJithil avatar Apr 01 '24 09:04 MrJithil

CI: https://ci.nodejs.org/job/node-test-pull-request/58027/

nodejs-github-bot avatar Apr 01 '24 09:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58046/

nodejs-github-bot avatar Apr 02 '24 02:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58107/

nodejs-github-bot avatar Apr 04 '24 11:04 nodejs-github-bot

I think something like special printing Error in console.error|console.warn could be a potential evolution of this implementation, e.g:

   $ node -e 'console.error(new Error("ERR"))'
-  Error: ERR
     at [eval]:1:15
     at runScriptInThisContext (node:internal/vm:209:10)
     at node:internal/process/execution:118:14
     at [eval]-wrapper:6:24
     at runScript (node:internal/process/execution:101:62)
     at evalScript (node:internal/process/execution:133:3)
     at node:internal/main/eval_string:51:3

But as you can see in my little illustration above, I would prefer it to be just a highlight of that header rather than the entire error stack - in order for that bit to be a proper highlight - instead of an entire coloured blob of text that ultimately diminishes its meaning.

ruyadorno avatar Apr 04 '24 15:04 ruyadorno

would prefer it to be just a highlight of that header rather than the entire error stack - in order for that bit to be a proper highlight - instead of an

Currently we are skipping the non-string types. So, node -e 'console.error(new Error("ERR"))'. will be skipped colouring.

MrJithil avatar Apr 05 '24 08:04 MrJithil

I'm sorry @MrJithil but while this is a good work in progress and I initially had no comments, I took some time thinking a bit more about this change and I currently have 2 objections to landing this PR:

  • Colouring the entire output might ultimately diminish the meaning of that colouring and only makes the entire output hard to read (see Use color with intention).
  • We recently landed util.styleText which might be more suitable for this use case since users can now chose what part to color from their output using a native helper from the runtime. If this PR is to land as-is it should be using that API or its internals.

One other thought I currently have in my mind is that if there is even any prior art from other runtimes on colouring error / warn output by default similar to how it's implemented here. If there are examples of that it could then be used as supporting argument for this change.

There is no specific request which demands the urgency of this feature. I thought it would be great if have something like this in REPL. Like browsers, it would be great to differentiate an error, warn and log from its colour.

As you suggested styleText can be used to colour the warn and error.

But, one question here, in REPL don't we need to check whether the tty supports colouring before adding colour codes?

MrJithil avatar Apr 05 '24 08:04 MrJithil

Currently we are skipping the non-string types. So, node -e 'console.error(new Error("ERR"))'. will be skipped colouring.

Then I don't think this PR fixes #40361, might be better to update its description.

There is no specific request which demands the urgency of this feature. I thought it would be great if have something like this in REPL. Like browsers, it would be great to differentiate an error, warn and log from its colour.

I'm happy to approve this PR if it applies to REPL-only but I don't believe this to be the case at the moment.

ruyadorno avatar Apr 09 '24 18:04 ruyadorno

Currently we are skipping the non-string types. So, node -e 'console.error(new Error("ERR"))'. will be skipped colouring.

Then I don't think this PR fixes #40361, might be better to update its description.

There is no specific request which demands the urgency of this feature. I thought it would be great if have something like this in REPL. Like browsers, it would be great to differentiate an error, warn and log from its colour.

I'm happy to approve this PR if it applies to REPL-only but I don't believe this to be the case at the moment.

Its not only for REPL. It will be applicable to all node console output.

MrJithil avatar Apr 10 '24 10:04 MrJithil

CI: https://ci.nodejs.org/job/node-test-pull-request/58246/

nodejs-github-bot avatar Apr 10 '24 19:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58420/

nodejs-github-bot avatar Apr 16 '24 04:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58629/

nodejs-github-bot avatar Apr 23 '24 09:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58630/

nodejs-github-bot avatar Apr 23 '24 09:04 nodejs-github-bot

CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3422

ruyadorno avatar Apr 23 '24 13:04 ruyadorno

CI: https://ci.nodejs.org/job/node-test-pull-request/58654/

nodejs-github-bot avatar Apr 24 '24 05:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58682/

nodejs-github-bot avatar Apr 25 '24 03:04 nodejs-github-bot

I thought a bit more about this after talking with a few other folks and changed my mind on the remaining objection. LGTM 👍

thank you. proceeding further.

MrJithil avatar Apr 25 '24 03:04 MrJithil

CI: https://ci.nodejs.org/job/node-test-pull-request/58691/

nodejs-github-bot avatar Apr 25 '24 09:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58692/

nodejs-github-bot avatar Apr 25 '24 11:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58697/

nodejs-github-bot avatar Apr 25 '24 14:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58710/

nodejs-github-bot avatar Apr 26 '24 07:04 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/58746/

nodejs-github-bot avatar Apr 27 '24 14:04 nodejs-github-bot