Skip to content

Commit

Permalink
feat(node/util): styleText (#22758)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored and littledivy committed Mar 8, 2024
1 parent 1064977 commit 1a217c9
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext/node/polyfills/internal/util/inspect.mjs
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
};

0 comments on commit 1a217c9

Please sign in to comment.