Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.3k
util: add colorText method#43371
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
util: add colorText method #43371
Uh oh!
There was an error while loading. Please reload this page.
Conversation
hemanth commented Jun 10, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ljharb commented Jun 10, 2022
Name sounds good to me; i don't think it should be in Is there any way to have tests that actually verify that the expected colors show up on supported platforms? |
cjihrig left a comment
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.
Left some preliminary comments.
Is there any way to have tests that actually verify that the expected colors show up on supported platforms?
test/parallel/test-util-format.js and test/parallel/test-util-inspect.js might be good places to look for examples.
Also cc @BridgeAR who might be interested since this overlaps with the color support in util.inspect().
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| functioncolorText(format,text){ | ||
| validateString(format,'format'); | ||
| validateString(text,'text'); | ||
| constformatCodes=inspect.colors[format]; |
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 this is going to be public API, we should probably provide some additional validation for supported formats.
hemanthJun 11, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
additional validation
like?
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 was thinking we could validate that format is actually something supported by inspect.colors, but feel free to ignore if you want.
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 they pass anything apart as of now, we are just returning the text as in, do you feel it makes sense to check if the format is supported by inspect.colors and throw an error for non-supported inputs?
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.
Yes, that's what I originally meant.
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.
Is inspect.colors mutable and exposed to users?
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.
^ yes!
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
9c94ffe to 9ac1a84CompareTrott commented Jun 13, 2022
Micro-nit on the commit message: The verb in the first line of the commit message should be imperative. In other words, |
hemanth commented Jun 13, 2022
Thanks for the clarification @Trott. I had that doubt, fixed it. |
nodejs-github-bot commented Jun 14, 2022
hemanth commented Jun 14, 2022
We should also add this to the |
cjihrig commented Jun 14, 2022
Yes, this would require a docs update. |
hemanth commented Jun 17, 2022
Is this ready enough? |
cjihrig left a comment
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.
Left a few comments. It would be good for others to weigh in on things like the overall API design and whether or not we should make this experimental first.
Uh oh!
There was an error while loading. Please reload this page.
doc/api/util.md Outdated
| added: v18.3.0 | ||
| --> | ||
| *`format`{string} `format` one of the color format from `util.inspect.colors` |
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.
We should probably link util.inspect.colors to https://nodejs.org/api/util.html#customizing-utilinspect-colors.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| Takes `format` and `text` and retuns the colored text form | ||
| ```js | ||
| constutil=require('node:util'); |
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.
Can you remove this line. Otherwise we'll need to show the same example code for CJS and ESM.
| constutil=require('node:util'); | ||
| console.log(util.colorText('red', 'This text shall be in red color')); | ||
| // ^ '\u001b[31mThis text shall be in red color\u001b[39m' |
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.
| // ^ '\u001b[31mThis text shall be in red color\u001b[39m' | |
| // ^ '\u001b[31mThis text shall be colored red\u001b[39m' |
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.
s/shall be/is?
| functioncolorText(format,text){ | ||
| validateString(format,'format'); | ||
| validateString(text,'text'); | ||
| constformatCodes=inspect.colors[format]; |
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.
Yes, that's what I originally meant.
| assert.throws(()=>{ | ||
| util.colorText('red',undefined); | ||
| },{ | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| message: 'The "text" argument must be of type string. Received undefined' | ||
| }); |
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.
Wouldn't this already be tested on line 20 when invalidOption is undefined?
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.
Right, this should rather be util.colorText(undefined, undefined);
Uh oh!
There was an error while loading. Please reload this page.
| constutil=require('node:util'); | ||
| console.log(util.colorText('red', 'This text shall be in red color')); | ||
| // ^ '\u001b[31mThis text shall be in red color\u001b[39m' |
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.
s/shall be/is?
| functioncolorText(format,text){ | ||
| validateString(format,'format'); | ||
| validateString(text,'text'); | ||
| constformatCodes=inspect.colors[format]; |
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.
Is inspect.colors mutable and exposed to users?
Co-authored-by: Colin Ihrig <[email protected]>
Co-authored-by: Colin Ihrig <[email protected]>
Co-authored-by: Colin Ihrig <[email protected]>
Co-authored-by: Colin Ihrig <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
BridgeAR left a comment • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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 am hesitant to add new formatting functionalities to util (console does not seem ideal either). I just opened a something similar API: #43523. It does a bit more by also allowing nested colors, this functionality here does not do that (e.g., colorText('green', 'green ' + colorText('red', 'red') + ' green') would end up being colored: green red defaultColor instead of green red green).
jasnell commented Jun 27, 2022
Let's throw this into the TSC agenda as part of the discussion for #43523 |
mcollina commented Jul 13, 2022
Dropping it from the tsc-agenda until #43382 is resolved. |
| added: REPLACEME | ||
| --> | ||
| *`format`{string} A color format defined in `util.inspect.colors`. |
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.
Accepting string | string[] would be really helpful to e.g. make text both a certain color and underline/italic/bold/... at the same time
mcollina commented Mar 7, 2024
Superseded by #51850 |
This is with reference to the comment in #42770
A few things to finalize before completing this draft PR:
consoleAPI?//cc @nodejs/util