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 Mar 6, 2024
1 parent 9df917c commit 0fb67ce
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext/node/polyfills/internal/util/inspect.mjs
Original file line number Diff line number Diff line change
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 0fb67ce

Please sign in to comment.