|
1 |
| -// using chalk as some of our other dependencies are already using it... |
2 |
| -// exporting from here so we can easily refactor to a different color library if needed |
3 |
| -import * as ansi from "ansi-colors"; |
4 |
| -import * as chalk from "chalk"; |
| 1 | +import { styleText, stripVTControlCharacters } from "node:util"; |
5 | 2 |
|
6 |
| -export type Color = typeof chalk.Color; |
| 3 | +// Define color types based on util.inspect.colors |
| 4 | +export type StyleFormat = |
| 5 | + | "reset" |
| 6 | + | "bold" |
| 7 | + | "dim" |
| 8 | + | "italic" |
| 9 | + | "underline" |
| 10 | + | "blink" |
| 11 | + | "inverse" |
| 12 | + | "hidden" |
| 13 | + | "strikethrough" |
| 14 | + | "doubleunderline" |
| 15 | + | "black" |
| 16 | + | "red" |
| 17 | + | "green" |
| 18 | + | "yellow" |
| 19 | + | "blue" |
| 20 | + | "magenta" |
| 21 | + | "cyan" |
| 22 | + | "white" |
| 23 | + | "gray" |
| 24 | + | "redBright" |
| 25 | + | "greenBright" |
| 26 | + | "yellowBright" |
| 27 | + | "blueBright" |
| 28 | + | "magentaBright" |
| 29 | + | "cyanBright" |
| 30 | + | "whiteBright" |
| 31 | + | "bgBlack" |
| 32 | + | "bgRed" |
| 33 | + | "bgGreen" |
| 34 | + | "bgYellow" |
| 35 | + | "bgBlue" |
| 36 | + | "bgMagenta" |
| 37 | + | "bgCyan" |
| 38 | + | "bgWhite" |
| 39 | + | "bgGray" |
| 40 | + | "bgRedBright" |
| 41 | + | "bgGreenBright" |
| 42 | + | "bgYellowBright" |
| 43 | + | "bgBlueBright" |
| 44 | + | "bgMagentaBright" |
| 45 | + | "bgCyanBright" |
| 46 | + | "bgWhiteBright"; |
7 | 47 |
|
8 |
| -export function stripColors(formatStr: string) { |
9 |
| - return ansi.stripColor(formatStr); |
10 |
| -} |
| 48 | +export type Color = StyleFormat; |
11 | 49 |
|
12 |
| -export const color = chalk; |
| 50 | +// Create a chalk-like API using the Node.js util.styleText function |
| 51 | +export const color = { |
| 52 | + reset: (text: string) => styleText("reset", text), |
| 53 | + bold: (text: string) => styleText("bold", text), |
| 54 | + dim: (text: string) => styleText("dim", text), |
| 55 | + italic: (text: string) => styleText("italic", text), |
| 56 | + underline: (text: string) => styleText("underline", text), |
| 57 | + inverse: (text: string) => styleText("inverse", text), |
| 58 | + hidden: (text: string) => styleText("hidden", text), |
| 59 | + strikethrough: (text: string) => styleText("strikethrough", text), |
| 60 | + |
| 61 | + // Text colors |
| 62 | + black: (text: string) => styleText("black", text), |
| 63 | + red: (text: string) => styleText("red", text), |
| 64 | + blue: (text: string) => styleText("blue", text), |
| 65 | + magenta: (text: string) => styleText("magenta", text), |
| 66 | + cyan: (text: string) => styleText("cyan", text), |
| 67 | + white: (text: string) => styleText("white", text), |
| 68 | + gray: (text: string) => styleText("gray", text), |
| 69 | + yellow: (text: string) => styleText("yellow", text), |
| 70 | + green: (text: string) => styleText("green", text), |
| 71 | + grey: (text: string) => styleText("grey", text), |
| 72 | + |
| 73 | + // Background colors |
| 74 | + bgBlack: (text: string) => styleText("bgBlack", text), |
| 75 | + bgBlackBright: (text: string) => styleText("bgBlackBright", text), |
| 76 | + bgRed: (text: string) => styleText("bgRed", text), |
| 77 | + bgGreen: (text: string) => styleText("bgGreen", text), |
| 78 | + bgYellow: (text: string) => styleText("bgYellow", text), |
| 79 | + bgBlue: (text: string) => styleText("bgBlue", text), |
| 80 | + bgMagenta: (text: string) => styleText("bgMagenta", text), |
| 81 | + bgCyan: (text: string) => styleText("bgCyan", text), |
| 82 | + bgWhite: (text: string) => styleText("bgWhite", text), |
| 83 | + cyanBright: (text: string) => styleText("cyanBright", text), |
| 84 | + whiteBright: (text: string) => styleText("whiteBright", text), |
| 85 | + greenBright: (text: string) => styleText("greenBright", text), |
| 86 | + yellowBright: (text: string) => styleText("yellowBright", text), |
| 87 | + redBright: (text: string) => styleText("redBright", text), |
| 88 | + |
| 89 | + styleText, |
| 90 | +}; |
| 91 | + |
| 92 | +export const stripColors = (text: string) => stripVTControlCharacters(text); |
0 commit comments