-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
util: add colorText method #43371
Changes from all commits
7cd400f
9da85fe
baad390
f4814d4
d21bd40
d48eb4f
3bcbc5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -71,6 +71,25 @@ callbackFunction((err, ret) => { | |||||
}); | ||||||
``` | ||||||
|
||||||
## `util.colorText(format, text)` | ||||||
|
||||||
<!-- YAML | ||||||
added: REPLACEME | ||||||
--> | ||||||
|
||||||
* `format` {string} A color format defined in `util.inspect.colors`. | ||||||
* `text` {string} The text to color. | ||||||
* Returns: {string} Colored text string. | ||||||
|
||||||
Takes `format` and `text` and returns the colored text form | ||||||
|
||||||
```js | ||||||
const util = require('node:util'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||
|
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/shall be/is? |
||||||
``` | ||||||
|
||||||
## `util.debuglog(section[, callback])` | ||||||
|
||||||
<!-- YAML | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ const { debuglog } = require('internal/util/debuglog'); | |
const { | ||
validateFunction, | ||
validateNumber, | ||
validateString, | ||
} = require('internal/validators'); | ||
const { TextDecoder, TextEncoder } = require('internal/encoding'); | ||
const { isBuffer } = require('buffer').Buffer; | ||
|
@@ -331,12 +332,28 @@ function getSystemErrorName(err) { | |
return internalErrorName(err); | ||
} | ||
|
||
/** | ||
* @param {string} format | ||
* @param {string} text | ||
* @returns {string} | ||
*/ | ||
function colorText(format, text) { | ||
validateString(format, 'format'); | ||
validateString(text, 'text'); | ||
const formatCodes = inspect.colors[format]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking we could validate that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ yes! |
||
if (!ArrayIsArray(formatCodes)) { | ||
return text; | ||
} | ||
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; | ||
} | ||
|
||
// Keep the `exports =` so that various functions can still be monkeypatched | ||
module.exports = { | ||
_errnoException: errnoException, | ||
_exceptionWithHostPort: exceptionWithHostPort, | ||
_extend, | ||
callbackify, | ||
colorText, | ||
debug: debuglog, | ||
debuglog, | ||
deprecate, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
|
||
[ | ||
undefined, | ||
null, | ||
false, | ||
5n, | ||
5, | ||
Symbol(), | ||
].forEach((invalidOption) => { | ||
assert.throws(() => { | ||
util.colorText(invalidOption, 'test'); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => { | ||
util.colorText('red', invalidOption); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
}); | ||
|
||
assert.throws(() => { | ||
util.colorText('red', undefined); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: 'The "text" argument must be of type string. Received undefined' | ||
}); | ||
Comment on lines
+26
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this already be tested on line 20 when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, this should rather be |
||
|
||
assert.strictEqual(util.colorText('red', 'test'), '\u001b[31mtest\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.
Accepting
string | string[]
would be really helpful to e.g. make text both a certain color and underline/italic/bold/... at the same time