styleText

external fun styleText(format: ForegroundColors, text: String): String(source)
external fun styleText(format: BackgroundColors, text: String): String(source)
external fun styleText(format: Modifiers, text: String): String(source)
external fun styleText(format: Array<Any>, text: String): String(source)

Stability: 1.1 - Active development

This function returns a formatted text considering the format passed.

const { styleText } = require('node:util');
const errorMessage = styleText('red', 'Error! Error!');
console.log(errorMessage);

util.inspect.colors also provides text formats such as italic, and underline and you can combine both:

console.log(
util.styleText(['underline', 'italic'], 'My italic underlined message'),
);

When passing an array of formats, the order of the format applied is left to right so the following style might overwrite the previous one.

console.log(
util.styleText(['red', 'green'], 'text'), // green
);

The full list of formats can be found in modifiers.

Since

v20.12.0

Parameters

format

A text format or an Array of text formats defined in util.inspect.colors.

text

The text to to be formatted.