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.
- package/CHANGELOG.md +22 -0
- package/README.md +48 -2
- package/dst/ansi-quantone-api-color.d.ts +10 -0
- package/dst/ansi-quantone-api-color.js +44 -0
- package/dst/ansi-quantone-api-debug.d.ts +26 -0
- package/dst/ansi-quantone-api-debug.js +279 -0
- package/dst/ansi-quantone-api-mapping.d.ts +32 -0
- package/dst/ansi-quantone-api-mapping.js +55 -0
- package/dst/ansi-quantone-api-sgr.d.ts +30 -0
- package/dst/ansi-quantone-api-sgr.js +150 -0
- package/dst/ansi-quantone-api-stream.d.ts +7 -0
- package/dst/ansi-quantone-api-stream.js +194 -0
- package/dst/ansi-quantone-api-terminal.d.ts +14 -0
- package/dst/ansi-quantone-api-terminal.js +142 -0
- package/dst/ansi-quantone-api-types.d.ts +28 -0
- package/dst/ansi-quantone-api-types.js +6 -0
- package/dst/ansi-quantone-api.3 +87 -0
- package/dst/ansi-quantone-api.d.ts +11 -0
- package/dst/ansi-quantone-api.js +196 -0
- package/dst/ansi-quantone-cli.d.ts +7 -0
- package/dst/ansi-quantone-cli.js +256 -0
- package/dst/ansi-quantone.1 +123 -0
- package/etc/ansi-quantone.yaml +39 -0
- package/etc/eslint.mjs +81 -0
- package/etc/stx.conf +40 -0
- package/etc/tsc.json +25 -0
- package/package.json +57 -5
- package/src/ansi-quantone-api-color.ts +48 -0
- package/src/ansi-quantone-api-debug.ts +300 -0
- package/src/ansi-quantone-api-mapping.ts +77 -0
- package/src/ansi-quantone-api-sgr.ts +183 -0
- package/src/ansi-quantone-api-stream.ts +207 -0
- package/src/ansi-quantone-api-terminal.ts +156 -0
- package/src/ansi-quantone-api-types.ts +40 -0
- package/src/ansi-quantone-api.md +185 -0
- package/src/ansi-quantone-api.ts +223 -0
- package/src/ansi-quantone-cli.md +212 -0
- package/src/ansi-quantone-cli.ts +259 -0
|
@@ -0,0 +1,196 @@
|
|
|
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 process from "node:process";
|
|
7
|
+
import * as pty from "node-pty";
|
|
8
|
+
import { quantone } from "quantone";
|
|
9
|
+
import { xtermColors, queryColors, loadColors, saveColors, stdioTerminal } from "./ansi-quantone-api-terminal.js";
|
|
10
|
+
import { parseMapping } from "./ansi-quantone-api-mapping.js";
|
|
11
|
+
import { rgbToOKLab } from "./ansi-quantone-api-color.js";
|
|
12
|
+
import { mapColor } from "./ansi-quantone-api-sgr.js";
|
|
13
|
+
import { processChunk } from "./ansi-quantone-api-stream.js";
|
|
14
|
+
import { mouseModes, debugFeed, debugInput } from "./ansi-quantone-api-debug.js";
|
|
15
|
+
export { stdioTerminal };
|
|
16
|
+
/* ==== API entry point ==== */
|
|
17
|
+
/* run a command in a PTY on a terminal and recolor its output according to a configuration,
|
|
18
|
+
and resolve to the exit code of the command once it exited */
|
|
19
|
+
export const run = async (config, command, args = [], terminal = stdioTerminal()) => {
|
|
20
|
+
const options = config.palette;
|
|
21
|
+
/* validate the palette options and explicit default colors once upfront */
|
|
22
|
+
try {
|
|
23
|
+
quantone(options, 0);
|
|
24
|
+
for (const color of [config.foreground, config.background])
|
|
25
|
+
if (color !== "auto" && color !== "none")
|
|
26
|
+
quantone(options, color);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
throw new Error(`invalid palette options: ${err instanceof Error ? err.message : String(err)}`);
|
|
30
|
+
}
|
|
31
|
+
/* parse the color mappings */
|
|
32
|
+
const mappings = [];
|
|
33
|
+
for (const text of config.mappings) {
|
|
34
|
+
try {
|
|
35
|
+
mappings.push(parseMapping(text));
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
throw new Error(`invalid color mapping "${text}": ${err instanceof Error ? err.message : String(err)}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/* switch the terminal into raw mode */
|
|
42
|
+
terminal.setup();
|
|
43
|
+
/* query the terminal for its ANSI colors and the default colors which are determined
|
|
44
|
+
automatically (the default background always, to explicitly restore it on exit) */
|
|
45
|
+
const codes = [
|
|
46
|
+
...Array.from({ length: 256 }, (_, i) => `4;${i}`),
|
|
47
|
+
...(config.foreground === "auto" ? ["10"] : []),
|
|
48
|
+
...(config.background !== "none" ? ["11"] : [])
|
|
49
|
+
];
|
|
50
|
+
const answer = terminal.query ?
|
|
51
|
+
await queryColors(terminal, codes) : { colors: new Map(), rest: Buffer.alloc(0) };
|
|
52
|
+
/* cache the answered colors, or fall back to the cached colors if none were answered
|
|
53
|
+
(e.g. in a tmux popup, which answers no color queries at all) */
|
|
54
|
+
if (terminal.cache !== undefined) {
|
|
55
|
+
if (answer.colors.size > 0)
|
|
56
|
+
saveColors(terminal.cache, answer.colors);
|
|
57
|
+
else
|
|
58
|
+
answer.colors = loadColors(terminal.cache);
|
|
59
|
+
}
|
|
60
|
+
/* determine the recoloring context (with the canonical xterm values as the fallbacks
|
|
61
|
+
of the ANSI colors, and the ANSI colors 7/0 as the fallbacks of the default colors) */
|
|
62
|
+
const ansi = xtermColors.map((hex, i) => answer.colors.get(`4;${i}`) ?? hex);
|
|
63
|
+
const resolveColor = (color, code, fallback) => color === "none" ? null : color === "auto" ? (answer.colors.get(code) ?? ansi[fallback]) : color;
|
|
64
|
+
const fg = resolveColor(config.foreground, "10", 7);
|
|
65
|
+
const bg = resolveColor(config.background, "11", 0);
|
|
66
|
+
const fgSGR = fg !== null ? mapColor(options, fg, "fg") : null;
|
|
67
|
+
const bgOSC = bg !== null ?
|
|
68
|
+
`\x1b]11;${quantone(options, bg).replace(/^#(..)(..)(..)$/, "rgb:$1/$2/$3")}\x1b\\` : null;
|
|
69
|
+
const rc = {
|
|
70
|
+
options,
|
|
71
|
+
fgSGR,
|
|
72
|
+
bgOSC,
|
|
73
|
+
defaults: Buffer.from((bgOSC ?? "") + (fgSGR !== null ? `\x1b[${fgSGR.join(";")}m` : "")),
|
|
74
|
+
ansi,
|
|
75
|
+
lab: config.reduce ? ansi.map((hex) => rgbToOKLab(parseInt(hex.substring(1), 16))) : null,
|
|
76
|
+
reduced: new Map(),
|
|
77
|
+
mappings,
|
|
78
|
+
src: { fg: { type: "default" }, bg: { type: "default" } },
|
|
79
|
+
dst: { fg: { type: "default" }, bg: { type: "default" } },
|
|
80
|
+
buf: Buffer.alloc(0)
|
|
81
|
+
};
|
|
82
|
+
/* establish the mapped default colors and reset them again on exit
|
|
83
|
+
(the background is explicitly restored, as not all terminals support OSC 111) */
|
|
84
|
+
const bgOrig = answer.colors.get("11");
|
|
85
|
+
const bgReset = (bgOrig !== undefined ?
|
|
86
|
+
`\x1b]11;${bgOrig.replace(/^#(..)(..)(..)$/, "rgb:$1/$2/$3")}\x1b\\` : "") + "\x1b]111\x1b\\";
|
|
87
|
+
const reset = (fgSGR !== null ? "\x1b[39m" : "") + (bgOSC !== null ? bgReset : "");
|
|
88
|
+
terminal.write(rc.defaults);
|
|
89
|
+
/* create the debug mode context with the emulated source and target screens
|
|
90
|
+
(where the target screen starts with the mapped default colors, too, and
|
|
91
|
+
where the terminal emulator is loaded in debug mode only) */
|
|
92
|
+
const xterm = config.debug ? (await import("@xterm/headless")).default : null;
|
|
93
|
+
const screen = (xt) => new xt.Terminal({
|
|
94
|
+
cols: terminal.columns(),
|
|
95
|
+
rows: terminal.rows(),
|
|
96
|
+
scrollback: 0,
|
|
97
|
+
allowProposedApi: true
|
|
98
|
+
});
|
|
99
|
+
const dbg = xterm === null ? null : {
|
|
100
|
+
src: screen(xterm),
|
|
101
|
+
dst: screen(xterm),
|
|
102
|
+
pending: 0,
|
|
103
|
+
prefix: null,
|
|
104
|
+
active: false,
|
|
105
|
+
x: 0,
|
|
106
|
+
y: 0,
|
|
107
|
+
covered: new Set(),
|
|
108
|
+
full: false,
|
|
109
|
+
ansi,
|
|
110
|
+
hints: {
|
|
111
|
+
srcfg: fg !== null ? String(fg) : "",
|
|
112
|
+
srcbg: bg !== null ? String(bg) : "",
|
|
113
|
+
dstfg: "",
|
|
114
|
+
dstbg: bg !== null ? quantone(options, bg) : ""
|
|
115
|
+
},
|
|
116
|
+
mouse: new Map(),
|
|
117
|
+
mouseOn: false,
|
|
118
|
+
output: terminal
|
|
119
|
+
};
|
|
120
|
+
if (dbg !== null) {
|
|
121
|
+
debugFeed(dbg, Buffer.alloc(0), rc.defaults);
|
|
122
|
+
/* track the mouse modes of the command (for restoring them after the debug overlay) */
|
|
123
|
+
for (const final of ["h", "l"]) {
|
|
124
|
+
dbg.dst.parser.registerCsiHandler({ prefix: "?", final }, (params) => {
|
|
125
|
+
for (const param of params)
|
|
126
|
+
if (typeof param === "number" && mouseModes.includes(param))
|
|
127
|
+
dbg.mouse.set(param, final === "h");
|
|
128
|
+
return false;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/* spawn the command in a PTY
|
|
133
|
+
(on failure, reset the mapped default colors and restore the terminal again) */
|
|
134
|
+
let term;
|
|
135
|
+
try {
|
|
136
|
+
term = pty.spawn(command, args, {
|
|
137
|
+
name: process.env["TERM"] ?? "xterm-color",
|
|
138
|
+
cols: terminal.columns(),
|
|
139
|
+
rows: terminal.rows(),
|
|
140
|
+
cwd: process.cwd(),
|
|
141
|
+
env: Object.fromEntries(Object.entries(process.env)
|
|
142
|
+
.filter((e) => e[1] !== undefined)),
|
|
143
|
+
encoding: null
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
terminal.write(reset);
|
|
148
|
+
terminal.restore();
|
|
149
|
+
throw new Error(`failed to spawn command "${command}": ${err instanceof Error ? err.message : String(err)}`);
|
|
150
|
+
}
|
|
151
|
+
/* pipe PTY output through color transformation to the terminal
|
|
152
|
+
(honoring back-pressure by pausing the PTY until the terminal drained) */
|
|
153
|
+
term.onData((data) => {
|
|
154
|
+
const chunk = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
155
|
+
const transformed = processChunk(chunk, rc);
|
|
156
|
+
if (dbg !== null)
|
|
157
|
+
debugFeed(dbg, chunk, transformed);
|
|
158
|
+
if (transformed.length > 0 && !terminal.write(transformed)) {
|
|
159
|
+
term.pause();
|
|
160
|
+
terminal.onDrain(() => { term.resume(); });
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
/* forward termination signals to the command
|
|
164
|
+
(for a regular exit which resets the mapped default colors) */
|
|
165
|
+
const offSignal = terminal.onSignal((signal) => { term.kill(signal); });
|
|
166
|
+
/* pipe the terminal input to the PTY (starting with the type-ahead received during the color query) */
|
|
167
|
+
if (answer.rest.length > 0)
|
|
168
|
+
term.write(answer.rest);
|
|
169
|
+
const offData = terminal.onData((data) => {
|
|
170
|
+
if (dbg !== null)
|
|
171
|
+
debugInput(dbg, data, (bytes) => { term.write(bytes); });
|
|
172
|
+
else
|
|
173
|
+
term.write(data);
|
|
174
|
+
});
|
|
175
|
+
/* handle terminal resize (of the emulated screens in debug mode, too) */
|
|
176
|
+
const offResize = terminal.onResize(() => {
|
|
177
|
+
const cols = terminal.columns();
|
|
178
|
+
const rows = terminal.rows();
|
|
179
|
+
term.resize(cols, rows);
|
|
180
|
+
if (dbg !== null) {
|
|
181
|
+
dbg.src.resize(cols, rows);
|
|
182
|
+
dbg.dst.resize(cols, rows);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
/* handle command exit: restore the terminal, reset the mapped default colors
|
|
186
|
+
(and the debug mouse mode), and resolve once all pending output is flushed */
|
|
187
|
+
return new Promise((resolve) => {
|
|
188
|
+
term.onExit(({ exitCode }) => {
|
|
189
|
+
offData();
|
|
190
|
+
offResize();
|
|
191
|
+
offSignal();
|
|
192
|
+
terminal.restore();
|
|
193
|
+
terminal.write((dbg?.mouseOn ? "\x1b[?1000l\x1b[?1006l" : "") + reset, () => { resolve(exitCode); });
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*!
|
|
3
|
+
** ansi-quantone -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
|
|
4
|
+
** Copyright (c) 2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
** Licensed under MIT license
|
|
6
|
+
*/
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import process from "node:process";
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import yargs from "yargs";
|
|
13
|
+
import { hideBin } from "yargs/helpers";
|
|
14
|
+
import YAML from "yaml";
|
|
15
|
+
import { run } from "./ansi-quantone-api.js";
|
|
16
|
+
/* ==== config file parsing ==== */
|
|
17
|
+
/* the palette options and whether their value is a color (or else a number) */
|
|
18
|
+
const optionKeys = {
|
|
19
|
+
neutral: true,
|
|
20
|
+
accent: true,
|
|
21
|
+
shades: false,
|
|
22
|
+
chromatic: false,
|
|
23
|
+
floor: false,
|
|
24
|
+
ceiling: false,
|
|
25
|
+
threshold: false,
|
|
26
|
+
shift: false
|
|
27
|
+
};
|
|
28
|
+
/* parse the YAML config file text into the configuration of a profile, by merging
|
|
29
|
+
its profile sections "default", "*", and the profile (if existing), in this order
|
|
30
|
+
(where later options override earlier ones and color mappings are accumulated) */
|
|
31
|
+
const parseConfig = (text, profile) => {
|
|
32
|
+
const config = YAML.parse(text) ?? {};
|
|
33
|
+
if (typeof config !== "object" || Array.isArray(config))
|
|
34
|
+
throw new Error("expected a mapping of profile sections");
|
|
35
|
+
const sections = config;
|
|
36
|
+
for (const [name, section] of Object.entries(sections))
|
|
37
|
+
if (section !== null && (typeof section !== "object" || Array.isArray(section)))
|
|
38
|
+
throw new Error(`invalid profile section "${name}" (expected mapping of options)`);
|
|
39
|
+
const result = { palette: {}, foreground: "auto", background: "none", reduce: false, mappings: [], debug: false };
|
|
40
|
+
const options = {};
|
|
41
|
+
for (const name of new Set(["default", "*", profile])) {
|
|
42
|
+
const section = (Object.hasOwn(sections, name) ? sections[name] : null) ?? {};
|
|
43
|
+
for (const [key, val] of Object.entries(section)) {
|
|
44
|
+
if (key === "foreground" || key === "background") {
|
|
45
|
+
if (!(typeof val === "number" || typeof val === "string"))
|
|
46
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected color, "auto", or "none")`);
|
|
47
|
+
result[key] = val;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (key === "reduce") {
|
|
51
|
+
if (typeof val !== "boolean")
|
|
52
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected boolean)`);
|
|
53
|
+
result.reduce = val;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (key === "mappings") {
|
|
57
|
+
if (!(Array.isArray(val) && val.every((v) => typeof v === "string")))
|
|
58
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected list of color mappings)`);
|
|
59
|
+
result.mappings.push(...val);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!Object.hasOwn(optionKeys, key))
|
|
63
|
+
throw new Error(`unknown option "${key}" in section "${name}"`);
|
|
64
|
+
const isColor = optionKeys[key];
|
|
65
|
+
if (!(typeof val === "number" || (isColor && typeof val === "string")))
|
|
66
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected ${isColor ? "color" : "number"})`);
|
|
67
|
+
options[key] = val;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
result.palette = options;
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
/* write a fatal error message and exit */
|
|
74
|
+
const fatalError = (context, err) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
77
|
+
process.stderr.write(`ansi-quantone: ERROR: ${context}: ${msg}\n`);
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
process.stderr.write(`ansi-quantone: ERROR: ${context}\n`);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
};
|
|
83
|
+
/* ==== CLI entry point ==== */
|
|
84
|
+
const main = async () => {
|
|
85
|
+
/* parse command-line arguments */
|
|
86
|
+
const argv = await yargs(hideBin(process.argv))
|
|
87
|
+
.usage("Usage: $0 " +
|
|
88
|
+
"[-C|--config <config-file>] " +
|
|
89
|
+
"[-p|--profile <name>] " +
|
|
90
|
+
"[-n|--neutral <color>] " +
|
|
91
|
+
"[-a|--accent <color>] " +
|
|
92
|
+
"[-s|--shades <count>] " +
|
|
93
|
+
"[-c|--chromatic <count>] " +
|
|
94
|
+
"[-l|--floor <percent>] " +
|
|
95
|
+
"[-u|--ceiling <percent>] " +
|
|
96
|
+
"[-t|--threshold <chroma>] " +
|
|
97
|
+
"[-S|--shift <degrees>] " +
|
|
98
|
+
"[-F|--foreground <color>|auto|none] " +
|
|
99
|
+
"[-B|--background <color>|auto|none] " +
|
|
100
|
+
"[-r|--reduce] " +
|
|
101
|
+
"[-m|--map <fg>/<bg>:<fg>/<bg>[,...]] " +
|
|
102
|
+
"[-d|--debug] " +
|
|
103
|
+
"[--] " +
|
|
104
|
+
"<command> [<args>...]")
|
|
105
|
+
.option("C", {
|
|
106
|
+
alias: "config",
|
|
107
|
+
type: "string",
|
|
108
|
+
description: "path to config file"
|
|
109
|
+
})
|
|
110
|
+
.option("p", {
|
|
111
|
+
alias: "profile",
|
|
112
|
+
type: "string",
|
|
113
|
+
default: "default",
|
|
114
|
+
description: "name of config profile section (merged after \"default\" and \"*\")"
|
|
115
|
+
})
|
|
116
|
+
.option("n", {
|
|
117
|
+
alias: "neutral",
|
|
118
|
+
type: "string",
|
|
119
|
+
description: "base color of the neutral ramp"
|
|
120
|
+
})
|
|
121
|
+
.option("a", {
|
|
122
|
+
alias: "accent",
|
|
123
|
+
type: "string",
|
|
124
|
+
description: "base color of the chromatic ramps"
|
|
125
|
+
})
|
|
126
|
+
.option("s", {
|
|
127
|
+
alias: "shades",
|
|
128
|
+
type: "number",
|
|
129
|
+
description: "number of shades per ramp"
|
|
130
|
+
})
|
|
131
|
+
.option("c", {
|
|
132
|
+
alias: "chromatic",
|
|
133
|
+
type: "number",
|
|
134
|
+
description: "number of chromatic hue segments"
|
|
135
|
+
})
|
|
136
|
+
.option("l", {
|
|
137
|
+
alias: "floor",
|
|
138
|
+
type: "number",
|
|
139
|
+
description: "OKLCH lightness floor of the chromatic ramps in percent"
|
|
140
|
+
})
|
|
141
|
+
.option("u", {
|
|
142
|
+
alias: "ceiling",
|
|
143
|
+
type: "number",
|
|
144
|
+
description: "OKLCH lightness inset below white of the chromatic ramps in percent"
|
|
145
|
+
})
|
|
146
|
+
.option("t", {
|
|
147
|
+
alias: "threshold",
|
|
148
|
+
type: "number",
|
|
149
|
+
description: "OKLCH chroma below which a color maps onto the neutral ramp"
|
|
150
|
+
})
|
|
151
|
+
.option("S", {
|
|
152
|
+
alias: "shift",
|
|
153
|
+
type: "number",
|
|
154
|
+
description: "OKLCH hue rotation of the input colors in degrees"
|
|
155
|
+
})
|
|
156
|
+
.option("F", {
|
|
157
|
+
alias: "foreground",
|
|
158
|
+
type: "string",
|
|
159
|
+
description: "input color of the terminal default foreground (or \"auto\" or \"none\")"
|
|
160
|
+
})
|
|
161
|
+
.option("B", {
|
|
162
|
+
alias: "background",
|
|
163
|
+
type: "string",
|
|
164
|
+
description: "input color of the terminal default background (or \"auto\" or \"none\")"
|
|
165
|
+
})
|
|
166
|
+
.option("r", {
|
|
167
|
+
alias: "reduce",
|
|
168
|
+
type: "boolean",
|
|
169
|
+
description: "reduce RGB/truecolor input colors onto the nearest ANSI color 0-255"
|
|
170
|
+
})
|
|
171
|
+
.option("m", {
|
|
172
|
+
alias: "map",
|
|
173
|
+
type: "string",
|
|
174
|
+
description: "color mapping(s) of input colors (repeatable or comma-separated)"
|
|
175
|
+
})
|
|
176
|
+
.option("d", {
|
|
177
|
+
alias: "debug",
|
|
178
|
+
type: "boolean",
|
|
179
|
+
default: false,
|
|
180
|
+
description: "enable debug mode (with debug overlay toggled by \"CTRL+a d\")"
|
|
181
|
+
})
|
|
182
|
+
.parserConfiguration({
|
|
183
|
+
"halt-at-non-option": true
|
|
184
|
+
})
|
|
185
|
+
.strict(true)
|
|
186
|
+
.help().alias("h", "help")
|
|
187
|
+
.version().alias("V", "version")
|
|
188
|
+
.parseAsync();
|
|
189
|
+
const configPath = argv.C ?? path.join(os.homedir(), ".ansi-quantone.yaml");
|
|
190
|
+
const appArgs = argv._;
|
|
191
|
+
if (appArgs.length === 0)
|
|
192
|
+
fatalError("no command specified");
|
|
193
|
+
const app = String(appArgs[0]);
|
|
194
|
+
const restArgs = appArgs.slice(1).map(String);
|
|
195
|
+
/* pass-through mode: without a TTY on stdout, run the application
|
|
196
|
+
directly, as recoloring through a PTY would break piped output */
|
|
197
|
+
if (!process.stdout.isTTY) {
|
|
198
|
+
const child = spawn(app, restArgs, { stdio: "inherit" });
|
|
199
|
+
for (const signal of ["SIGTERM", "SIGHUP"])
|
|
200
|
+
process.on(signal, () => { child.kill(signal); });
|
|
201
|
+
child.on("error", (err) => {
|
|
202
|
+
fatalError(`failed to spawn command "${app}"`, err);
|
|
203
|
+
});
|
|
204
|
+
child.on("exit", (code, signal) => {
|
|
205
|
+
if (signal !== null)
|
|
206
|
+
process.kill(process.pid, signal);
|
|
207
|
+
else
|
|
208
|
+
process.exit(code ?? 1);
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
/* read and parse the config file (an explicitly given one has to exist) */
|
|
213
|
+
let config = { palette: {}, foreground: "auto", background: "none", reduce: false, mappings: [], debug: false };
|
|
214
|
+
if (argv.C !== undefined || fs.existsSync(configPath)) {
|
|
215
|
+
try {
|
|
216
|
+
config = parseConfig(fs.readFileSync(configPath, "utf-8"), argv.p);
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
fatalError(`failed to read and parse config file "${configPath}"`, err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const options = config.palette;
|
|
223
|
+
/* let the command-line options override the config file options
|
|
224
|
+
(where the color mappings of the command-line, a repeatable option
|
|
225
|
+
with a comma-separated list each, are appended to the ones of the config file) */
|
|
226
|
+
if (argv.n !== undefined)
|
|
227
|
+
options.neutral = argv.n;
|
|
228
|
+
if (argv.a !== undefined)
|
|
229
|
+
options.accent = argv.a;
|
|
230
|
+
if (argv.s !== undefined)
|
|
231
|
+
options.shades = argv.s;
|
|
232
|
+
if (argv.c !== undefined)
|
|
233
|
+
options.chromatic = argv.c;
|
|
234
|
+
if (argv.l !== undefined)
|
|
235
|
+
options.floor = argv.l;
|
|
236
|
+
if (argv.u !== undefined)
|
|
237
|
+
options.ceiling = argv.u;
|
|
238
|
+
if (argv.t !== undefined)
|
|
239
|
+
options.threshold = argv.t;
|
|
240
|
+
if (argv.S !== undefined)
|
|
241
|
+
options.shift = argv.S;
|
|
242
|
+
if (argv.F !== undefined)
|
|
243
|
+
config.foreground = argv.F;
|
|
244
|
+
if (argv.B !== undefined)
|
|
245
|
+
config.background = argv.B;
|
|
246
|
+
if (argv.r !== undefined)
|
|
247
|
+
config.reduce = argv.r;
|
|
248
|
+
config.debug = argv.d;
|
|
249
|
+
config.mappings.push(...[].concat(argv.m ?? []).flatMap((m) => m.split(",")));
|
|
250
|
+
/* run the application with recoloring on the process stdio */
|
|
251
|
+
const exitCode = await run(config, app, restArgs);
|
|
252
|
+
process.exit(exitCode);
|
|
253
|
+
};
|
|
254
|
+
main().catch((err) => {
|
|
255
|
+
fatalError(err instanceof Error ? err.message : String(err));
|
|
256
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
.TH "ANSI-QUANTONE" "1" "December 2026" "" ""
|
|
2
|
+
.SH "NAME"
|
|
3
|
+
\fBansi-quantone\fR - Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
|
|
4
|
+
.SH "SYNOPSIS"
|
|
5
|
+
.P
|
|
6
|
+
\fBansi-quantone\fR \[lB]\fB-h\fR|\fB--help\fR\[rB] \[lB]\fB-V\fR|\fB--version\fR\[rB] \[lB]\fB-C\fR|\fB--config\fR \fIconfig-file\fR\[rB] \[lB]\fB-p\fR|\fB--profile\fR \fIname\fR\[rB] \[lB]\fB-n\fR|\fB--neutral\fR \fIcolor\fR\[rB] \[lB]\fB-a\fR|\fB--accent\fR \fIcolor\fR\[rB] \[lB]\fB-s\fR|\fB--shades\fR \fIcount\fR\[rB] \[lB]\fB-c\fR|\fB--chromatic\fR \fIcount\fR\[rB] \[lB]\fB-l\fR|\fB--floor\fR \fIpercent\fR\[rB] \[lB]\fB-u\fR|\fB--ceiling\fR \fIpercent\fR\[rB] \[lB]\fB-t\fR|\fB--threshold\fR \fIchroma\fR\[rB] \[lB]\fB-S\fR|\fB--shift\fR \fIdegrees\fR\[rB] \[lB]\fB-F\fR|\fB--foreground\fR \fIcolor\fR|\fBauto\fR|\fBnone\fR\[rB] \[lB]\fB-B\fR|\fB--background\fR \fIcolor\fR|\fBauto\fR|\fBnone\fR\[rB] \[lB]\fB-r\fR|\fB--reduce\fR\[rB] \[lB]\fB-m\fR|\fB--map\fR \fImapping\fR\[lB]\fB,\fR...\[rB]\[rB] \[lB]\fB-d\fR|\fB--debug\fR\[rB] \[lB]\fB--\fR\[rB] \fIcommand\fR \[lB]\fIargs\fR \[lB]...\[rB]\[rB]
|
|
7
|
+
.SH "DESCRIPTION"
|
|
8
|
+
.P
|
|
9
|
+
\fBansi-quantone\fR, \fIRecolor ANSI Color Sequences of CLI Commands onto a Toned Palette\fR, is a small command-line tool to recolor the output of an arbitrary command-line or Terminal User Interface (TUI) application. It spawns the target \fIcommand\fR inside a Pseudo-Terminal (PTY), intercepts the ANSI Select Graphic Rendition (SGR) color escape sequences of its output, and maps every foreground and background color onto a toned palette with the color quantizer \fBQuantone\fR.
|
|
10
|
+
.P
|
|
11
|
+
The palette consists of one \fIneutral\fR ramp and \fIN\fR chromatic ramps, each with the same number of \fIshades\fR. This way, the colors of any terminal application are reduced to an unobtrusive and uniform color scheme, without modifying the application itself.
|
|
12
|
+
.P
|
|
13
|
+
The command-line tool is a thin wrapper around the \fBansi-quantone-api\fR(3) API: it determines the configuration from the configuration file and the command-line options, and then runs the \fIcommand\fR on the terminal of the process. See \fBansi-quantone-api\fR(3) for the details of the recoloring, the color mapping rules, the debug mode, and the cache of the queried terminal colors (used within \fBtmux\fR popups). If standard output is not a terminal (e.g. a pipe), the \fIcommand\fR is run directly, without a PTY and without any recoloring.
|
|
14
|
+
.SH "OPTIONS"
|
|
15
|
+
.P
|
|
16
|
+
The following command-line options and arguments exist:
|
|
17
|
+
.RS 0
|
|
18
|
+
.IP \(bu 4
|
|
19
|
+
\[lB]\fB-h\fR|\fB--help\fR\[rB]: Show program usage information only.
|
|
20
|
+
.IP \(bu 4
|
|
21
|
+
\[lB]\fB-V\fR|\fB--version\fR\[rB]: Show program version information only.
|
|
22
|
+
.IP \(bu 4
|
|
23
|
+
\[lB]\fB-C\fR|\fB--config\fR \fIconfig-file\fR\[rB]: Read the palette options from the YAML \fIconfig-file\fR, which then has to exist. The default is \fB~/.ansi-quantone.yaml\fR (in the user's home directory), which is read only if it exists.
|
|
24
|
+
.IP \(bu 4
|
|
25
|
+
\[lB]\fB-p\fR|\fB--profile\fR \fIname\fR\[rB]: Use the profile section \fIname\fR of the configuration file, merged after the sections \fBdefault\fR and \fB*\fR. If no such section exists, only \fBdefault\fR and \fB*\fR are used (default: \fBdefault\fR).
|
|
26
|
+
.IP \(bu 4
|
|
27
|
+
\[lB]\fB-n\fR|\fB--neutral\fR \fIcolor\fR\[rB]: The base color of the neutral ramp (default: \fB#999999\fR).
|
|
28
|
+
.IP \(bu 4
|
|
29
|
+
\[lB]\fB-a\fR|\fB--accent\fR \fIcolor\fR\[rB]: The base color of the chromatic ramps (default: \fB#336699\fR).
|
|
30
|
+
.IP \(bu 4
|
|
31
|
+
\[lB]\fB-s\fR|\fB--shades\fR \fIcount\fR\[rB]: The number of shades per ramp (minimum: 2, default: 16).
|
|
32
|
+
.IP \(bu 4
|
|
33
|
+
\[lB]\fB-c\fR|\fB--chromatic\fR \fIcount\fR\[rB]: The number of chromatic hue segments (minimum: 1, default: 2).
|
|
34
|
+
.IP \(bu 4
|
|
35
|
+
\[lB]\fB-l\fR|\fB--floor\fR \fIpercent\fR\[rB]: The \fBOKLCH\fR lightness floor of the chromatic ramps (minimum: 0, default: 20).
|
|
36
|
+
.IP \(bu 4
|
|
37
|
+
\[lB]\fB-u\fR|\fB--ceiling\fR \fIpercent\fR\[rB]: The \fBOKLCH\fR lightness inset below white of the chromatic ramps (minimum: 0, default: 10, with floor plus ceiling below 100).
|
|
38
|
+
.IP \(bu 4
|
|
39
|
+
\[lB]\fB-t\fR|\fB--threshold\fR \fIchroma\fR\[rB]: The \fBOKLCH\fR chroma below which a color maps onto the neutral ramp (minimum: 0, default: 0.04).
|
|
40
|
+
.IP \(bu 4
|
|
41
|
+
\[lB]\fB-S\fR|\fB--shift\fR \fIdegrees\fR\[rB]: The \fBOKLCH\fR hue rotation of the input colors (range: -180...180, default: 34).
|
|
42
|
+
.IP \(bu 4
|
|
43
|
+
\[lB]\fB-F\fR|\fB--foreground\fR \fIcolor\fR|\fBauto\fR|\fBnone\fR\[rB]: The input color of the terminal default foreground, which is mapped onto the palette. With \fBauto\fR, it is queried from the terminal, falling back to ANSI color \fB7\fR. With \fBnone\fR, the default foreground is not recolored at all (default: \fBauto\fR).
|
|
44
|
+
.IP \(bu 4
|
|
45
|
+
\[lB]\fB-B\fR|\fB--background\fR \fIcolor\fR|\fBauto\fR|\fBnone\fR\[rB]: The input color of the terminal default background, which is mapped onto the palette. With \fBauto\fR, it is queried from the terminal, falling back to ANSI color \fB0\fR. With \fBnone\fR, the default background is not recolored at all (default: \fBnone\fR).
|
|
46
|
+
.IP \(bu 4
|
|
47
|
+
\[lB]\fB-r\fR|\fB--reduce\fR\[rB]: Reduce all RGB/truecolor input colors onto the perceptually nearest of the 256 ANSI colors, before applying the color mappings.
|
|
48
|
+
.IP \(bu 4
|
|
49
|
+
\[lB]\fB-m\fR|\fB--map\fR \fImapping\fR\[lB]\fB,\fR...\[rB]\[rB]: Remap input colors with one or more color \fImapping\fR rules of the form \fB<fg>/<bg> : <fg>/<bg>\fR. The option can be given multiple times, each time with a comma-separated list of rules.
|
|
50
|
+
.IP \(bu 4
|
|
51
|
+
\[lB]\fB-d\fR|\fB--debug\fR\[rB]: Enable the debug mode, in which the key sequence \fBCTRL+a d\fR toggles a debug overlay.
|
|
52
|
+
.IP \(bu 4
|
|
53
|
+
\fIcommand\fR \[lB]\fIargs\fR \[lB]...\[rB]\[rB]: The target application to spawn inside the PTY, followed by its optional arguments. Use \fB--\fR to separate \fBansi-quantone\fR options from the target command if the command starts with a hyphen.
|
|
54
|
+
.RE 0
|
|
55
|
+
|
|
56
|
+
.P
|
|
57
|
+
A \fIcolor\fR is given in the hex notation \fB#RRGGBB\fR or \fB#RGB\fR, as an ANSI color index \fB0\fR-\fB255\fR, or as an ANSI color name like \fBblue\fR or \fBbrightBlue\fR. The palette options given on the command-line override the ones of the configuration file, and all options not given at all take the defaults of \fBQuantone\fR.
|
|
58
|
+
.SH "CONFIGURATION"
|
|
59
|
+
.P
|
|
60
|
+
The configuration file of \fBansi-quantone\fR is a YAML file with a mapping of profile sections. The effective configuration is created by merging the sections \fBdefault\fR, \fB*\fR, and the profile selected with option \fB-p\fR (if it exists), in this order: later options override earlier ones, while the color mapping rules \fBmappings\fR are accumulated in this order. All sections are optional.
|
|
61
|
+
.P
|
|
62
|
+
Each profile section is a mapping of the optional palette options \fBneutral\fR, \fBaccent\fR, \fBshades\fR, \fBchromatic\fR, \fBfloor\fR, \fBceiling\fR, \fBthreshold\fR, and \fBshift\fR, the optional default color options \fBforeground\fR and \fBbackground\fR, the optional boolean \fBreduce\fR, and the optional list of color mapping rules \fBmappings\fR, which correspond to the command-line options of the same name (resp. \fB--map\fR). Other keys are rejected. The color mapping rules of the command-line are appended to the ones of the configuration file.
|
|
63
|
+
.SH "EXAMPLE"
|
|
64
|
+
.P
|
|
65
|
+
An example configuration file \fB~/.ansi-quantone.yaml\fR:
|
|
66
|
+
.P
|
|
67
|
+
.RS 2
|
|
68
|
+
.nf
|
|
69
|
+
default:
|
|
70
|
+
neutral: "#999999"
|
|
71
|
+
accent: "#336699"
|
|
72
|
+
shades: 16
|
|
73
|
+
chromatic: 2
|
|
74
|
+
floor: 20
|
|
75
|
+
ceiling: 10
|
|
76
|
+
threshold: 0.04
|
|
77
|
+
shift: 34
|
|
78
|
+
foreground: auto
|
|
79
|
+
background: none
|
|
80
|
+
"*":
|
|
81
|
+
reduce: true
|
|
82
|
+
mappings:
|
|
83
|
+
- "@warm/* : red/*"
|
|
84
|
+
- "232..243/* : default/*"
|
|
85
|
+
mc:
|
|
86
|
+
background: auto
|
|
87
|
+
mappings:
|
|
88
|
+
- "*/blue : */black"
|
|
89
|
+
.fi
|
|
90
|
+
.RE
|
|
91
|
+
.P
|
|
92
|
+
Example invocations:
|
|
93
|
+
.P
|
|
94
|
+
.RS 2
|
|
95
|
+
.nf
|
|
96
|
+
# run "mc" (Midnight Commander) with the default configuration file
|
|
97
|
+
$ ansi-quantone -- mc
|
|
98
|
+
|
|
99
|
+
# run "mc" with the "mc" profile of the default configuration file
|
|
100
|
+
$ ansi-quantone -p mc -- mc
|
|
101
|
+
|
|
102
|
+
# run "ls" with an explicit configuration file
|
|
103
|
+
$ ansi-quantone -C ~/.ansi-quantone-dark.yaml -- ls -l --color=always
|
|
104
|
+
|
|
105
|
+
# run "htop" with a palette of 8 shades of grey, blue, red, and green
|
|
106
|
+
$ ansi-quantone -n "#888" -a "#336699" -s 8 -c 3 -- htop
|
|
107
|
+
|
|
108
|
+
# run "vim" with recolored default foreground and background colors
|
|
109
|
+
$ ansi-quantone -F auto -B auto -- vim
|
|
110
|
+
|
|
111
|
+
# run "lazygit" with reduced RGB colors and remapped blue backgrounds
|
|
112
|
+
$ ansi-quantone -r -m "*/blue : */black" -m "yellow/* : white/*" -- lazygit
|
|
113
|
+
|
|
114
|
+
# run "htop" in debug mode to inspect the colors with "CTRL+a d"
|
|
115
|
+
$ ansi-quantone -d -- htop
|
|
116
|
+
.fi
|
|
117
|
+
.RE
|
|
118
|
+
.SH "SEE ALSO"
|
|
119
|
+
.P
|
|
120
|
+
\fBansi-quantone-api\fR(3), \fBquantone\fR(1), \fBansi-recolor\fR(1)
|
|
121
|
+
.SH "AUTHOR"
|
|
122
|
+
.P
|
|
123
|
+
Dr. Ralf S. Engelschall \fI\(larse@engelschall.com\(ra\fR
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
|
|
7
|
+
default:
|
|
8
|
+
neutral: "#999999"
|
|
9
|
+
accent: "#336699"
|
|
10
|
+
shades: 16
|
|
11
|
+
ceiling: 5
|
|
12
|
+
floor: 5
|
|
13
|
+
chromatic: 2
|
|
14
|
+
threshold: 0.02
|
|
15
|
+
shift: 34
|
|
16
|
+
foreground: auto
|
|
17
|
+
background: none
|
|
18
|
+
mappings:
|
|
19
|
+
- "green/*: blue/*"
|
|
20
|
+
- "brightGreen/*: brightBlue/*"
|
|
21
|
+
- "cyan/*: blue/*"
|
|
22
|
+
- "brightCyan/*: brightBlue/*"
|
|
23
|
+
|
|
24
|
+
claude:
|
|
25
|
+
mappings:
|
|
26
|
+
# unified diff coloring
|
|
27
|
+
- "*/#ffdcdc: red/254" # removed line
|
|
28
|
+
- "*/#dcffdc: blue/153" # added line
|
|
29
|
+
- "*/#ffc7c7: #ffffff/164" # removed word
|
|
30
|
+
- "*/#b2ffb2: #ffffff/62" # added word
|
|
31
|
+
|
|
32
|
+
# sent prompt
|
|
33
|
+
- "#000000/#f0f0f0: #ffffff/blue" # prompt text
|
|
34
|
+
- "#666666/#f0f0f0: #ffffff/blue" # prompt bar
|
|
35
|
+
- "default/#f0f0f0: #ffffff/blue" # prompt prefix
|
|
36
|
+
|
|
37
|
+
# dimmed text
|
|
38
|
+
- "#666666/default: 240/default" # dimmed lines
|
|
39
|
+
|