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

util: add colorText method #43371

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/api/util.md
Expand Up @@ -71,6 +71,25 @@ callbackFunction((err, ret) => {
});
```

## `util.colorText(format, text)`

<!-- YAML
added: v18.3.0
hemanth marked this conversation as resolved.
Show resolved Hide resolved
-->

* `format` {string} `format` one of the color format from `util.inspect.colors`
Copy link
Contributor

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.

hemanth marked this conversation as resolved.
Show resolved Hide resolved
* `text` {string} The text you would like to color
hemanth marked this conversation as resolved.
Show resolved Hide resolved
* Returns: {string} colored text string
hemanth marked this conversation as resolved.
Show resolved Hide resolved

Takes `format` and `text` and retuns the colored text form
hemanth marked this conversation as resolved.
Show resolved Hide resolved

```js
const util = require('node:util');
Copy link
Contributor

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.


console.log(util.colorText('red', 'This text shall be in red color'));
// ^ '\u001b[31mThis text shall be in red color\u001b[39m'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// ^ '\u001b[31mThis text shall be in red color\u001b[39m'
// ^ '\u001b[31mThis text shall be colored red\u001b[39m'

Copy link
Member

Choose a reason for hiding this comment

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

s/shall be/is?

```

## `util.debuglog(section[, callback])`

<!-- YAML
Expand Down
17 changes: 17 additions & 0 deletions lib/util.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Copy link
Contributor

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.

Copy link
Contributor Author

@hemanth hemanth Jun 11, 2022

Choose a reason for hiding this comment

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

additional validation

like?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-util-colorText.js
@@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

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


assert.strictEqual(util.colorText('red', 'test'), '\u001b[31mtest\u001b[39m');