-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.ts
More file actions
40 lines (36 loc) · 1.17 KB
/
colors.ts
File metadata and controls
40 lines (36 loc) · 1.17 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
/**
* @fileoverview 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.
*/
import yoctocolorsCjs from '../external/yoctocolors-cjs'
import type { ColorValue } from '../colors/types'
/**
* Apply a color to text using yoctocolors.
* Handles both named colors and RGB tuples.
*/
/*@__NO_SIDE_EFFECTS__*/
export function applyColor(
text: string,
color: ColorValue,
colors: typeof yoctocolorsCjs,
): string {
if (typeof color === 'string') {
// Named color like 'green', 'red', etc.
return (colors as any)[color](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.
*/
/*@__NO_SIDE_EFFECTS__*/
export function getYoctocolors() {
return yoctocolorsCjs
}