|
| 1 | +/** |
| 2 | + * ANSI escape codes for text formatting |
| 3 | + */ |
| 4 | +export enum Effect { |
| 5 | + Reset = '\x1b[0m', |
| 6 | + Bold = '\x1b[1m', |
| 7 | + Underline = '\x1b[4m', |
| 8 | + CrossedOut = '\x1b[9m', |
| 9 | + BoldOff = '\x1b[22m', |
| 10 | + UnderlineOff = '\x1b[24m', |
| 11 | + CrossedOutOff = '\x1b[29m', |
| 12 | + ForegroundRed = '\x1b[31m', |
| 13 | + ForegroundGreen = '\x1b[32m', |
| 14 | + ForegroundYellow = '\x1b[33m', |
| 15 | + ForegroundBlue = '\x1b[34m', |
| 16 | + ForegroundMagenta = '\x1b[35m', |
| 17 | + ForegroundCyan = '\x1b[36m', |
| 18 | + ForegroundWhite = '\x1b[37m', |
| 19 | + ForegroundGray = '\x1b[90m', |
| 20 | + BackgroundRed = '\x1b[41m', |
| 21 | + BackgroundGreen = '\x1b[42m', |
| 22 | + BackgroundYellow = '\x1b[43m', |
| 23 | + BackgroundBlue = '\x1b[44m', |
| 24 | + BackgroundMagenta = '\x1b[45m', |
| 25 | + BackgroundCyan = '\x1b[46m', |
| 26 | + BackgroundWhite = '\x1b[47m', |
| 27 | + BackgroundGray = '\x1b[100m', |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * ANSI escape code for setting visual effects using SGR (Select Graphic Rendition) subset |
| 32 | + * |
| 33 | + * @example console.log('Hello, ${sgr('world', Effect.ForegroundRed)}'); |
| 34 | + * |
| 35 | + * |
| 36 | + * @param message - The message to colorize |
| 37 | + * @param color - The color to apply |
| 38 | + * @returns The colored message |
| 39 | + */ |
| 40 | +export function sgr(message: string, color: Effect | Effect[]): string { |
| 41 | + const colorCode = Array.isArray(color) ? color.join('') : color ?? Effect.Reset; |
| 42 | + |
| 43 | + return `${colorCode}${message}${Effect.Reset}`; |
| 44 | +} |
0 commit comments