1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(node/util): styleText (#22758)

Implements https://github.com/nodejs/node/pull/51850
This commit is contained in:
Leo Kettmeir 2024-03-07 00:45:28 +01:00 committed by GitHub
parent 9df917c0be
commit 0fb67ce43e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,
};