-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.ts
More file actions
51 lines (46 loc) · 1.95 KB
/
Copy pathcolors.ts
File metadata and controls
51 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @file Color application helpers for `logger/*` modules. Wraps the vendored
* `yoctocolors-cjs` palette so the logger can accept either a named color
* (`'green'`) or an explicit RGB tuple (`[255, 0, 0]`); RGB tuples are
* emitted via the 24-bit `[38;2;...m` escape because `yoctocolors-cjs`
* doesn't ship an `rgb()` helper. The vendored palette re-exports from the
* external-pack mega-bundle, whose module top-level evaluates every packed
* package — so it is required lazily on first color application (Logger
* construction at the earliest), keeping `logger/*` importers
* browser-load-safe and cheap at module init.
*/
import type yoctocolorsCjs from '../external/yoctocolors-cjs'
import type { ColorValue } from '../colors/types'
let cachedYoctocolors: typeof yoctocolorsCjs | undefined
/**
* Apply a color to text using yoctocolors. Handles both named colors and RGB
* tuples.
*/
export function applyColor(text: string, color: ColorValue): string {
if (typeof color === 'string') {
// Named color like 'green', 'red', etc. The yoctocolors palette indexes to
// a (text: string) => string formatter for each named color.
const formatter = (
getYoctocolors() as unknown as Record<
string,
((text: string) => string) | undefined
>
)[color]
return formatter ? formatter(text) : text
}
// RGB tuple [r, g, b] - manually construct ANSI escape codes.
// yoctocolors-cjs doesn't have an rgb() method, so we build it ourselves.
const { 0: r, 1: g, 2: b } = color
return `\u001B[38;2;${r};${g};${b}m${text}\u001B[39m`
}
/**
* Get the yoctocolors module for terminal colors. Required lazily on first
* call — see the @file note on the external-pack top-level.
*/
export function getYoctocolors(): typeof yoctocolorsCjs {
if (cachedYoctocolors === undefined) {
cachedYoctocolors =
require('../external/yoctocolors-cjs') as typeof yoctocolorsCjs
}
return cachedYoctocolors
}