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,185 @@
|
|
|
1
|
+
|
|
2
|
+
# ansi-quantone-api(3) -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
|
|
3
|
+
|
|
4
|
+
## SYNOPSIS
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { run, stdioTerminal, type Config, type TerminalIO } from "ansi-quantone"
|
|
8
|
+
|
|
9
|
+
run(config: Config, command: string, args?: string[], terminal?: TerminalIO): Promise<number>
|
|
10
|
+
stdioTerminal(): TerminalIO
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## DESCRIPTION
|
|
14
|
+
|
|
15
|
+
`ansi-quantone-api`, the API of *Recolor ANSI Color Sequences of CLI
|
|
16
|
+
Commands onto a Toned Palette*, runs an arbitrary command-line or
|
|
17
|
+
Terminal User Interface (TUI) application inside a Pseudo-Terminal
|
|
18
|
+
(PTY) on a terminal, intercepts the ANSI Select Graphic Rendition (SGR)
|
|
19
|
+
color escape sequences of its output, and maps every foreground and
|
|
20
|
+
background color onto a toned palette with the color quantizer
|
|
21
|
+
**Quantone**. It is used by the command-line tool `ansi-quantone`(1).
|
|
22
|
+
|
|
23
|
+
All explicit colors are recolored: the ANSI basic 8 colors, the bright
|
|
24
|
+
8 colors, the 256-color palette, and RGB/truecolor. The RGB values of
|
|
25
|
+
the ANSI colors are queried from the terminal (via **OSC 4**), so the
|
|
26
|
+
actual color theme of the terminal is honored, with the canonical
|
|
27
|
+
**xterm** RGB values as the fallback for all colors not reported. The
|
|
28
|
+
recolored colors are always emitted as RGB/truecolor.
|
|
29
|
+
|
|
30
|
+
The terminal default colors carry no color value of their own. Hence,
|
|
31
|
+
their input colors are either given explicitly or queried from the
|
|
32
|
+
terminal (via **OSC 10** / **OSC 11**), and then mapped onto the palette
|
|
33
|
+
as well. The default foreground is established as an explicit
|
|
34
|
+
foreground color at startup and after every SGR 39 (default
|
|
35
|
+
foreground), SGR 0 (reset), RIS (full reset), and DECSTR (soft reset).
|
|
36
|
+
The default background instead is changed in the terminal itself (via
|
|
37
|
+
**OSC 11**), as only this also covers all never written screen cells.
|
|
38
|
+
On exit, it is restored to its originally queried color (via **OSC
|
|
39
|
+
11**) and then reset (via **OSC 111**), and any default background
|
|
40
|
+
changes of the *command* itself are suppressed meanwhile.
|
|
41
|
+
|
|
42
|
+
If the terminal has a cache file, all answered colors are stored there
|
|
43
|
+
(atomically, over the previously cached ones), and if it answers no
|
|
44
|
+
color queries at all (e.g. a **tmux** popup), the cached colors are
|
|
45
|
+
used instead of the fallbacks.
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
- `run(`*config*`,` *command*`,` *args*`,` *terminal*`)`:
|
|
50
|
+
Spawns the *command* with its arguments *args* (default: none) in a
|
|
51
|
+
PTY, with the current working directory and environment of the
|
|
52
|
+
process, and recolors its output onto the *terminal* (default:
|
|
53
|
+
`stdioTerminal()`) according to the *config*. It resolves to the
|
|
54
|
+
exit code of the *command*, once it exited and the *terminal* is
|
|
55
|
+
restored again. It rejects with an `Error` on an invalid *config* or
|
|
56
|
+
if the *command* cannot be spawned.
|
|
57
|
+
|
|
58
|
+
- `stdioTerminal()`:
|
|
59
|
+
Returns the terminal I/O of the process stdio: output is written to
|
|
60
|
+
standard output, input is read from standard input, which is switched
|
|
61
|
+
into raw mode, and the signals `SIGTERM` and `SIGHUP` are forwarded
|
|
62
|
+
to the *command*. Within **tmux** only, its cache file is
|
|
63
|
+
`$XDG_CACHE_HOME/ansi-quantone/colors-tmux-`*key*`.json` (default
|
|
64
|
+
`$XDG_CACHE_HOME`: `~/.cache`), where *key* is derived from the
|
|
65
|
+
**tmux** server socket and client PID, as each client can be a
|
|
66
|
+
different terminal with different colors.
|
|
67
|
+
|
|
68
|
+
- `Config`:
|
|
69
|
+
The configuration of `run()`, with the following fields:
|
|
70
|
+
`palette` (the palette options `neutral`, `accent`, `shades`,
|
|
71
|
+
`chromatic`, `floor`, `ceiling`, `threshold`, and `shift` of
|
|
72
|
+
**Quantone**), `foreground` and `background` (the input colors of the
|
|
73
|
+
terminal default colors: a color, `"auto"` to query them from the
|
|
74
|
+
terminal, falling back to ANSI color `7` resp. `0`, or `"none"` to
|
|
75
|
+
not recolor them), `reduce` (whether to reduce RGB/truecolor input
|
|
76
|
+
colors onto the perceptually nearest, by **OKLab** distance, of the
|
|
77
|
+
256 ANSI colors before the color mappings), `mappings` (the list of
|
|
78
|
+
color mapping rules, see **COLOR MAPPINGS**), and `debug` (whether to
|
|
79
|
+
enable the debug mode, see **DEBUG MODE**).
|
|
80
|
+
|
|
81
|
+
- `TerminalIO`:
|
|
82
|
+
The terminal I/O of a run, e.g. the process stdio or a bridge to the
|
|
83
|
+
terminal of a Web UI, with the following fields: `query` (whether the
|
|
84
|
+
terminal answers color queries), `cache` (the optional path of the
|
|
85
|
+
cache file of the queried colors), `columns()` and `rows()` (its current
|
|
86
|
+
size), `write(`*data*`,` *cb*`)` (write output, returning `false` on
|
|
87
|
+
back-pressure), `onDrain(`*cb*`)` (once the output is drained again),
|
|
88
|
+
`onData(`*cb*`)` (on input data), `onResize(`*cb*`)` (on size
|
|
89
|
+
changes), `onSignal(`*cb*`)` (on the termination requests `SIGTERM`
|
|
90
|
+
and `SIGHUP`), `setup()` (switch into raw mode), and `restore()`
|
|
91
|
+
(restore the original mode). The `onData`, `onResize`, and `onSignal`
|
|
92
|
+
functions return a function to unregister the callback again.
|
|
93
|
+
|
|
94
|
+
A *color* is given in the hex notation `#RRGGBB` or `#RGB`, as an ANSI
|
|
95
|
+
color index `0`-`255`, or as an ANSI color name like `blue` or
|
|
96
|
+
`brightBlue`. All palette options not given take the defaults of
|
|
97
|
+
**Quantone**.
|
|
98
|
+
|
|
99
|
+
## COLOR MAPPINGS
|
|
100
|
+
|
|
101
|
+
A color mapping rule has the form `<fg>/<bg> : <fg>/<bg>`, where the
|
|
102
|
+
left side is the source foreground/background color pair to match and
|
|
103
|
+
the right side is the replacement color pair. The colors are tracked
|
|
104
|
+
across all SGR sequences, so a rule always matches the current color
|
|
105
|
+
pair. The replacement colors are still mapped onto the palette. All
|
|
106
|
+
matching rules are applied in order, so later rules override earlier
|
|
107
|
+
ones. The processing order is: reducing (`reduce`), mapping, and
|
|
108
|
+
palette mapping.
|
|
109
|
+
|
|
110
|
+
A color of a rule is one of the following:
|
|
111
|
+
|
|
112
|
+
- `*`: On the source side, matches any color. On the target side,
|
|
113
|
+
keeps the current color.
|
|
114
|
+
|
|
115
|
+
- `default`: The terminal default color (SGR 39 / SGR 49).
|
|
116
|
+
|
|
117
|
+
- An ANSI color index `0`-`255` or an ANSI color name like `blue` or
|
|
118
|
+
`brightBlue`. It matches the ANSI color in all its forms, i.e.,
|
|
119
|
+
`blue`, `4`, SGR 34, and SGR 38;5;4 are the same color.
|
|
120
|
+
|
|
121
|
+
- A hex color `#RRGGBB` or `#RGB`. It matches RGB/truecolor input
|
|
122
|
+
colors with exactly this value only.
|
|
123
|
+
|
|
124
|
+
- A range `<from>..<to>` of ANSI color indices (e.g. `232..243`).
|
|
125
|
+
Allowed on the source side only.
|
|
126
|
+
|
|
127
|
+
- A class `@<family>`, `@<tone>`, or `@<family>.<tone>` (e.g. `@warm`,
|
|
128
|
+
`@light`, or `@cool.dark`), matching all colors except `default` by
|
|
129
|
+
their RGB value (for ANSI colors, as queried from the terminal). The
|
|
130
|
+
family is `grey` (alias `gray`) for colors with a chroma (max minus
|
|
131
|
+
min channel) below 48, `warm` for the hues from 280° to 60°, and
|
|
132
|
+
`cool` for the hues from 60° to 280°. The tone is `dark` for a luma
|
|
133
|
+
(Rec. 709) below 64, `light` for one above 180, and `mid` otherwise.
|
|
134
|
+
Allowed on the source side only.
|
|
135
|
+
|
|
136
|
+
## DEBUG MODE
|
|
137
|
+
|
|
138
|
+
In debug mode (`debug`), the source and target output are additionally
|
|
139
|
+
emulated on two virtual screens. The key sequence `CTRL+a d` toggles a
|
|
140
|
+
debug overlay, consisting of a debug cursor and an info box. The debug
|
|
141
|
+
cursor is a red 3x3 frame with rounded corners around the observed
|
|
142
|
+
character, initially in the center of the screen.
|
|
143
|
+
|
|
144
|
+
The info box in the top-right corner (or in the top-left corner, if the
|
|
145
|
+
debug cursor is in the upper-right quadrant of the screen) shows the
|
|
146
|
+
position and code point of the observed character, plus its source and
|
|
147
|
+
target foreground and background colors, each with a color swatch. For
|
|
148
|
+
a source ANSI color, its RGB value as the input of the recoloring is
|
|
149
|
+
shown, too.
|
|
150
|
+
|
|
151
|
+
While the overlay is displayed, the cursor keys move the debug cursor,
|
|
152
|
+
a left mouse click positions it, and all other input is discarded. For
|
|
153
|
+
this, the mouse click reporting of the terminal is enabled meanwhile,
|
|
154
|
+
and the mouse modes of the *command* are restored afterwards. The key
|
|
155
|
+
sequences `CTRL+a a` and `CTRL+a CTRL+a` send a literal `CTRL+a` to the
|
|
156
|
+
*command*.
|
|
157
|
+
|
|
158
|
+
The keys are recognized in their legacy encoding as well as in the
|
|
159
|
+
encodings of the xterm *modifyOtherKeys* and the *kitty keyboard
|
|
160
|
+
protocol*, as enabled by some applications. Inside **tmux**(1) or
|
|
161
|
+
**screen**(1) with `CTRL+a` as their prefix key, press `CTRL+a a d`.
|
|
162
|
+
|
|
163
|
+
## EXAMPLE
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { run } from "ansi-quantone"
|
|
167
|
+
|
|
168
|
+
const exitCode = await run({
|
|
169
|
+
palette: { neutral: "#999999", accent: "#336699", shades: 16 },
|
|
170
|
+
foreground: "auto",
|
|
171
|
+
background: "none",
|
|
172
|
+
reduce: true,
|
|
173
|
+
mappings: [ "*/blue : */black" ],
|
|
174
|
+
debug: false
|
|
175
|
+
}, "htop")
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## SEE ALSO
|
|
179
|
+
|
|
180
|
+
`ansi-quantone`(1), `quantone`(1), `ansi-recolor`(1)
|
|
181
|
+
|
|
182
|
+
## AUTHOR
|
|
183
|
+
|
|
184
|
+
Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
185
|
+
|
|
@@ -0,0 +1,223 @@
|
|
|
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
|
+
import process from "node:process"
|
|
8
|
+
|
|
9
|
+
import * as pty from "node-pty"
|
|
10
|
+
import { quantone, type QuantoneOptions, type QuantoneColor } from "quantone"
|
|
11
|
+
|
|
12
|
+
import type { DefaultColor, Config, TerminalIO } from "./ansi-quantone-api-types.js"
|
|
13
|
+
import { xtermColors, queryColors, loadColors, saveColors, stdioTerminal } from "./ansi-quantone-api-terminal.js"
|
|
14
|
+
import { type ColorMapping, parseMapping } from "./ansi-quantone-api-mapping.js"
|
|
15
|
+
import { rgbToOKLab } from "./ansi-quantone-api-color.js"
|
|
16
|
+
import { type Recolor, mapColor } from "./ansi-quantone-api-sgr.js"
|
|
17
|
+
import { processChunk } from "./ansi-quantone-api-stream.js"
|
|
18
|
+
import { type Debug, mouseModes, debugFeed, debugInput } from "./ansi-quantone-api-debug.js"
|
|
19
|
+
|
|
20
|
+
export type { QuantoneOptions, QuantoneColor, DefaultColor, Config, TerminalIO }
|
|
21
|
+
export { stdioTerminal }
|
|
22
|
+
|
|
23
|
+
/* ==== API entry point ==== */
|
|
24
|
+
|
|
25
|
+
/* run a command in a PTY on a terminal and recolor its output according to a configuration,
|
|
26
|
+
and resolve to the exit code of the command once it exited */
|
|
27
|
+
export const run = async (config: Config, command: string, args: string[] = [],
|
|
28
|
+
terminal: TerminalIO = stdioTerminal()): Promise<number> => {
|
|
29
|
+
const options = config.palette
|
|
30
|
+
|
|
31
|
+
/* validate the palette options and explicit default colors once upfront */
|
|
32
|
+
try {
|
|
33
|
+
quantone(options, 0)
|
|
34
|
+
for (const color of [ config.foreground, config.background ])
|
|
35
|
+
if (color !== "auto" && color !== "none")
|
|
36
|
+
quantone(options, color)
|
|
37
|
+
}
|
|
38
|
+
catch (err: unknown) {
|
|
39
|
+
throw new Error(`invalid palette options: ${err instanceof Error ? err.message : String(err)}`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* parse the color mappings */
|
|
43
|
+
const mappings: ColorMapping[] = []
|
|
44
|
+
for (const text of config.mappings) {
|
|
45
|
+
try {
|
|
46
|
+
mappings.push(parseMapping(text))
|
|
47
|
+
}
|
|
48
|
+
catch (err: unknown) {
|
|
49
|
+
throw new Error(`invalid color mapping "${text}": ${err instanceof Error ? err.message : String(err)}`)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* switch the terminal into raw mode */
|
|
54
|
+
terminal.setup()
|
|
55
|
+
|
|
56
|
+
/* query the terminal for its ANSI colors and the default colors which are determined
|
|
57
|
+
automatically (the default background always, to explicitly restore it on exit) */
|
|
58
|
+
const codes = [
|
|
59
|
+
...Array.from({ length: 256 }, (_, i) => `4;${i}`),
|
|
60
|
+
...(config.foreground === "auto" ? [ "10" ] : []),
|
|
61
|
+
...(config.background !== "none" ? [ "11" ] : [])
|
|
62
|
+
]
|
|
63
|
+
const answer = terminal.query ?
|
|
64
|
+
await queryColors(terminal, codes) : { colors: new Map<string, string>(), rest: Buffer.alloc(0) }
|
|
65
|
+
|
|
66
|
+
/* cache the answered colors, or fall back to the cached colors if none were answered
|
|
67
|
+
(e.g. in a tmux popup, which answers no color queries at all) */
|
|
68
|
+
if (terminal.cache !== undefined) {
|
|
69
|
+
if (answer.colors.size > 0)
|
|
70
|
+
saveColors(terminal.cache, answer.colors)
|
|
71
|
+
else
|
|
72
|
+
answer.colors = loadColors(terminal.cache)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* determine the recoloring context (with the canonical xterm values as the fallbacks
|
|
76
|
+
of the ANSI colors, and the ANSI colors 7/0 as the fallbacks of the default colors) */
|
|
77
|
+
const ansi = xtermColors.map((hex, i) => answer.colors.get(`4;${i}`) ?? hex)
|
|
78
|
+
const resolveColor = (color: DefaultColor, code: string, fallback: number): QuantoneColor | null =>
|
|
79
|
+
color === "none" ? null : color === "auto" ? (answer.colors.get(code) ?? ansi[fallback]) : color
|
|
80
|
+
const fg = resolveColor(config.foreground, "10", 7)
|
|
81
|
+
const bg = resolveColor(config.background, "11", 0)
|
|
82
|
+
const fgSGR = fg !== null ? mapColor(options, fg, "fg") : null
|
|
83
|
+
const bgOSC = bg !== null ?
|
|
84
|
+
`\x1b]11;${quantone(options, bg).replace(/^#(..)(..)(..)$/, "rgb:$1/$2/$3")}\x1b\\` : null
|
|
85
|
+
const rc: Recolor = {
|
|
86
|
+
options,
|
|
87
|
+
fgSGR,
|
|
88
|
+
bgOSC,
|
|
89
|
+
defaults: Buffer.from((bgOSC ?? "") + (fgSGR !== null ? `\x1b[${fgSGR.join(";")}m` : "")),
|
|
90
|
+
ansi,
|
|
91
|
+
lab: config.reduce ? ansi.map((hex) => rgbToOKLab(parseInt(hex.substring(1), 16))) : null,
|
|
92
|
+
reduced: new Map<number, number>(),
|
|
93
|
+
mappings,
|
|
94
|
+
src: { fg: { type: "default" }, bg: { type: "default" } },
|
|
95
|
+
dst: { fg: { type: "default" }, bg: { type: "default" } },
|
|
96
|
+
buf: Buffer.alloc(0)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* establish the mapped default colors and reset them again on exit
|
|
100
|
+
(the background is explicitly restored, as not all terminals support OSC 111) */
|
|
101
|
+
const bgOrig = answer.colors.get("11")
|
|
102
|
+
const bgReset = (bgOrig !== undefined ?
|
|
103
|
+
`\x1b]11;${bgOrig.replace(/^#(..)(..)(..)$/, "rgb:$1/$2/$3")}\x1b\\` : "") + "\x1b]111\x1b\\"
|
|
104
|
+
const reset = (fgSGR !== null ? "\x1b[39m" : "") + (bgOSC !== null ? bgReset : "")
|
|
105
|
+
terminal.write(rc.defaults)
|
|
106
|
+
|
|
107
|
+
/* create the debug mode context with the emulated source and target screens
|
|
108
|
+
(where the target screen starts with the mapped default colors, too, and
|
|
109
|
+
where the terminal emulator is loaded in debug mode only) */
|
|
110
|
+
const xterm = config.debug ? (await import("@xterm/headless")).default : null
|
|
111
|
+
const screen = (xt: NonNullable<typeof xterm>) => new xt.Terminal({
|
|
112
|
+
cols: terminal.columns(),
|
|
113
|
+
rows: terminal.rows(),
|
|
114
|
+
scrollback: 0,
|
|
115
|
+
allowProposedApi: true
|
|
116
|
+
})
|
|
117
|
+
const dbg: Debug | null = xterm === null ? null : {
|
|
118
|
+
src: screen(xterm),
|
|
119
|
+
dst: screen(xterm),
|
|
120
|
+
pending: 0,
|
|
121
|
+
prefix: null,
|
|
122
|
+
active: false,
|
|
123
|
+
x: 0,
|
|
124
|
+
y: 0,
|
|
125
|
+
covered: new Set<number>(),
|
|
126
|
+
full: false,
|
|
127
|
+
ansi,
|
|
128
|
+
hints: {
|
|
129
|
+
srcfg: fg !== null ? String(fg) : "",
|
|
130
|
+
srcbg: bg !== null ? String(bg) : "",
|
|
131
|
+
dstfg: "",
|
|
132
|
+
dstbg: bg !== null ? quantone(options, bg) : ""
|
|
133
|
+
},
|
|
134
|
+
mouse: new Map<number, boolean>(),
|
|
135
|
+
mouseOn: false,
|
|
136
|
+
output: terminal
|
|
137
|
+
}
|
|
138
|
+
if (dbg !== null) {
|
|
139
|
+
debugFeed(dbg, Buffer.alloc(0), rc.defaults)
|
|
140
|
+
|
|
141
|
+
/* track the mouse modes of the command (for restoring them after the debug overlay) */
|
|
142
|
+
for (const final of [ "h", "l" ]) {
|
|
143
|
+
dbg.dst.parser.registerCsiHandler({ prefix: "?", final }, (params) => {
|
|
144
|
+
for (const param of params)
|
|
145
|
+
if (typeof param === "number" && mouseModes.includes(param))
|
|
146
|
+
dbg.mouse.set(param, final === "h")
|
|
147
|
+
return false
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* spawn the command in a PTY
|
|
153
|
+
(on failure, reset the mapped default colors and restore the terminal again) */
|
|
154
|
+
let term: pty.IPty
|
|
155
|
+
try {
|
|
156
|
+
term = pty.spawn(command, args, {
|
|
157
|
+
name: process.env["TERM"] ?? "xterm-color",
|
|
158
|
+
cols: terminal.columns(),
|
|
159
|
+
rows: terminal.rows(),
|
|
160
|
+
cwd: process.cwd(),
|
|
161
|
+
env: Object.fromEntries(
|
|
162
|
+
Object.entries(process.env)
|
|
163
|
+
.filter((e): e is [ string, string ] => e[1] !== undefined)),
|
|
164
|
+
encoding: null
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
catch (err: unknown) {
|
|
168
|
+
terminal.write(reset)
|
|
169
|
+
terminal.restore()
|
|
170
|
+
throw new Error(`failed to spawn command "${command}": ${err instanceof Error ? err.message : String(err)}`)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* pipe PTY output through color transformation to the terminal
|
|
174
|
+
(honoring back-pressure by pausing the PTY until the terminal drained) */
|
|
175
|
+
term.onData((data: Buffer | string) => {
|
|
176
|
+
const chunk = Buffer.isBuffer(data) ? data : Buffer.from(data)
|
|
177
|
+
const transformed = processChunk(chunk, rc)
|
|
178
|
+
if (dbg !== null)
|
|
179
|
+
debugFeed(dbg, chunk, transformed)
|
|
180
|
+
if (transformed.length > 0 && !terminal.write(transformed)) {
|
|
181
|
+
term.pause()
|
|
182
|
+
terminal.onDrain(() => { term.resume() })
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
/* forward termination signals to the command
|
|
187
|
+
(for a regular exit which resets the mapped default colors) */
|
|
188
|
+
const offSignal = terminal.onSignal((signal) => { term.kill(signal) })
|
|
189
|
+
|
|
190
|
+
/* pipe the terminal input to the PTY (starting with the type-ahead received during the color query) */
|
|
191
|
+
if (answer.rest.length > 0)
|
|
192
|
+
term.write(answer.rest)
|
|
193
|
+
const offData = terminal.onData((data: Buffer) => {
|
|
194
|
+
if (dbg !== null)
|
|
195
|
+
debugInput(dbg, data, (bytes) => { term.write(bytes) })
|
|
196
|
+
else
|
|
197
|
+
term.write(data)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
/* handle terminal resize (of the emulated screens in debug mode, too) */
|
|
201
|
+
const offResize = terminal.onResize(() => {
|
|
202
|
+
const cols = terminal.columns()
|
|
203
|
+
const rows = terminal.rows()
|
|
204
|
+
term.resize(cols, rows)
|
|
205
|
+
if (dbg !== null) {
|
|
206
|
+
dbg.src.resize(cols, rows)
|
|
207
|
+
dbg.dst.resize(cols, rows)
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
/* handle command exit: restore the terminal, reset the mapped default colors
|
|
212
|
+
(and the debug mouse mode), and resolve once all pending output is flushed */
|
|
213
|
+
return new Promise((resolve) => {
|
|
214
|
+
term.onExit(({ exitCode }: { exitCode: number }) => {
|
|
215
|
+
offData()
|
|
216
|
+
offResize()
|
|
217
|
+
offSignal()
|
|
218
|
+
terminal.restore()
|
|
219
|
+
terminal.write((dbg?.mouseOn ? "\x1b[?1000l\x1b[?1006l" : "") + reset, () => { resolve(exitCode) })
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
|
|
2
|
+
# ansi-quantone(1) -- Recolor ANSI Color Sequences of CLI Commands onto a Toned Palette
|
|
3
|
+
|
|
4
|
+
## SYNOPSIS
|
|
5
|
+
|
|
6
|
+
`ansi-quantone`
|
|
7
|
+
\[`-h`|`--help`\]
|
|
8
|
+
\[`-V`|`--version`\]
|
|
9
|
+
\[`-C`|`--config` *config-file*\]
|
|
10
|
+
\[`-p`|`--profile` *name*\]
|
|
11
|
+
\[`-n`|`--neutral` *color*\]
|
|
12
|
+
\[`-a`|`--accent` *color*\]
|
|
13
|
+
\[`-s`|`--shades` *count*\]
|
|
14
|
+
\[`-c`|`--chromatic` *count*\]
|
|
15
|
+
\[`-l`|`--floor` *percent*\]
|
|
16
|
+
\[`-u`|`--ceiling` *percent*\]
|
|
17
|
+
\[`-t`|`--threshold` *chroma*\]
|
|
18
|
+
\[`-S`|`--shift` *degrees*\]
|
|
19
|
+
\[`-F`|`--foreground` *color*|`auto`|`none`\]
|
|
20
|
+
\[`-B`|`--background` *color*|`auto`|`none`\]
|
|
21
|
+
\[`-r`|`--reduce`\]
|
|
22
|
+
\[`-m`|`--map` *mapping*\[`,`...\]\]
|
|
23
|
+
\[`-d`|`--debug`\]
|
|
24
|
+
\[`--`\]
|
|
25
|
+
*command* \[*args* \[...\]\]
|
|
26
|
+
|
|
27
|
+
## DESCRIPTION
|
|
28
|
+
|
|
29
|
+
`ansi-quantone`, *Recolor ANSI Color Sequences of CLI Commands onto a
|
|
30
|
+
Toned Palette*, is a small command-line tool to recolor the output of an
|
|
31
|
+
arbitrary command-line or Terminal User Interface (TUI) application. It
|
|
32
|
+
spawns the target *command* inside a Pseudo-Terminal (PTY), intercepts
|
|
33
|
+
the ANSI Select Graphic Rendition (SGR) color escape sequences of its
|
|
34
|
+
output, and maps every foreground and background color onto a toned
|
|
35
|
+
palette with the color quantizer **Quantone**.
|
|
36
|
+
|
|
37
|
+
The palette consists of one *neutral* ramp and *N* chromatic ramps, each
|
|
38
|
+
with the same number of *shades*. This way, the colors of any terminal
|
|
39
|
+
application are reduced to an unobtrusive and uniform color scheme,
|
|
40
|
+
without modifying the application itself.
|
|
41
|
+
|
|
42
|
+
The command-line tool is a thin wrapper around the `ansi-quantone-api`(3)
|
|
43
|
+
API: it determines the configuration from the configuration file and the
|
|
44
|
+
command-line options, and then runs the *command* on the terminal of the
|
|
45
|
+
process. See `ansi-quantone-api`(3) for the details of the recoloring,
|
|
46
|
+
the color mapping rules, the debug mode, and the cache of the queried
|
|
47
|
+
terminal colors (used within **tmux** popups). If standard output is not a
|
|
48
|
+
terminal (e.g. a pipe), the *command* is run directly, without a PTY and
|
|
49
|
+
without any recoloring.
|
|
50
|
+
|
|
51
|
+
## OPTIONS
|
|
52
|
+
|
|
53
|
+
The following command-line options and arguments exist:
|
|
54
|
+
|
|
55
|
+
- \[`-h`|`--help`\]:
|
|
56
|
+
Show program usage information only.
|
|
57
|
+
|
|
58
|
+
- \[`-V`|`--version`\]:
|
|
59
|
+
Show program version information only.
|
|
60
|
+
|
|
61
|
+
- \[`-C`|`--config` *config-file*\]:
|
|
62
|
+
Read the palette options from the YAML *config-file*, which then has
|
|
63
|
+
to exist. The default is `~/.ansi-quantone.yaml` (in the user's home
|
|
64
|
+
directory), which is read only if it exists.
|
|
65
|
+
|
|
66
|
+
- \[`-p`|`--profile` *name*\]:
|
|
67
|
+
Use the profile section *name* of the configuration file, merged
|
|
68
|
+
after the sections `default` and `*`. If no such section exists, only
|
|
69
|
+
`default` and `*` are used (default: `default`).
|
|
70
|
+
|
|
71
|
+
- \[`-n`|`--neutral` *color*\]:
|
|
72
|
+
The base color of the neutral ramp (default: `#999999`).
|
|
73
|
+
|
|
74
|
+
- \[`-a`|`--accent` *color*\]:
|
|
75
|
+
The base color of the chromatic ramps (default: `#336699`).
|
|
76
|
+
|
|
77
|
+
- \[`-s`|`--shades` *count*\]:
|
|
78
|
+
The number of shades per ramp (minimum: 2, default: 16).
|
|
79
|
+
|
|
80
|
+
- \[`-c`|`--chromatic` *count*\]:
|
|
81
|
+
The number of chromatic hue segments (minimum: 1, default: 2).
|
|
82
|
+
|
|
83
|
+
- \[`-l`|`--floor` *percent*\]:
|
|
84
|
+
The **OKLCH** lightness floor of the chromatic ramps
|
|
85
|
+
(minimum: 0, default: 20).
|
|
86
|
+
|
|
87
|
+
- \[`-u`|`--ceiling` *percent*\]:
|
|
88
|
+
The **OKLCH** lightness inset below white of the chromatic ramps
|
|
89
|
+
(minimum: 0, default: 10, with floor plus ceiling below 100).
|
|
90
|
+
|
|
91
|
+
- \[`-t`|`--threshold` *chroma*\]:
|
|
92
|
+
The **OKLCH** chroma below which a color maps onto the neutral ramp
|
|
93
|
+
(minimum: 0, default: 0.04).
|
|
94
|
+
|
|
95
|
+
- \[`-S`|`--shift` *degrees*\]:
|
|
96
|
+
The **OKLCH** hue rotation of the input colors
|
|
97
|
+
(range: -180...180, default: 34).
|
|
98
|
+
|
|
99
|
+
- \[`-F`|`--foreground` *color*|`auto`|`none`\]:
|
|
100
|
+
The input color of the terminal default foreground, which is
|
|
101
|
+
mapped onto the palette. With `auto`, it is queried from the
|
|
102
|
+
terminal, falling back to ANSI color `7`. With `none`, the default
|
|
103
|
+
foreground is not recolored at all (default: `auto`).
|
|
104
|
+
|
|
105
|
+
- \[`-B`|`--background` *color*|`auto`|`none`\]:
|
|
106
|
+
The input color of the terminal default background, which is
|
|
107
|
+
mapped onto the palette. With `auto`, it is queried from the
|
|
108
|
+
terminal, falling back to ANSI color `0`. With `none`, the default
|
|
109
|
+
background is not recolored at all (default: `none`).
|
|
110
|
+
|
|
111
|
+
- \[`-r`|`--reduce`\]:
|
|
112
|
+
Reduce all RGB/truecolor input colors onto the perceptually nearest
|
|
113
|
+
of the 256 ANSI colors, before applying the color mappings.
|
|
114
|
+
|
|
115
|
+
- \[`-m`|`--map` *mapping*\[`,`...\]\]:
|
|
116
|
+
Remap input colors with one or more color *mapping* rules of the form
|
|
117
|
+
`<fg>/<bg> : <fg>/<bg>`. The option can be given multiple times, each
|
|
118
|
+
time with a comma-separated list of rules.
|
|
119
|
+
|
|
120
|
+
- \[`-d`|`--debug`\]:
|
|
121
|
+
Enable the debug mode, in which the key sequence `CTRL+a d` toggles
|
|
122
|
+
a debug overlay.
|
|
123
|
+
|
|
124
|
+
- *command* \[*args* \[...\]\]:
|
|
125
|
+
The target application to spawn inside the PTY, followed by its
|
|
126
|
+
optional arguments. Use `--` to separate `ansi-quantone` options
|
|
127
|
+
from the target command if the command starts with a hyphen.
|
|
128
|
+
|
|
129
|
+
A *color* is given in the hex notation `#RRGGBB` or `#RGB`, as an ANSI
|
|
130
|
+
color index `0`-`255`, or as an ANSI color name like `blue` or
|
|
131
|
+
`brightBlue`. The palette options given on the command-line override the
|
|
132
|
+
ones of the configuration file, and all options not given at all take
|
|
133
|
+
the defaults of **Quantone**.
|
|
134
|
+
|
|
135
|
+
## CONFIGURATION
|
|
136
|
+
|
|
137
|
+
The configuration file of `ansi-quantone` is a YAML file with a mapping
|
|
138
|
+
of profile sections. The effective configuration is created by merging
|
|
139
|
+
the sections `default`, `*`, and the profile selected with option `-p`
|
|
140
|
+
(if it exists), in this order: later options override earlier ones,
|
|
141
|
+
while the color mapping rules `mappings` are accumulated in this order.
|
|
142
|
+
All sections are optional.
|
|
143
|
+
|
|
144
|
+
Each profile section is a mapping of the optional palette options
|
|
145
|
+
`neutral`, `accent`, `shades`, `chromatic`, `floor`, `ceiling`,
|
|
146
|
+
`threshold`, and `shift`, the optional default color options
|
|
147
|
+
`foreground` and `background`, the optional boolean `reduce`, and the
|
|
148
|
+
optional list of color mapping rules `mappings`, which correspond to the
|
|
149
|
+
command-line options of the same name (resp. `--map`). Other keys are
|
|
150
|
+
rejected. The color mapping rules of the command-line are appended to
|
|
151
|
+
the ones of the configuration file.
|
|
152
|
+
|
|
153
|
+
## EXAMPLE
|
|
154
|
+
|
|
155
|
+
An example configuration file `~/.ansi-quantone.yaml`:
|
|
156
|
+
|
|
157
|
+
```yaml
|
|
158
|
+
default:
|
|
159
|
+
neutral: "#999999"
|
|
160
|
+
accent: "#336699"
|
|
161
|
+
shades: 16
|
|
162
|
+
chromatic: 2
|
|
163
|
+
floor: 20
|
|
164
|
+
ceiling: 10
|
|
165
|
+
threshold: 0.04
|
|
166
|
+
shift: 34
|
|
167
|
+
foreground: auto
|
|
168
|
+
background: none
|
|
169
|
+
"*":
|
|
170
|
+
reduce: true
|
|
171
|
+
mappings:
|
|
172
|
+
- "@warm/* : red/*"
|
|
173
|
+
- "232..243/* : default/*"
|
|
174
|
+
mc:
|
|
175
|
+
background: auto
|
|
176
|
+
mappings:
|
|
177
|
+
- "*/blue : */black"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Example invocations:
|
|
181
|
+
|
|
182
|
+
```sh
|
|
183
|
+
# run "mc" (Midnight Commander) with the default configuration file
|
|
184
|
+
$ ansi-quantone -- mc
|
|
185
|
+
|
|
186
|
+
# run "mc" with the "mc" profile of the default configuration file
|
|
187
|
+
$ ansi-quantone -p mc -- mc
|
|
188
|
+
|
|
189
|
+
# run "ls" with an explicit configuration file
|
|
190
|
+
$ ansi-quantone -C ~/.ansi-quantone-dark.yaml -- ls -l --color=always
|
|
191
|
+
|
|
192
|
+
# run "htop" with a palette of 8 shades of grey, blue, red, and green
|
|
193
|
+
$ ansi-quantone -n "#888" -a "#336699" -s 8 -c 3 -- htop
|
|
194
|
+
|
|
195
|
+
# run "vim" with recolored default foreground and background colors
|
|
196
|
+
$ ansi-quantone -F auto -B auto -- vim
|
|
197
|
+
|
|
198
|
+
# run "lazygit" with reduced RGB colors and remapped blue backgrounds
|
|
199
|
+
$ ansi-quantone -r -m "*/blue : */black" -m "yellow/* : white/*" -- lazygit
|
|
200
|
+
|
|
201
|
+
# run "htop" in debug mode to inspect the colors with "CTRL+a d"
|
|
202
|
+
$ ansi-quantone -d -- htop
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## SEE ALSO
|
|
206
|
+
|
|
207
|
+
`ansi-quantone-api`(3), `quantone`(1), `ansi-recolor`(1)
|
|
208
|
+
|
|
209
|
+
## AUTHOR
|
|
210
|
+
|
|
211
|
+
Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
212
|
+
|