From 6fd0043f6bf766cc488a88339059e8879fa07161 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 17 Aug 2023 20:56:52 -0700 Subject: Add `util.inspect.custom` support to `util.inspect/Bun.inspect/console.log` (#4194) * start work on util.inspect.custom * asdf * finish util inspect custom inspect * inspect * fix tests * revert * tidy * revert * oops * test * fix issues --- src/js/builtins/UtilInspect.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/js/builtins/UtilInspect.ts (limited to 'src/js/builtins/UtilInspect.ts') diff --git a/src/js/builtins/UtilInspect.ts b/src/js/builtins/UtilInspect.ts new file mode 100644 index 000000000..586f03b57 --- /dev/null +++ b/src/js/builtins/UtilInspect.ts @@ -0,0 +1,17 @@ +type Inspect = typeof import("util").inspect; + +// This is passed to [util.inspect.custom](..., { stylize }) to help users colorize parts. +export function getStylizeWithColor(inspect: Inspect) { + return function stylizeWithColor(str: string, styleType: string) { + const style = inspect.styles[styleType]; + if (style !== undefined) { + const color = inspect.colors[style]; + if (color !== undefined) return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`; + } + return str; + }; +} + +export function stylizeWithNoColor(str: string) { + return str; +} -- cgit v1.2.3