Created
February 22, 2026 18:03
-
-
Save GauBen/744c080ec3a5911946cdfbb7115519ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { type InspectColor, styleText } from "node:util"; | |
| type StyleText = (( | |
| text: string | TemplateStringsArray, | |
| ...args: unknown[] | |
| ) => string) & { [key in InspectColor]: StyleText }; | |
| function createStyleText(...styles: InspectColor[]): StyleText { | |
| return new Proxy( | |
| (text: string | TemplateStringsArray, ...args: unknown[]) => | |
| styleText( | |
| styles, | |
| typeof text === "string" ? text : String.raw({ raw: text }, ...args), | |
| ), | |
| { | |
| get(_, prop) { | |
| if (typeof prop !== "string") | |
| throw new TypeError("Property must be a string"); | |
| return createStyleText(...styles, prop as InspectColor); | |
| }, | |
| }, | |
| ) as StyleText; | |
| } | |
| const k = createStyleText(); | |
| console.log(k.red.underline`This is ${k.yellow`yellow`} text`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment