console-toolkit 1.2.9 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -26
- package/package.json +26 -6
- package/src/alphanumeric/arrows.d.ts +48 -0
- package/src/alphanumeric/arrows.js +23 -0
- package/src/alphanumeric/fractions.d.ts +65 -0
- package/src/alphanumeric/fractions.js +49 -0
- package/src/alphanumeric/number-formatters.d.ts +91 -0
- package/src/alphanumeric/number-formatters.js +45 -1
- package/src/alphanumeric/roman.d.ts +15 -0
- package/src/alphanumeric/roman.js +12 -0
- package/src/alphanumeric/unicode-cultural-numbers.d.ts +65 -0
- package/src/alphanumeric/unicode-cultural-numbers.js +1 -0
- package/src/alphanumeric/unicode-letters.d.ts +32 -0
- package/src/alphanumeric/unicode-letters.js +8 -0
- package/src/alphanumeric/unicode-numbers.d.ts +44 -0
- package/src/alphanumeric/unicode-numbers.js +21 -0
- package/src/alphanumeric/utils.d.ts +45 -0
- package/src/alphanumeric/utils.js +26 -0
- package/src/ansi/csi.d.ts +141 -0
- package/src/ansi/csi.js +51 -2
- package/src/ansi/index.d.ts +26 -0
- package/src/ansi/sgr-constants.d.ts +173 -0
- package/src/ansi/sgr-state.d.ts +91 -0
- package/src/ansi/sgr-state.js +45 -0
- package/src/ansi/sgr.d.ts +587 -0
- package/src/ansi/sgr.js +426 -6
- package/src/box.d.ts +160 -0
- package/src/box.js +113 -12
- package/src/charts/bars/block-frac-grouped.d.ts +12 -0
- package/src/charts/bars/block-frac-grouped.js +6 -0
- package/src/charts/bars/block-frac.d.ts +34 -0
- package/src/charts/bars/block-frac.js +13 -0
- package/src/charts/bars/block-grouped.d.ts +12 -0
- package/src/charts/bars/block-grouped.js +6 -0
- package/src/charts/bars/block.d.ts +43 -0
- package/src/charts/bars/block.js +13 -0
- package/src/charts/bars/draw-grouped.d.ts +41 -0
- package/src/charts/bars/draw-grouped.js +4 -0
- package/src/charts/bars/draw-stacked.d.ts +47 -0
- package/src/charts/bars/draw-stacked.js +4 -0
- package/src/charts/bars/frac-grouped.d.ts +32 -0
- package/src/charts/bars/frac-grouped.js +13 -0
- package/src/charts/bars/plain-grouped.d.ts +12 -0
- package/src/charts/bars/plain-grouped.js +6 -0
- package/src/charts/bars/plain.d.ts +75 -0
- package/src/charts/bars/plain.js +27 -0
- package/src/charts/columns/block-frac-grouped.d.ts +12 -0
- package/src/charts/columns/block-frac-grouped.js +6 -0
- package/src/charts/columns/block-frac.d.ts +39 -0
- package/src/charts/columns/block-frac.js +13 -0
- package/src/charts/columns/block-grouped.d.ts +12 -0
- package/src/charts/columns/block-grouped.js +6 -0
- package/src/charts/columns/block.d.ts +43 -0
- package/src/charts/columns/block.js +13 -0
- package/src/charts/columns/draw-grouped.d.ts +41 -0
- package/src/charts/columns/draw-grouped.js +4 -0
- package/src/charts/columns/draw-stacked.d.ts +39 -0
- package/src/charts/columns/draw-stacked.js +4 -0
- package/src/charts/columns/frac-grouped.d.ts +37 -0
- package/src/charts/columns/frac-grouped.js +13 -0
- package/src/charts/columns/plain-grouped.d.ts +12 -0
- package/src/charts/columns/plain-grouped.js +6 -0
- package/src/charts/columns/plain.d.ts +32 -0
- package/src/charts/columns/plain.js +13 -0
- package/src/charts/themes/default.d.ts +6 -0
- package/src/charts/themes/default.js +1 -0
- package/src/charts/themes/rainbow-reversed.d.ts +6 -0
- package/src/charts/themes/rainbow-reversed.js +2 -1
- package/src/charts/themes/rainbow.d.ts +6 -0
- package/src/charts/themes/rainbow.js +1 -0
- package/src/charts/utils.d.ts +79 -0
- package/src/charts/utils.js +32 -4
- package/src/draw-block-frac.d.ts +16 -0
- package/src/draw-block-frac.js +14 -0
- package/src/draw-block.d.ts +53 -0
- package/src/draw-block.js +25 -1
- package/src/meta.d.ts +84 -0
- package/src/meta.js +64 -0
- package/src/output/show.d.ts +55 -0
- package/src/output/show.js +28 -0
- package/src/output/updater.d.ts +114 -0
- package/src/output/updater.js +58 -4
- package/src/output/writer.d.ts +87 -0
- package/src/output/writer.js +57 -5
- package/src/panel.d.ts +402 -0
- package/src/panel.js +219 -5
- package/src/plot/bitmap.d.ts +80 -0
- package/src/plot/bitmap.js +33 -4
- package/src/plot/draw-line.d.ts +13 -0
- package/src/plot/draw-line.js +8 -0
- package/src/plot/draw-rect.d.ts +13 -0
- package/src/plot/draw-rect.js +38 -30
- package/src/plot/index.d.ts +39 -0
- package/src/plot/index.js +22 -0
- package/src/plot/to-quads.d.ts +10 -0
- package/src/plot/to-quads.js +5 -0
- package/src/spinner/index.d.ts +4 -0
- package/src/spinner/index.js +0 -2
- package/src/spinner/spin.d.ts +13 -0
- package/src/spinner/spin.js +13 -2
- package/src/spinner/spinner.d.ts +69 -0
- package/src/spinner/spinner.js +30 -2
- package/src/spinner/spinners.d.ts +34 -0
- package/src/spinner/spinners.js +23 -9
- package/src/strings/clip.d.ts +21 -0
- package/src/strings/clip.js +10 -0
- package/src/strings/parse.d.ts +23 -0
- package/src/strings/parse.js +7 -0
- package/src/strings/split.d.ts +38 -0
- package/src/strings/split.js +15 -0
- package/src/strings.d.ts +44 -0
- package/src/strings.js +34 -4
- package/src/style.d.ts +462 -0
- package/src/style.js +58 -4
- package/src/symbols.d.ts +167 -0
- package/src/symbols.js +91 -7
- package/src/table/draw-borders.d.ts +38 -0
- package/src/table/draw-borders.js +10 -2
- package/src/table/index.d.ts +8 -0
- package/src/table/index.js +1 -0
- package/src/table/table.d.ts +234 -0
- package/src/table/table.js +59 -1
- package/src/themes/blocks/unicode-half.d.ts +6 -0
- package/src/themes/blocks/unicode-half.js +1 -0
- package/src/themes/blocks/unicode-thin.d.ts +6 -0
- package/src/themes/blocks/unicode-thin.js +1 -0
- package/src/themes/lines/ascii-compact.d.ts +6 -0
- package/src/themes/lines/ascii-compact.js +1 -0
- package/src/themes/lines/ascii-dots.d.ts +6 -0
- package/src/themes/lines/ascii-dots.js +1 -0
- package/src/themes/lines/ascii-girder.d.ts +6 -0
- package/src/themes/lines/ascii-girder.js +1 -0
- package/src/themes/lines/ascii-github.d.ts +6 -0
- package/src/themes/lines/ascii-github.js +1 -0
- package/src/themes/lines/ascii-reddit.d.ts +6 -0
- package/src/themes/lines/ascii-reddit.js +1 -0
- package/src/themes/lines/ascii-rounded.d.ts +6 -0
- package/src/themes/lines/ascii-rounded.js +1 -0
- package/src/themes/lines/ascii.d.ts +6 -0
- package/src/themes/lines/ascii.js +1 -0
- package/src/themes/lines/unicode-bold.d.ts +6 -0
- package/src/themes/lines/unicode-bold.js +1 -0
- package/src/themes/lines/unicode-rounded.d.ts +6 -0
- package/src/themes/lines/unicode-rounded.js +1 -0
- package/src/themes/lines/unicode.d.ts +6 -0
- package/src/themes/lines/unicode.js +1 -0
- package/src/themes/utils.d.ts +33 -0
- package/src/themes/utils.js +7 -0
- package/src/turtle/draw-line-art.d.ts +19 -0
- package/src/turtle/draw-line-art.js +7 -0
- package/src/turtle/draw-unicode.d.ts +19 -0
- package/src/turtle/draw-unicode.js +8 -0
- package/src/turtle/index.d.ts +21 -0
- package/src/turtle/index.js +8 -0
- package/src/turtle/turtle.d.ts +269 -0
- package/src/turtle/turtle.js +124 -4
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/** Pre-built SGR escape sequences for common text styling commands.
|
|
2
|
+
* Each constant is a complete escape sequence string ready to write to the terminal.
|
|
3
|
+
* @see {@link https://github.com/uhop/console-toolkit/wiki/Package:-ansi}
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** SGR sequence: enable bold. */
|
|
7
|
+
export const BOLD: string;
|
|
8
|
+
/** SGR sequence: enable dim/faint. */
|
|
9
|
+
export const DIM: string;
|
|
10
|
+
/** SGR sequence: enable italic. */
|
|
11
|
+
export const ITALIC: string;
|
|
12
|
+
/** SGR sequence: enable underline. */
|
|
13
|
+
export const UNDERLINE: string;
|
|
14
|
+
/** SGR sequence: enable blink. */
|
|
15
|
+
export const BLINK: string;
|
|
16
|
+
/** SGR sequence: enable rapid blink. */
|
|
17
|
+
export const RAPID_BLINK: string;
|
|
18
|
+
/** SGR sequence: enable inverse/reverse video. */
|
|
19
|
+
export const INVERSE: string;
|
|
20
|
+
/** SGR sequence: enable hidden/invisible text. */
|
|
21
|
+
export const HIDDEN: string;
|
|
22
|
+
/** SGR sequence: enable strikethrough. */
|
|
23
|
+
export const STRIKETHROUGH: string;
|
|
24
|
+
/** SGR sequence: enable overline. */
|
|
25
|
+
export const OVERLINE: string;
|
|
26
|
+
|
|
27
|
+
/** SGR sequence: reset all attributes. */
|
|
28
|
+
export const RESET_ALL: string;
|
|
29
|
+
/** SGR sequence: reset all attributes (alias of RESET_ALL). */
|
|
30
|
+
export const RESET: string;
|
|
31
|
+
|
|
32
|
+
/** SGR sequence: reset bold (also resets dim). */
|
|
33
|
+
export const RESET_BOLD: string;
|
|
34
|
+
/** SGR sequence: reset dim (also resets bold). */
|
|
35
|
+
export const RESET_DIM: string;
|
|
36
|
+
/** SGR sequence: reset italic. */
|
|
37
|
+
export const RESET_ITALIC: string;
|
|
38
|
+
/** SGR sequence: reset underline. */
|
|
39
|
+
export const RESET_UNDERLINE: string;
|
|
40
|
+
/** SGR sequence: reset blink. */
|
|
41
|
+
export const RESET_BLINK: string;
|
|
42
|
+
/** SGR sequence: reset rapid blink. */
|
|
43
|
+
export const RESET_RAPID_BLINK: string;
|
|
44
|
+
/** SGR sequence: reset inverse. */
|
|
45
|
+
export const RESET_INVERSE: string;
|
|
46
|
+
/** SGR sequence: reset hidden. */
|
|
47
|
+
export const RESET_HIDDEN: string;
|
|
48
|
+
/** SGR sequence: reset strikethrough. */
|
|
49
|
+
export const RESET_STRIKETHROUGH: string;
|
|
50
|
+
/** SGR sequence: reset overline. */
|
|
51
|
+
export const RESET_OVERLINE: string;
|
|
52
|
+
|
|
53
|
+
/** SGR sequence: reset foreground color to default. */
|
|
54
|
+
export const RESET_COLOR: string;
|
|
55
|
+
/** SGR sequence: reset background color to default. */
|
|
56
|
+
export const RESET_BG_COLOR: string;
|
|
57
|
+
/** SGR sequence: reset font to default. */
|
|
58
|
+
export const RESET_FONT: string;
|
|
59
|
+
|
|
60
|
+
/** SGR sequence: set foreground to black. */
|
|
61
|
+
export const BLACK: string;
|
|
62
|
+
/** SGR sequence: set foreground to red. */
|
|
63
|
+
export const RED: string;
|
|
64
|
+
/** SGR sequence: set foreground to green. */
|
|
65
|
+
export const GREEN: string;
|
|
66
|
+
/** SGR sequence: set foreground to yellow. */
|
|
67
|
+
export const YELLOW: string;
|
|
68
|
+
/** SGR sequence: set foreground to blue. */
|
|
69
|
+
export const BLUE: string;
|
|
70
|
+
/** SGR sequence: set foreground to magenta. */
|
|
71
|
+
export const MAGENTA: string;
|
|
72
|
+
/** SGR sequence: set foreground to cyan. */
|
|
73
|
+
export const CYAN: string;
|
|
74
|
+
/** SGR sequence: set foreground to white. */
|
|
75
|
+
export const WHITE: string;
|
|
76
|
+
|
|
77
|
+
/** SGR sequence: set foreground to bright black. */
|
|
78
|
+
export const BRIGHT_BLACK: string;
|
|
79
|
+
/** SGR sequence: set foreground to bright red. */
|
|
80
|
+
export const BRIGHT_RED: string;
|
|
81
|
+
/** SGR sequence: set foreground to bright green. */
|
|
82
|
+
export const BRIGHT_GREEN: string;
|
|
83
|
+
/** SGR sequence: set foreground to bright yellow. */
|
|
84
|
+
export const BRIGHT_YELLOW: string;
|
|
85
|
+
/** SGR sequence: set foreground to bright blue. */
|
|
86
|
+
export const BRIGHT_BLUE: string;
|
|
87
|
+
/** SGR sequence: set foreground to bright magenta. */
|
|
88
|
+
export const BRIGHT_MAGENTA: string;
|
|
89
|
+
/** SGR sequence: set foreground to bright cyan. */
|
|
90
|
+
export const BRIGHT_CYAN: string;
|
|
91
|
+
/** SGR sequence: set foreground to bright white. */
|
|
92
|
+
export const BRIGHT_WHITE: string;
|
|
93
|
+
|
|
94
|
+
/** SGR sequence: set background to black. */
|
|
95
|
+
export const BG_BLACK: string;
|
|
96
|
+
/** SGR sequence: set background to red. */
|
|
97
|
+
export const BG_RED: string;
|
|
98
|
+
/** SGR sequence: set background to green. */
|
|
99
|
+
export const BG_GREEN: string;
|
|
100
|
+
/** SGR sequence: set background to yellow. */
|
|
101
|
+
export const BG_YELLOW: string;
|
|
102
|
+
/** SGR sequence: set background to blue. */
|
|
103
|
+
export const BG_BLUE: string;
|
|
104
|
+
/** SGR sequence: set background to magenta. */
|
|
105
|
+
export const BG_MAGENTA: string;
|
|
106
|
+
/** SGR sequence: set background to cyan. */
|
|
107
|
+
export const BG_CYAN: string;
|
|
108
|
+
/** SGR sequence: set background to white. */
|
|
109
|
+
export const BG_WHITE: string;
|
|
110
|
+
|
|
111
|
+
/** SGR sequence: set background to bright black. */
|
|
112
|
+
export const BG_BRIGHT_BLACK: string;
|
|
113
|
+
/** SGR sequence: set background to bright red. */
|
|
114
|
+
export const BG_BRIGHT_RED: string;
|
|
115
|
+
/** SGR sequence: set background to bright green. */
|
|
116
|
+
export const BG_BRIGHT_GREEN: string;
|
|
117
|
+
/** SGR sequence: set background to bright yellow. */
|
|
118
|
+
export const BG_BRIGHT_YELLOW: string;
|
|
119
|
+
/** SGR sequence: set background to bright blue. */
|
|
120
|
+
export const BG_BRIGHT_BLUE: string;
|
|
121
|
+
/** SGR sequence: set background to bright magenta. */
|
|
122
|
+
export const BG_BRIGHT_MAGENTA: string;
|
|
123
|
+
/** SGR sequence: set background to bright cyan. */
|
|
124
|
+
export const BG_BRIGHT_CYAN: string;
|
|
125
|
+
/** SGR sequence: set background to bright white. */
|
|
126
|
+
export const BG_BRIGHT_WHITE: string;
|
|
127
|
+
|
|
128
|
+
/** SGR sequence: set foreground to black (explicit FG_ prefix). */
|
|
129
|
+
export const FG_BLACK: string;
|
|
130
|
+
/** SGR sequence: set foreground to red (explicit FG_ prefix). */
|
|
131
|
+
export const FG_RED: string;
|
|
132
|
+
/** SGR sequence: set foreground to green (explicit FG_ prefix). */
|
|
133
|
+
export const FG_GREEN: string;
|
|
134
|
+
/** SGR sequence: set foreground to yellow (explicit FG_ prefix). */
|
|
135
|
+
export const FG_YELLOW: string;
|
|
136
|
+
/** SGR sequence: set foreground to blue (explicit FG_ prefix). */
|
|
137
|
+
export const FG_BLUE: string;
|
|
138
|
+
/** SGR sequence: set foreground to magenta (explicit FG_ prefix). */
|
|
139
|
+
export const FG_MAGENTA: string;
|
|
140
|
+
/** SGR sequence: set foreground to cyan (explicit FG_ prefix). */
|
|
141
|
+
export const FG_CYAN: string;
|
|
142
|
+
/** SGR sequence: set foreground to white (explicit FG_ prefix). */
|
|
143
|
+
export const FG_WHITE: string;
|
|
144
|
+
|
|
145
|
+
/** SGR sequence: set foreground to bright black (explicit FG_ prefix). */
|
|
146
|
+
export const FG_BRIGHT_BLACK: string;
|
|
147
|
+
/** SGR sequence: set foreground to bright red (explicit FG_ prefix). */
|
|
148
|
+
export const FG_BRIGHT_RED: string;
|
|
149
|
+
/** SGR sequence: set foreground to bright green (explicit FG_ prefix). */
|
|
150
|
+
export const FG_BRIGHT_GREEN: string;
|
|
151
|
+
/** SGR sequence: set foreground to bright yellow (explicit FG_ prefix). */
|
|
152
|
+
export const FG_BRIGHT_YELLOW: string;
|
|
153
|
+
/** SGR sequence: set foreground to bright blue (explicit FG_ prefix). */
|
|
154
|
+
export const FG_BRIGHT_BLUE: string;
|
|
155
|
+
/** SGR sequence: set foreground to bright magenta (explicit FG_ prefix). */
|
|
156
|
+
export const FG_BRIGHT_MAGENTA: string;
|
|
157
|
+
/** SGR sequence: set foreground to bright cyan (explicit FG_ prefix). */
|
|
158
|
+
export const FG_BRIGHT_CYAN: string;
|
|
159
|
+
/** SGR sequence: set foreground to bright white (explicit FG_ prefix). */
|
|
160
|
+
export const FG_BRIGHT_WHITE: string;
|
|
161
|
+
|
|
162
|
+
/** SGR sequence: set foreground to gray (alias of BRIGHT_BLACK). */
|
|
163
|
+
export const GRAY: string;
|
|
164
|
+
/** SGR sequence: set foreground to grey (alias of BRIGHT_BLACK). */
|
|
165
|
+
export const GREY: string;
|
|
166
|
+
/** SGR sequence: set background to gray (alias of BG_BRIGHT_BLACK). */
|
|
167
|
+
export const BG_GRAY: string;
|
|
168
|
+
/** SGR sequence: set background to grey (alias of BG_BRIGHT_BLACK). */
|
|
169
|
+
export const BG_GREY: string;
|
|
170
|
+
/** SGR sequence: set foreground to gray (alias of FG_BRIGHT_BLACK). */
|
|
171
|
+
export const FG_GRAY: string;
|
|
172
|
+
/** SGR sequence: set foreground to grey (alias of FG_BRIGHT_BLACK). */
|
|
173
|
+
export const FG_GREY: string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** SGR (Select Graphics Rendition) state object. Properties are `null` for reset, a command string when set, or `undefined` when unspecified. */
|
|
2
|
+
export interface SgrState {
|
|
3
|
+
/** Bold attribute command or `null` to reset. */
|
|
4
|
+
bold?: string | null;
|
|
5
|
+
/** Dim/faint attribute command or `null` to reset. */
|
|
6
|
+
dim?: string | null;
|
|
7
|
+
/** Italic attribute command or `null` to reset. */
|
|
8
|
+
italic?: string | null;
|
|
9
|
+
/** Underline attribute command or `null` to reset. */
|
|
10
|
+
underline?: string | null;
|
|
11
|
+
/** Blink attribute command or `null` to reset. */
|
|
12
|
+
blink?: string | null;
|
|
13
|
+
/** Inverse/reverse attribute command or `null` to reset. */
|
|
14
|
+
inverse?: string | null;
|
|
15
|
+
/** Hidden attribute command or `null` to reset. */
|
|
16
|
+
hidden?: string | null;
|
|
17
|
+
/** Strikethrough attribute command or `null` to reset. */
|
|
18
|
+
strikethrough?: string | null;
|
|
19
|
+
/** Overline attribute command or `null` to reset. */
|
|
20
|
+
overline?: string | null;
|
|
21
|
+
/** Foreground color command(s) or `null` to reset. */
|
|
22
|
+
foreground?: string | string[] | null;
|
|
23
|
+
/** Background color command(s) or `null` to reset. */
|
|
24
|
+
background?: string | string[] | null;
|
|
25
|
+
/** Decoration (underline) color command(s) or `null` to reset. */
|
|
26
|
+
decoration?: string | string[] | null;
|
|
27
|
+
/** Font command or `null` to reset. */
|
|
28
|
+
font?: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The fully reset SGR state with all properties set to `null`. */
|
|
32
|
+
export const RESET_STATE: SgrState;
|
|
33
|
+
|
|
34
|
+
/** Extracts the cumulative SGR state from a string by parsing all SGR sequences.
|
|
35
|
+
* @param s - The string to parse for SGR sequences.
|
|
36
|
+
* @param initState - Initial state to build upon.
|
|
37
|
+
* @returns The resulting SGR state.
|
|
38
|
+
*/
|
|
39
|
+
export function extractState(s: string, initState?: SgrState): SgrState;
|
|
40
|
+
/** Converts a value to an SGR state object.
|
|
41
|
+
* @param value - An SgrState object, a string containing SGR sequences, an object with `getState()`, or `null` for reset.
|
|
42
|
+
* @returns The resulting SGR state.
|
|
43
|
+
*/
|
|
44
|
+
export function toState(value: SgrState | string | {getState(): SgrState} | null): SgrState;
|
|
45
|
+
|
|
46
|
+
/** Combines multiple SGR states, with later states overriding earlier ones.
|
|
47
|
+
* @param states - States to combine (SgrState objects, strings, or nulls).
|
|
48
|
+
* @returns The combined state.
|
|
49
|
+
*/
|
|
50
|
+
export function combineStates(...states: (SgrState | string | {getState(): SgrState} | null)[]): SgrState;
|
|
51
|
+
/** Converts an array of SGR command strings into an SGR state object.
|
|
52
|
+
* @param commands - Array of SGR command strings.
|
|
53
|
+
* @returns The resulting state.
|
|
54
|
+
*/
|
|
55
|
+
export function commandsToState(commands: string[]): SgrState;
|
|
56
|
+
/** Adds SGR commands to an existing state.
|
|
57
|
+
* @param state - The base state.
|
|
58
|
+
* @param commands - SGR command strings to add.
|
|
59
|
+
* @returns The updated state.
|
|
60
|
+
*/
|
|
61
|
+
export function addCommandsToState(state: SgrState, commands: string[]): SgrState;
|
|
62
|
+
|
|
63
|
+
/** Converts an SGR state to an array of SGR command strings.
|
|
64
|
+
* @param state - The SGR state to convert.
|
|
65
|
+
* @returns Array of SGR command strings.
|
|
66
|
+
*/
|
|
67
|
+
export function stateToCommands(state: SgrState): string[];
|
|
68
|
+
/** Computes the minimal SGR commands needed to transition from one state to another.
|
|
69
|
+
* @param prev - The previous SGR state.
|
|
70
|
+
* @param next - The target SGR state.
|
|
71
|
+
* @returns Array of SGR command strings for the transition.
|
|
72
|
+
*/
|
|
73
|
+
export function stateTransition(prev: SgrState, next: SgrState): string[];
|
|
74
|
+
/** Computes the minimal SGR commands to reverse a state transition.
|
|
75
|
+
* @param prev - The previous SGR state.
|
|
76
|
+
* @param next - The target SGR state.
|
|
77
|
+
* @returns Array of SGR command strings for the reverse transition.
|
|
78
|
+
*/
|
|
79
|
+
export function stateReverseTransition(prev: SgrState, next: SgrState): string[];
|
|
80
|
+
|
|
81
|
+
/** Converts an array of SGR commands to an escape sequence string.
|
|
82
|
+
* @param commands - Array of SGR command strings, or null/undefined.
|
|
83
|
+
* @returns The SGR escape sequence string.
|
|
84
|
+
*/
|
|
85
|
+
export function stringifyCommands(commands: string[] | undefined | null): string;
|
|
86
|
+
/** Optimizes SGR sequences in a string by computing minimal state transitions.
|
|
87
|
+
* @param s - The string containing SGR sequences.
|
|
88
|
+
* @param initState - Initial SGR state.
|
|
89
|
+
* @returns The optimized string.
|
|
90
|
+
*/
|
|
91
|
+
export function optimize(s: string, initState?: SgrState): string;
|
package/src/ansi/sgr-state.js
CHANGED
|
@@ -10,6 +10,9 @@ import {
|
|
|
10
10
|
matchSgr
|
|
11
11
|
} from './sgr.js';
|
|
12
12
|
|
|
13
|
+
/** The fully reset SGR state with all properties set to `null`.
|
|
14
|
+
* @type {object}
|
|
15
|
+
*/
|
|
13
16
|
export const RESET_STATE = {
|
|
14
17
|
bold: null,
|
|
15
18
|
dim: null,
|
|
@@ -30,6 +33,11 @@ const defaultState = Symbol('defaultState');
|
|
|
30
33
|
|
|
31
34
|
let toState;
|
|
32
35
|
|
|
36
|
+
/** Extracts the cumulative SGR state from a string by parsing all SGR sequences.
|
|
37
|
+
* @param {string} s - The string to parse.
|
|
38
|
+
* @param {object} [initState] - Initial state to build upon.
|
|
39
|
+
* @returns {object} The resulting SGR state.
|
|
40
|
+
*/
|
|
33
41
|
export const extractState = (s, initState = defaultState) => {
|
|
34
42
|
let state = toState(initState);
|
|
35
43
|
matchSgr.lastIndex = 0;
|
|
@@ -63,6 +71,10 @@ const getStateResets = state => {
|
|
|
63
71
|
return resetCount;
|
|
64
72
|
};
|
|
65
73
|
|
|
74
|
+
/** Combines multiple SGR states, with later states overriding earlier ones.
|
|
75
|
+
* @param {...(object|string)} states - States to combine.
|
|
76
|
+
* @returns {object} The combined state.
|
|
77
|
+
*/
|
|
66
78
|
export const combineStates = (...states) => {
|
|
67
79
|
let state = {};
|
|
68
80
|
for (const s of states) {
|
|
@@ -74,6 +86,10 @@ export const combineStates = (...states) => {
|
|
|
74
86
|
return state;
|
|
75
87
|
};
|
|
76
88
|
|
|
89
|
+
/** Converts an array of SGR command strings into an SGR state object.
|
|
90
|
+
* @param {string[]} commands - Array of SGR command strings.
|
|
91
|
+
* @returns {object} The resulting state.
|
|
92
|
+
*/
|
|
77
93
|
export const commandsToState = commands => {
|
|
78
94
|
let state = {};
|
|
79
95
|
for (let i = 0; i < commands.length; ++i) {
|
|
@@ -191,6 +207,11 @@ export const commandsToState = commands => {
|
|
|
191
207
|
return state;
|
|
192
208
|
};
|
|
193
209
|
|
|
210
|
+
/** Adds SGR commands to an existing state.
|
|
211
|
+
* @param {object} state - The current state.
|
|
212
|
+
* @param {string[]} commands - Commands to add.
|
|
213
|
+
* @returns {object} The updated state.
|
|
214
|
+
*/
|
|
194
215
|
export const addCommandsToState = (state, commands) => combineStates(state, commandsToState(commands));
|
|
195
216
|
|
|
196
217
|
const equalColors = (a, b) => {
|
|
@@ -217,6 +238,10 @@ const resetColorProperties = {
|
|
|
217
238
|
|
|
218
239
|
const chainedStates = {bold: 1, dim: 1};
|
|
219
240
|
|
|
241
|
+
/** Converts an SGR state to an array of SGR command strings.
|
|
242
|
+
* @param {object|string} state - The state to convert.
|
|
243
|
+
* @returns {string[]} The command strings.
|
|
244
|
+
*/
|
|
220
245
|
export const stateToCommands = state => {
|
|
221
246
|
state = toState(state);
|
|
222
247
|
const commands = [];
|
|
@@ -257,6 +282,11 @@ export const stateToCommands = state => {
|
|
|
257
282
|
return resetCount === TOTAL_RESETS ? [''] : commands;
|
|
258
283
|
};
|
|
259
284
|
|
|
285
|
+
/** Computes the minimal SGR commands needed to transition from one state to another.
|
|
286
|
+
* @param {object|string} prev - The previous state.
|
|
287
|
+
* @param {object|string} next - The next state.
|
|
288
|
+
* @returns {string[]} The transition commands.
|
|
289
|
+
*/
|
|
260
290
|
export const stateTransition = (prev, next) => {
|
|
261
291
|
prev = toState(prev);
|
|
262
292
|
next = toState(next);
|
|
@@ -318,6 +348,11 @@ export const stateTransition = (prev, next) => {
|
|
|
318
348
|
return commands;
|
|
319
349
|
};
|
|
320
350
|
|
|
351
|
+
/** Computes the minimal SGR commands to reverse a state transition (from next back to prev).
|
|
352
|
+
* @param {object|string} prev - The state to restore.
|
|
353
|
+
* @param {object|string} next - The current state.
|
|
354
|
+
* @returns {string[]} The reverse transition commands.
|
|
355
|
+
*/
|
|
321
356
|
export const stateReverseTransition = (prev, next) => {
|
|
322
357
|
prev = toState(prev);
|
|
323
358
|
next = toState(next);
|
|
@@ -347,6 +382,7 @@ export const stateReverseTransition = (prev, next) => {
|
|
|
347
382
|
if (prev.dim === null) ++resetCount;
|
|
348
383
|
|
|
349
384
|
for (const name of Object.keys(RESET_STATE)) {
|
|
385
|
+
if (chainedStates[name] === 1) continue; // skip chained states
|
|
350
386
|
const value = prev[name];
|
|
351
387
|
if (resetColorProperties.hasOwnProperty(name)) {
|
|
352
388
|
// color
|
|
@@ -374,8 +410,17 @@ export const stateReverseTransition = (prev, next) => {
|
|
|
374
410
|
return commands;
|
|
375
411
|
};
|
|
376
412
|
|
|
413
|
+
/** Converts an array of SGR commands to an escape sequence string, or empty string if no commands.
|
|
414
|
+
* @param {string[]} commands
|
|
415
|
+
* @returns {string}
|
|
416
|
+
*/
|
|
377
417
|
export const stringifyCommands = commands => (commands?.length ? setCommands(commands) : '');
|
|
378
418
|
|
|
419
|
+
/** Optimizes SGR sequences in a string by computing minimal state transitions.
|
|
420
|
+
* @param {string} s - The string to optimize.
|
|
421
|
+
* @param {object} [initState] - Initial state.
|
|
422
|
+
* @returns {string} The optimized string.
|
|
423
|
+
*/
|
|
379
424
|
export const optimize = (s, initState = defaultState) => {
|
|
380
425
|
let state = toState(initState),
|
|
381
426
|
result = '',
|