ansi-quantone 0.0.0-stage → 0.9.1

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +48 -2
  3. package/dst/ansi-quantone-api-color.d.ts +10 -0
  4. package/dst/ansi-quantone-api-color.js +44 -0
  5. package/dst/ansi-quantone-api-debug.d.ts +26 -0
  6. package/dst/ansi-quantone-api-debug.js +279 -0
  7. package/dst/ansi-quantone-api-mapping.d.ts +32 -0
  8. package/dst/ansi-quantone-api-mapping.js +55 -0
  9. package/dst/ansi-quantone-api-sgr.d.ts +30 -0
  10. package/dst/ansi-quantone-api-sgr.js +150 -0
  11. package/dst/ansi-quantone-api-stream.d.ts +7 -0
  12. package/dst/ansi-quantone-api-stream.js +194 -0
  13. package/dst/ansi-quantone-api-terminal.d.ts +14 -0
  14. package/dst/ansi-quantone-api-terminal.js +142 -0
  15. package/dst/ansi-quantone-api-types.d.ts +28 -0
  16. package/dst/ansi-quantone-api-types.js +6 -0
  17. package/dst/ansi-quantone-api.3 +87 -0
  18. package/dst/ansi-quantone-api.d.ts +11 -0
  19. package/dst/ansi-quantone-api.js +196 -0
  20. package/dst/ansi-quantone-cli.d.ts +7 -0
  21. package/dst/ansi-quantone-cli.js +256 -0
  22. package/dst/ansi-quantone.1 +123 -0
  23. package/etc/ansi-quantone.yaml +39 -0
  24. package/etc/eslint.mjs +81 -0
  25. package/etc/stx.conf +40 -0
  26. package/etc/tsc.json +25 -0
  27. package/package.json +57 -5
  28. package/src/ansi-quantone-api-color.ts +48 -0
  29. package/src/ansi-quantone-api-debug.ts +300 -0
  30. package/src/ansi-quantone-api-mapping.ts +77 -0
  31. package/src/ansi-quantone-api-sgr.ts +183 -0
  32. package/src/ansi-quantone-api-stream.ts +207 -0
  33. package/src/ansi-quantone-api-terminal.ts +156 -0
  34. package/src/ansi-quantone-api-types.ts +40 -0
  35. package/src/ansi-quantone-api.md +185 -0
  36. package/src/ansi-quantone-api.ts +223 -0
  37. package/src/ansi-quantone-cli.md +212 -0
  38. package/src/ansi-quantone-cli.ts +259 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+
2
+ ChangeLog
3
+ =========
4
+
5
+ 0.9.1 (2026-12-06)
6
+ ------------------
7
+
8
+ - BUGFIX [code, docs]: cache queried terminal colors per tmux client and use them in tmux popups,
9
+ which answer no color queries (falling back to too light xterm colors before)
10
+
11
+ 0.9.0 (2026-12-06)
12
+ ------------------
13
+
14
+ - FEATURE [code, docs, infr]: command `ansi-quantone` recolors a PTY-run command onto a Quantone palette
15
+ - FEATURE [code, docs, infr]: recolor terminal default colors (`-F`/`-B`), add palette `-l`/`-u` options
16
+ - FEATURE [code, infr]: debug mode (`-d`) with `CTRL+a d` overlay comparing source and target cell colors
17
+ - FEATURE [code, docs, infr]: profile-based YAML configuration with sections selected via `-p`
18
+ - FEATURE [code, docs, infr]: reduction (`-r`) of RGB/truecolor input colors onto nearest ANSI colors
19
+ - FEATURE [code, docs, infr]: color mapping rules (`-m`) `<fg>/<bg> : <fg>/<bg>` applied before recoloring
20
+ - FEATURE [code, docs, infr]: programmatic API `run()` with pluggable `TerminalIO`, CLI as thin wrapper
21
+ - REFACTOR [code]: split API module `ansi-quantone-api.ts` into cohesive `ansi-quantone-api-*.ts` modules
22
+
package/README.md CHANGED
@@ -1,3 +1,49 @@
1
- # Temporary Holding Version
2
1
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
2
+ ANSI Quantone
3
+ =============
4
+
5
+ **Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette**
6
+
7
+ [![github (author stars)](https://img.shields.io/github/stars/rse?logo=github&label=author%20stars&color=%233377aa)](https://github.com/rse)
8
+ [![github (author followers)](https://img.shields.io/github/followers/rse?label=author%20followers&logo=github&color=%234477aa)](https://github.com/rse)
9
+
10
+ Abstract
11
+ --------
12
+
13
+ `ansi-quantone`, *Recolor ANSI Color Sequences of CLI Commands onto a
14
+ Toned Palette*, is a small command-line tool to recolor the output of an
15
+ arbitrary command-line or Terminal User Interface (TUI) application. It
16
+ spawns the target *command* inside a Pseudo-Terminal (PTY), intercepts
17
+ the ANSI Select Graphic Rendition (SGR) color escape sequences of its
18
+ output, and maps every foreground and background color onto a toned
19
+ palette with the color quantizer [Quantone](https://github.com/rse/quantone).
20
+ Optionally, even the terminal default foreground and background colors
21
+ are mapped onto the palette. Before, RGB/truecolor input colors can be
22
+ reduced onto the 256 ANSI colors, and input colors can be remapped with
23
+ color mapping rules.
24
+
25
+ The palette options of **Quantone** can be given on the command-line or
26
+ in a configuration file in YAML format, grouped into profiles. If standard output is not a
27
+ terminal (e.g. a pipe), the *command* is run directly, without a PTY and
28
+ without any recoloring.
29
+
30
+ Installation
31
+ ------------
32
+
33
+ ```
34
+ $ npm install -g ansi-quantone
35
+ ```
36
+
37
+ Usage
38
+ -----
39
+
40
+ See the Unix manual pages [ansi-quantone(1)](src/ansi-quantone-cli.md) for the documentation of the
41
+ `ansi-quantone` command and [ansi-quantone-api(3)](src/ansi-quantone-api.md) for the documentation
42
+ of its underlying API.
43
+
44
+ License
45
+ -------
46
+
47
+ Copyright &copy; 2026 Dr. Ralf S. Engelschall (http://engelschall.com/)<br/>
48
+ Licensed under [MIT](https://spdx.org/licenses/MIT)
49
+
@@ -0,0 +1,10 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ export declare const classifyColor: (rgb: number) => {
7
+ family: string;
8
+ tone: string;
9
+ };
10
+ export declare const rgbToOKLab: (rgb: number) => [number, number, number];
@@ -0,0 +1,44 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ /* ==== color classification and conversion ==== */
7
+ /* classify an RGB color into its hue family and lightness tone (as in "ansi-recolor"):
8
+ colors with a chroma below 48 are "grey", the others "warm" (hue 280°-60°: magenta, red,
9
+ orange, yellow) or "cool" (hue 60°-280°: green, cyan, blue, violet), and their luma
10
+ (Rec. 709) yields the tone "dark" (below 64), "light" (above 180), or "mid" */
11
+ export const classifyColor = (rgb) => {
12
+ const r = (rgb >> 16) & 0xFF;
13
+ const g = (rgb >> 8) & 0xFF;
14
+ const b = rgb & 0xFF;
15
+ const max = Math.max(r, g, b);
16
+ const min = Math.min(r, g, b);
17
+ const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
18
+ const tone = luma < 64 ? "dark" : luma > 180 ? "light" : "mid";
19
+ if (max - min < 48)
20
+ return { family: "grey", tone };
21
+ let hue;
22
+ if (max === r)
23
+ hue = (60 * (g - b) / (max - min) + 360) % 360;
24
+ else if (max === g)
25
+ hue = 60 * (b - r) / (max - min) + 120;
26
+ else
27
+ hue = 60 * (r - g) / (max - min) + 240;
28
+ return { family: hue <= 60 || hue >= 280 ? "warm" : "cool", tone };
29
+ };
30
+ /* convert an RGB color into the perceptual OKLab color space */
31
+ export const rgbToOKLab = (rgb) => {
32
+ const [r, g, b] = [rgb >> 16, rgb >> 8, rgb].map((v) => {
33
+ const c = (v & 0xFF) / 255;
34
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
35
+ });
36
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
37
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
38
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
39
+ return [
40
+ 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
41
+ 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s,
42
+ 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
43
+ ];
44
+ };
@@ -0,0 +1,26 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ import type { Terminal } from "@xterm/headless";
7
+ import type { TerminalIO } from "./ansi-quantone-api-types.js";
8
+ export type Debug = {
9
+ src: Terminal;
10
+ dst: Terminal;
11
+ pending: number;
12
+ prefix: Buffer | null;
13
+ active: boolean;
14
+ x: number;
15
+ y: number;
16
+ covered: Set<number>;
17
+ full: boolean;
18
+ ansi: string[];
19
+ hints: Record<string, string>;
20
+ mouse: Map<number, boolean>;
21
+ mouseOn: boolean;
22
+ output: TerminalIO;
23
+ };
24
+ export declare const mouseModes: number[];
25
+ export declare const debugFeed: (dbg: Debug, source: Buffer, target: Buffer) => void;
26
+ export declare const debugInput: (dbg: Debug, data: Buffer, forward: (data: Buffer) => void) => void;
@@ -0,0 +1,279 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ /* the DECSET mouse tracking and encoding modes */
7
+ export const mouseModes = [9, 1000, 1002, 1003, 1005, 1006, 1015, 1016];
8
+ /* determine a color of a screen cell, as its description and its SGR parameters */
9
+ const cellColor = (cell, kind) => {
10
+ const base = kind === "fg" ? 30 : 40;
11
+ if (cell === undefined || (kind === "fg" ? cell.isFgDefault() : cell.isBgDefault()))
12
+ return { name: "default", sgr: [base + 9] };
13
+ const c = kind === "fg" ? cell.getFgColor() : cell.getBgColor();
14
+ if (kind === "fg" ? cell.isFgPalette() : cell.isBgPalette())
15
+ return { name: `ansi ${c}`, sgr: [base + 8, 5, c] };
16
+ return { name: "#" + c.toString(16).padStart(6, "0"), sgr: [base + 8, 2, (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF] };
17
+ };
18
+ /* determine the SGR sequence of the colors and attributes of a screen cell */
19
+ const cellSGR = (cell) => {
20
+ const params = [0];
21
+ if (cell.isBold())
22
+ params.push(1);
23
+ if (cell.isDim())
24
+ params.push(2);
25
+ if (cell.isItalic())
26
+ params.push(3);
27
+ if (cell.isUnderline())
28
+ params.push(4);
29
+ if (cell.isBlink())
30
+ params.push(5);
31
+ if (cell.isInverse())
32
+ params.push(7);
33
+ if (cell.isInvisible())
34
+ params.push(8);
35
+ if (cell.isStrikethrough())
36
+ params.push(9);
37
+ if (cell.isOverline())
38
+ params.push(53);
39
+ params.push(...cellColor(cell, "fg").sgr, ...cellColor(cell, "bg").sgr);
40
+ return `\x1b[${params.join(";")}m`;
41
+ };
42
+ /* render the debug overlay: restore the previously covered screen rows from the target
43
+ screen and draw the debug cursor and info box anew (with cursor, attributes, charset,
44
+ and origin mode saved/restored by DECSC/DECRC around it), once all output is parsed */
45
+ const debugRender = (dbg) => {
46
+ if (dbg.pending > 0)
47
+ return;
48
+ const { cols, rows, modes } = dbg.dst;
49
+ const bufSrc = dbg.src.buffer.active;
50
+ const bufDst = dbg.dst.buffer.active;
51
+ let out = "\x1b[?2026h\x1b7\x1b(B\x1b[?6l" + (modes.insertMode ? "\x1b[4l" : "");
52
+ /* restore the previously covered screen rows (or all of them,
53
+ as output in the meantime could have scrolled the debug overlay) */
54
+ const restore = dbg.full ? Array.from({ length: rows }, (_, y) => y) : [...dbg.covered];
55
+ for (const y of restore) {
56
+ const line = bufDst.getLine(bufDst.baseY + y);
57
+ if (line === undefined)
58
+ continue;
59
+ let last = "";
60
+ out += `\x1b[${y + 1};1H`;
61
+ for (let x = 0; x < cols; x++) {
62
+ const cell = line.getCell(x);
63
+ if (cell === undefined || cell.getWidth() === 0)
64
+ continue;
65
+ const sgr = cellSGR(cell);
66
+ if (sgr !== last)
67
+ out += (last = sgr);
68
+ out += cell.getChars() || " ";
69
+ }
70
+ }
71
+ dbg.covered.clear();
72
+ dbg.full = false;
73
+ if (dbg.active) {
74
+ /* determine the observed cell (never the second half of a wide character) */
75
+ dbg.x = Math.max(Math.min(dbg.x, cols - 1), 0);
76
+ dbg.y = Math.max(Math.min(dbg.y, rows - 1), 0);
77
+ if (dbg.x > 0 && bufDst.getLine(bufDst.baseY + dbg.y)?.getCell(dbg.x)?.getWidth() === 0)
78
+ dbg.x--;
79
+ const { x, y } = dbg;
80
+ const cellSrc = bufSrc.getLine(bufSrc.baseY + y)?.getCell(x);
81
+ const cellDst = bufDst.getLine(bufDst.baseY + y)?.getCell(x);
82
+ const w = Math.max(cellDst?.getWidth() ?? 1, 1);
83
+ /* draw the debug cursor as a 3x3 frame (with rounded corners) around the observed cell */
84
+ const border = "\x1b[0;38;2;204;51;51m";
85
+ const plain = "\x1b[0m";
86
+ const frame = ["╭" + "─".repeat(w) + "╮", "│" + " ".repeat(w) + "│", "╰" + "─".repeat(w) + "╯"];
87
+ out += border;
88
+ for (let r = 0; r < 3; r++) {
89
+ const fy = y - 1 + r;
90
+ if (fy < 0 || fy >= rows)
91
+ continue;
92
+ for (let c = 0; c < w + 2; c++) {
93
+ const fx = x - 1 + c;
94
+ if (fx < 0 || fx >= cols || (r === 1 && c > 0 && c <= w))
95
+ continue;
96
+ out += `\x1b[${fy + 1};${fx + 1}H` + frame[r][c];
97
+ }
98
+ dbg.covered.add(fy);
99
+ }
100
+ /* determine the info box lines (as content and its display width) */
101
+ const W = 36;
102
+ const chars = cellDst?.getChars() ?? "";
103
+ const pos = `cell: ${x + 1},${y + 1}`;
104
+ const lines = [];
105
+ if (chars === "")
106
+ lines.push([`${pos} (empty)`, pos.length + 8]);
107
+ else {
108
+ const cp = "U+" + (chars.codePointAt(0) ?? 0).toString(16).toUpperCase().padStart(4, "0");
109
+ lines.push([`${pos} ${cp} "${chars}"`, pos.length + cp.length + w + 4]);
110
+ }
111
+ for (const [label, cell, side] of [["source", cellSrc, "src"], ["target", cellDst, "dst"]]) {
112
+ for (const kind of ["fg", "bg"]) {
113
+ const color = cellColor(cell, kind);
114
+ const index = /^ansi (\d+)$/.exec(color.name);
115
+ const hint = color.name === "default" ? dbg.hints[side + kind] :
116
+ index !== null ? `= ${dbg.ansi[parseInt(index[1], 10)]}` : "";
117
+ const name = (hint !== "" ? `${color.name} ${hint}` : color.name).substring(0, W - 15);
118
+ const swatch = `\x1b[0;${color.sgr.join(";")}m${kind === "fg" ? "███" : " "}\x1b[0m`;
119
+ lines.push([`${label} ${kind}: ${swatch} ${name}`, 15 + name.length]);
120
+ }
121
+ }
122
+ /* draw the info box in the top-right corner
123
+ (or in the top-left corner, if the debug cursor is in the upper-right quadrant) */
124
+ const boxW = W + 4;
125
+ const boxH = lines.length + 2;
126
+ if (cols >= boxW && rows >= boxH) {
127
+ const bx = (y < rows / 2 && x >= cols / 2) ? 0 : cols - boxW;
128
+ const by = 0;
129
+ const title = "ANSI Quantone Debug";
130
+ const left = Math.floor((boxW - 2 - (title.length + 2)) / 2);
131
+ const right = boxW - 2 - (title.length + 2) - left;
132
+ out += `${border}\x1b[${by + 1};${bx + 1}H╭${"─".repeat(left)}\x1b[1;7m ${title} ${border}${"─".repeat(right)}╮`;
133
+ for (let i = 0; i < lines.length; i++)
134
+ out += `\x1b[${by + i + 2};${bx + 1}H${border}│${plain} ${lines[i][0]}${" ".repeat(W - lines[i][1])} ${border}│`;
135
+ out += `\x1b[${by + boxH};${bx + 1}H╰${"─".repeat(boxW - 2)}╯`;
136
+ for (let r = by; r < by + boxH; r++)
137
+ dbg.covered.add(r);
138
+ }
139
+ }
140
+ /* enable the mouse click reporting (in SGR encoding) while the debug overlay is displayed
141
+ (again on each rendering, as the command could have changed it), and afterwards
142
+ restore the mouse modes of the command (first all reset, then all set modes) */
143
+ if (dbg.active) {
144
+ out += "\x1b[?1000h\x1b[?1006h";
145
+ dbg.mouseOn = true;
146
+ }
147
+ else if (dbg.mouseOn) {
148
+ for (const state of [false, true])
149
+ for (const mode of mouseModes)
150
+ if ((dbg.mouse.get(mode) ?? false) === state)
151
+ out += `\x1b[?${mode}${state ? "h" : "l"}`;
152
+ dbg.mouseOn = false;
153
+ }
154
+ out += "\x1b[0m" + (modes.insertMode ? "\x1b[4h" : "") + "\x1b8\x1b[?2026l";
155
+ dbg.output.write(out);
156
+ };
157
+ /* feed the source and target output into their emulated screens
158
+ and re-render the debug overlay once both screens parsed all output */
159
+ export const debugFeed = (dbg, source, target) => {
160
+ if (dbg.covered.size > 0)
161
+ dbg.full = true;
162
+ for (const [screen, data] of [[dbg.src, source], [dbg.dst, target]]) {
163
+ if (data.length === 0)
164
+ continue;
165
+ dbg.pending++;
166
+ screen.write(data, () => {
167
+ if (--dbg.pending === 0 && (dbg.active || dbg.covered.size > 0))
168
+ debugRender(dbg);
169
+ });
170
+ }
171
+ };
172
+ /* decode the key at an input position, in its legacy encoding or in the xterm
173
+ "modifyOtherKeys" or kitty keyboard protocol encoding (which applications like
174
+ "lazygit" enable), into its name ("C-a", "a", "d", "A"-"D" for the cursor keys,
175
+ "mod" for a sole modifier key, "click" for a left mouse button press at screen
176
+ position x/y, "mouse" for any other mouse event, or "" for any other key),
177
+ its length, and whether it is a key release event */
178
+ const decodeKey = (data, i) => {
179
+ if (data[i] !== 0x1B)
180
+ return { key: data[i] === 0x01 ? "C-a" : data[i] === 0x61 ? "a" : data[i] === 0x64 ? "d" : "", len: 1, release: false };
181
+ const s = data.subarray(i, i + 32).toString("latin1");
182
+ const mouse = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])/.exec(s);
183
+ if (mouse !== null) {
184
+ /* SGR mouse event (left button press: button 0, with any modifiers, without motion) */
185
+ const click = (parseInt(mouse[1], 10) & ~(4 | 8 | 16)) === 0 && mouse[4] === "M";
186
+ const x = parseInt(mouse[2], 10) - 1;
187
+ const y = parseInt(mouse[3], 10) - 1;
188
+ return { key: click ? "click" : "mouse", len: mouse[0].length, release: false, x, y };
189
+ }
190
+ const cursor = /^\x1b(?:\[(?:1;\d+(?::(\d+))?)?|O)([A-D])/.exec(s);
191
+ if (cursor !== null)
192
+ return { key: cursor[2], len: cursor[0].length, release: cursor[1] === "3" };
193
+ const kitty = /^\x1b\[(\d+)(?::[\d:]*)?(?:;(\d+)(?::(\d+))?)?(?:;[\d:]*)?u/.exec(s);
194
+ const xterm = /^\x1b\[27;(\d+);(\d+)~/.exec(s);
195
+ const m = kitty ?? xterm;
196
+ if (m !== null) {
197
+ /* key code and modifiers (ignoring the Caps Lock and Num Lock states) */
198
+ const code = parseInt(kitty !== null ? m[1] : m[2], 10);
199
+ const mods = (parseInt((kitty !== null ? m[2] : m[1]) ?? "1", 10) - 1) & ~(64 | 128);
200
+ let key = "";
201
+ if (code >= 57441 && code <= 57452)
202
+ key = "mod";
203
+ else if (code === 97 && mods === 4)
204
+ key = "C-a";
205
+ else if (code === 97 && mods === 0)
206
+ key = "a";
207
+ else if (code === 100 && mods === 0)
208
+ key = "d";
209
+ return { key, len: m[0].length, release: kitty !== null && m[3] === "3" };
210
+ }
211
+ const other = /^\x1b(?:\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]|O.)/.exec(s);
212
+ return { key: "", len: other !== null ? other[0].length : 1, release: false };
213
+ };
214
+ /* handle the user input in debug mode: "CTRL+a d" toggles the debug overlay, "CTRL+a a"
215
+ and "CTRL+a CTRL+a" send a literal CTRL+a, and while the debug overlay is displayed,
216
+ the cursor keys and left mouse clicks move the debug cursor and all other input is discarded */
217
+ export const debugInput = (dbg, data, forward) => {
218
+ let i = 0;
219
+ while (i < data.length) {
220
+ if (dbg.prefix === null && !dbg.active && data[i] !== 0x01 && data[i] !== 0x1B) {
221
+ /* pass through all plain input up to the next potential prefix key */
222
+ let end = i;
223
+ while (end < data.length && data[end] !== 0x01 && data[end] !== 0x1B)
224
+ end++;
225
+ forward(data.subarray(i, end));
226
+ i = end;
227
+ continue;
228
+ }
229
+ const { key, len, release, x, y } = decodeKey(data, i);
230
+ const bytes = data.subarray(i, i + len);
231
+ i += len;
232
+ if (release || key === "mod" || key === "mouse") {
233
+ /* key release, sole modifier key, or other mouse event: ignore it during the debug mode handling */
234
+ if (dbg.prefix === null && !dbg.active)
235
+ forward(bytes);
236
+ }
237
+ else if (dbg.prefix !== null) {
238
+ /* key after the prefix key */
239
+ if (key === "d") {
240
+ dbg.active = !dbg.active;
241
+ if (dbg.active) {
242
+ dbg.x = Math.floor(dbg.dst.cols / 2);
243
+ dbg.y = Math.floor(dbg.dst.rows / 2);
244
+ }
245
+ debugRender(dbg);
246
+ }
247
+ else if ((key === "C-a" || key === "a") && !dbg.active)
248
+ forward(dbg.prefix);
249
+ dbg.prefix = null;
250
+ }
251
+ else if (key === "C-a") {
252
+ /* prefix key (kept in its original encoding) */
253
+ dbg.prefix = Buffer.from(bytes);
254
+ }
255
+ else if (dbg.active && key === "click" && x !== undefined && y !== undefined) {
256
+ /* left mouse click positions the debug cursor */
257
+ dbg.x = x;
258
+ dbg.y = y;
259
+ debugRender(dbg);
260
+ }
261
+ else if (dbg.active) {
262
+ /* cursor keys move the debug cursor */
263
+ const buffer = dbg.dst.buffer.active;
264
+ const width = buffer.getLine(buffer.baseY + dbg.y)?.getCell(dbg.x)?.getWidth() ?? 1;
265
+ if (key === "A")
266
+ dbg.y = Math.max(dbg.y - 1, 0);
267
+ else if (key === "B")
268
+ dbg.y = Math.min(dbg.y + 1, dbg.dst.rows - 1);
269
+ else if (key === "C")
270
+ dbg.x = Math.min(dbg.x + Math.max(width, 1), dbg.dst.cols - 1);
271
+ else if (key === "D")
272
+ dbg.x = Math.max(dbg.x - 1, 0);
273
+ if (["A", "B", "C", "D"].includes(key))
274
+ debugRender(dbg);
275
+ }
276
+ else
277
+ forward(bytes);
278
+ }
279
+ };
@@ -0,0 +1,32 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ export type InputColor = {
7
+ type: "default";
8
+ } | {
9
+ type: "index";
10
+ index: number;
11
+ } | {
12
+ type: "rgb";
13
+ rgb: number;
14
+ };
15
+ export type ColorSpec = InputColor | {
16
+ type: "wildcard";
17
+ } | {
18
+ type: "range";
19
+ from: number;
20
+ to: number;
21
+ } | {
22
+ type: "class";
23
+ family: string | null;
24
+ tone: string | null;
25
+ };
26
+ export type ColorMapping = {
27
+ fromFg: ColorSpec;
28
+ fromBg: ColorSpec;
29
+ toFg: ColorSpec;
30
+ toBg: ColorSpec;
31
+ };
32
+ export declare const parseMapping: (text: string) => ColorMapping;
@@ -0,0 +1,55 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ /* the lower-case ANSI color names onto their index (as in Quantone) */
7
+ const colorNames = new Map([["gray", 8], ["grey", 8]]);
8
+ const baseNames = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"];
9
+ for (let i = 0; i < baseNames.length; i++) {
10
+ colorNames.set(baseNames[i], i);
11
+ colorNames.set(`bright${baseNames[i]}`, i + 8);
12
+ colorNames.set(`${baseNames[i]}bright`, i + 8);
13
+ }
14
+ /* parse a color of a mapping rule */
15
+ const parseColorSpec = (spec, target) => {
16
+ if (spec === "*")
17
+ return { type: "wildcard" };
18
+ if (spec === "default")
19
+ return { type: "default" };
20
+ const hex = spec.match(/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/);
21
+ if (hex !== null) {
22
+ let n = parseInt(hex[1], 16);
23
+ if (hex[1].length === 3)
24
+ n = ((n & 0xF00) * 0x1100) | ((n & 0x0F0) * 0x110) | ((n & 0x00F) * 0x11);
25
+ return { type: "rgb", rgb: n };
26
+ }
27
+ if (/^\d{1,3}$/.test(spec) && parseInt(spec, 10) <= 255)
28
+ return { type: "index", index: parseInt(spec, 10) };
29
+ const index = colorNames.get(spec.toLowerCase());
30
+ if (index !== undefined)
31
+ return { type: "index", index };
32
+ const range = spec.match(/^(\d{1,3})\.\.(\d{1,3})$/);
33
+ if (!target && range !== null) {
34
+ const from = parseInt(range[1], 10);
35
+ const to = parseInt(range[2], 10);
36
+ if (from <= to && to <= 255)
37
+ return { type: "range", from, to };
38
+ }
39
+ const cls = spec.match(/^@(?:(warm|cool|grey|gray)(?:\.(dark|mid|light))?|(dark|mid|light))$/);
40
+ if (!target && cls !== null)
41
+ return { type: "class", family: cls[1] === "gray" ? "grey" : (cls[1] ?? null), tone: cls[2] ?? cls[3] ?? null };
42
+ throw new Error(`invalid ${target ? "target" : "source"} color "${spec}"`);
43
+ };
44
+ /* parse a color mapping rule "<fg>/<bg> : <fg>/<bg>" */
45
+ export const parseMapping = (text) => {
46
+ const m = text.match(/^\s*([^\s/:]+)\/([^\s/:]+)\s*:\s*([^\s/:]+)\/([^\s/:]+)\s*$/);
47
+ if (m === null)
48
+ throw new Error("expected \"<fg>/<bg> : <fg>/<bg>\"");
49
+ return {
50
+ fromFg: parseColorSpec(m[1], false),
51
+ fromBg: parseColorSpec(m[2], false),
52
+ toFg: parseColorSpec(m[3], true),
53
+ toBg: parseColorSpec(m[4], true)
54
+ };
55
+ };
@@ -0,0 +1,30 @@
1
+ /*!
2
+ ** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
3
+ ** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ ** Licensed under MIT license
5
+ */
6
+ import { type QuantoneOptions, type QuantoneColor } from "quantone";
7
+ import type { InputColor, ColorMapping } from "./ansi-quantone-api-mapping.js";
8
+ export type Recolor = {
9
+ options: QuantoneOptions;
10
+ fgSGR: number[] | null;
11
+ bgOSC: string | null;
12
+ defaults: Buffer;
13
+ ansi: string[];
14
+ lab: [number, number, number][] | null;
15
+ reduced: Map<number, number>;
16
+ mappings: ColorMapping[];
17
+ src: {
18
+ fg: InputColor;
19
+ bg: InputColor;
20
+ };
21
+ dst: {
22
+ fg: InputColor;
23
+ bg: InputColor;
24
+ };
25
+ buf: Buffer;
26
+ };
27
+ export declare const mapColor: (options: QuantoneOptions, color: QuantoneColor, kind: "fg" | "bg") => number[];
28
+ export declare const resetColors: (rc: Recolor) => void;
29
+ export declare const transformSGR: (params: number[], rc: Recolor) => number[];
30
+ export declare const isBgChange: (seq: Buffer) => boolean;