|
1 |
| -import type p from 'picocolors' |
2 |
| -import type { Formatter } from 'picocolors/types' |
3 | 1 | import { SAFE_COLORS_SYMBOL } from './constants'
|
4 | 2 |
|
| 3 | +type Colors = ReturnType<typeof createColors> |
| 4 | + |
5 | 5 | const colors = [
|
6 | 6 | 'reset',
|
7 | 7 | 'bold',
|
@@ -30,17 +30,84 @@ const colors = [
|
30 | 30 | 'bgWhite',
|
31 | 31 | ] as const
|
32 | 32 |
|
33 |
| -const formatter: Formatter = str => String(str) |
| 33 | +const string = (str: unknown) => String(str) |
| 34 | +string.open = '' |
| 35 | +string.close = '' |
34 | 36 |
|
35 | 37 | const defaultColors = colors.reduce((acc, key) => {
|
36 |
| - acc[key] = formatter |
| 38 | + acc[key] = string |
37 | 39 | return acc
|
38 |
| -}, { isColorSupported: false } as typeof p) |
| 40 | +}, {} as Colors) |
| 41 | + |
| 42 | +export function getDefaultColors(): Colors { |
| 43 | + return { ...defaultColors } |
| 44 | +} |
39 | 45 |
|
40 |
| -export function getColors(): typeof p { |
| 46 | +export function getColors(): Colors { |
41 | 47 | return (globalThis as any)[SAFE_COLORS_SYMBOL] || defaultColors
|
42 | 48 | }
|
43 | 49 |
|
44 |
| -export function setColors(colors: typeof p) { |
| 50 | +export function createColors(isTTY = false) { |
| 51 | + const enabled = typeof process !== 'undefined' |
| 52 | + && !('NO_COLOR' in process.env || process.argv.includes('--no-color')) |
| 53 | + && !('GITHUB_ACTIONS' in process.env) |
| 54 | + && ('FORCE_COLOR' in process.env |
| 55 | + || process.argv.includes('--color') |
| 56 | + || process.platform === 'win32' |
| 57 | + || (isTTY && process.env.TERM !== 'dumb') |
| 58 | + || 'CI' in process.env) |
| 59 | + |
| 60 | + const replaceClose = (string: string, close: string, replace: string, index: number): string => { |
| 61 | + const start = string.substring(0, index) + replace |
| 62 | + const end = string.substring(index + close.length) |
| 63 | + const nextIndex = end.indexOf(close) |
| 64 | + return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end |
| 65 | + } |
| 66 | + |
| 67 | + const formatter = (open: string, close: string, replace = open) => { |
| 68 | + const fn = (input: unknown) => { |
| 69 | + const string = `${input}` |
| 70 | + const index = string.indexOf(close, open.length) |
| 71 | + return ~index |
| 72 | + ? open + replaceClose(string, close, replace, index) + close |
| 73 | + : open + string + close |
| 74 | + } |
| 75 | + fn.open = open |
| 76 | + fn.close = close |
| 77 | + return fn |
| 78 | + } |
| 79 | + |
| 80 | + // based on "https://github.com/alexeyraspopov/picocolors", but browser-friendly |
| 81 | + return { |
| 82 | + isColorSupported: enabled, |
| 83 | + reset: enabled ? (s: string) => `\x1B[0m${s}\x1B[0m` : string, |
| 84 | + bold: enabled ? formatter('\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m') : string, |
| 85 | + dim: enabled ? formatter('\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m') : string, |
| 86 | + italic: enabled ? formatter('\x1B[3m', '\x1B[23m') : string, |
| 87 | + underline: enabled ? formatter('\x1B[4m', '\x1B[24m') : string, |
| 88 | + inverse: enabled ? formatter('\x1B[7m', '\x1B[27m') : string, |
| 89 | + hidden: enabled ? formatter('\x1B[8m', '\x1B[28m') : string, |
| 90 | + strikethrough: enabled ? formatter('\x1B[9m', '\x1B[29m') : string, |
| 91 | + black: enabled ? formatter('\x1B[30m', '\x1B[39m') : string, |
| 92 | + red: enabled ? formatter('\x1B[31m', '\x1B[39m') : string, |
| 93 | + green: enabled ? formatter('\x1B[32m', '\x1B[39m') : string, |
| 94 | + yellow: enabled ? formatter('\x1B[33m', '\x1B[39m') : string, |
| 95 | + blue: enabled ? formatter('\x1B[34m', '\x1B[39m') : string, |
| 96 | + magenta: enabled ? formatter('\x1B[35m', '\x1B[39m') : string, |
| 97 | + cyan: enabled ? formatter('\x1B[36m', '\x1B[39m') : string, |
| 98 | + white: enabled ? formatter('\x1B[37m', '\x1B[39m') : string, |
| 99 | + gray: enabled ? formatter('\x1B[90m', '\x1B[39m') : string, |
| 100 | + bgBlack: enabled ? formatter('\x1B[40m', '\x1B[49m') : string, |
| 101 | + bgRed: enabled ? formatter('\x1B[41m', '\x1B[49m') : string, |
| 102 | + bgGreen: enabled ? formatter('\x1B[42m', '\x1B[49m') : string, |
| 103 | + bgYellow: enabled ? formatter('\x1B[43m', '\x1B[49m') : string, |
| 104 | + bgBlue: enabled ? formatter('\x1B[44m', '\x1B[49m') : string, |
| 105 | + bgMagenta: enabled ? formatter('\x1B[45m', '\x1B[49m') : string, |
| 106 | + bgCyan: enabled ? formatter('\x1B[46m', '\x1B[49m') : string, |
| 107 | + bgWhite: enabled ? formatter('\x1B[47m', '\x1B[49m') : string, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export function setupColors(colors: Colors) { |
45 | 112 | (globalThis as any)[SAFE_COLORS_SYMBOL] = colors
|
46 | 113 | }
|
0 commit comments