From 0fb67ce43ea828db17fefc8df0ef8ec30b1a25c3 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Thu, 7 Mar 2024 00:45:28 +0100 Subject: [PATCH] feat(node/util): styleText (#22758) Implements https://github.com/nodejs/node/pull/51850 --- ext/node/polyfills/internal/util/inspect.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ext/node/polyfills/internal/util/inspect.mjs b/ext/node/polyfills/internal/util/inspect.mjs index f702c39c5d..2f9167dfb2 100644 --- a/ext/node/polyfills/internal/util/inspect.mjs +++ b/ext/node/polyfills/internal/util/inspect.mjs @@ -25,6 +25,7 @@ import { validateObject, + validateOneOf, validateString, } from "ext:deno_node/internal/validators.mjs"; import { codes } from "ext:deno_node/internal/error_codes.ts"; @@ -562,10 +563,20 @@ export function stripVTControlCharacters(str) { return str.replace(ansi, ""); } +export function styleText(format, text) { + validateString(text, "text"); + const formatCodes = inspect.colors[format]; + if (formatCodes == null) { + validateOneOf(format, "format", Object.keys(inspect.colors)); + } + return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`; +} + export default { format, getStringWidth, inspect, stripVTControlCharacters, formatWithOptions, + styleText, };