| */ |
| 6 | const styles = { |
| 7 | // Reset |
| 8 | reset: "\x1b[0m", |
| 9 |
| 10 | // Text styles |
| 11 | bold: "\x1b[1m", |
| 12 | dim: "\x1b[2m", |
| 13 | italic: "\x1b[3m", |
| 14 | underline: "\x1b[4m", |
| 15 | strikethrough: "\x1b[9m", |
| 16 |
| 17 | // Text colors |
| 18 | black: "\x1b[30m", |
| 19 | red: "\x1b[31m", |
| 20 | green: "\x1b[32m", |
| 21 | yellow: "\x1b[33m", |
| 22 | blue: "\x1b[34m", |
| 23 | magenta: "\x1b[35m", |
| 24 | cyan: "\x1b[36m", |
| 25 | white: "\x1b[37m", |
| 26 | gray: "\x1b[90m", |
| 27 | grey: "\x1b[90m", |
| 28 |
| 29 | // Bright text colors |
| 30 | brightRed: "\x1b[91m", |
| 31 | brightGreen: "\x1b[92m", |
| 32 | brightYellow: "\x1b[93m", |
| 33 | brightBlue: "\x1b[94m", |
| 34 | brightMagenta: "\x1b[95m", |
| 35 | brightCyan: "\x1b[96m", |
| 36 | brightWhite: "\x1b[97m", |
| 37 |
| 38 | // Background colors |
| 39 | bgBlack: "\x1b[40m", |
| 40 | bgRed: "\x1b[41m", |
| 41 | bgGreen: "\x1b[42m", |
| 42 | bgYellow: "\x1b[43m", |
| 43 | bgBlue: "\x1b[44m", |
| 44 | bgMagenta: "\x1b[45m", |
| 45 | bgCyan: "\x1b[46m", |
| 46 | bgWhite: "\x1b[47m", |
| 47 | }; |
| 48 |
| 49 | type Format = keyof typeof styles; |
| 50 |
| 51 | export const styleText = utils.styleText || _styleText; |
| 52 |
| 53 | /** |
| 54 | * Basic implementation of util.styleText for formatting text with ANSI colors |
| 55 | * @param format - A text format or an Array of text formats |
| 56 | * @param - The text to be formatted |
| 57 | * @returns The formatted text with ANSI escape codes |
| 58 | */ |
| 59 | function _styleText(format: Format | Format[], text: string) { |
| 60 | const formats = Array.isArray(format) ? format : [format]; |
| 61 | // Build the opening escape sequences |
| 62 | let openCodes = ""; |
| 63 | for (const fmt of formats) if (fmt in styles) openCodes += styles[fmt]; |
| 64 | // Return formatted text with reset at the end |
| 65 | return openCodes + text + styles.reset; |
| 66 | } |
| 67 |