ansi-quantone 0.0.0-stage → 0.9.0
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 +16 -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 +12 -0
- package/dst/ansi-quantone-api-terminal.js +91 -0
- package/dst/ansi-quantone-api-types.d.ts +27 -0
- package/dst/ansi-quantone-api-types.js +6 -0
- package/dst/ansi-quantone-api.3 +85 -0
- package/dst/ansi-quantone-api.d.ts +11 -0
- package/dst/ansi-quantone-api.js +188 -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 +102 -0
- package/src/ansi-quantone-api-types.ts +39 -0
- package/src/ansi-quantone-api.md +175 -0
- package/src/ansi-quantone-api.ts +214 -0
- package/src/ansi-quantone-cli.md +211 -0
- package/src/ansi-quantone-cli.ts +259 -0
|
@@ -0,0 +1,259 @@
|
|
|
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
|
+
|
|
8
|
+
import os from "node:os"
|
|
9
|
+
import path from "node:path"
|
|
10
|
+
import fs from "node:fs"
|
|
11
|
+
import process from "node:process"
|
|
12
|
+
import { spawn } from "node:child_process"
|
|
13
|
+
|
|
14
|
+
import yargs from "yargs"
|
|
15
|
+
import { hideBin } from "yargs/helpers"
|
|
16
|
+
import YAML from "yaml"
|
|
17
|
+
import { run, type Config, type QuantoneOptions, type QuantoneColor } from "./ansi-quantone-api.js"
|
|
18
|
+
|
|
19
|
+
/* ==== config file parsing ==== */
|
|
20
|
+
|
|
21
|
+
/* the palette options and whether their value is a color (or else a number) */
|
|
22
|
+
const optionKeys: Record<keyof QuantoneOptions, boolean> = {
|
|
23
|
+
neutral: true,
|
|
24
|
+
accent: true,
|
|
25
|
+
shades: false,
|
|
26
|
+
chromatic: false,
|
|
27
|
+
floor: false,
|
|
28
|
+
ceiling: false,
|
|
29
|
+
threshold: false,
|
|
30
|
+
shift: false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* parse the YAML config file text into the configuration of a profile, by merging
|
|
34
|
+
its profile sections "default", "*", and the profile (if existing), in this order
|
|
35
|
+
(where later options override earlier ones and color mappings are accumulated) */
|
|
36
|
+
const parseConfig = (text: string, profile: string): Config => {
|
|
37
|
+
const config: unknown = YAML.parse(text) ?? {}
|
|
38
|
+
if (typeof config !== "object" || Array.isArray(config))
|
|
39
|
+
throw new Error("expected a mapping of profile sections")
|
|
40
|
+
const sections = config as Record<string, unknown>
|
|
41
|
+
for (const [ name, section ] of Object.entries(sections))
|
|
42
|
+
if (section !== null && (typeof section !== "object" || Array.isArray(section)))
|
|
43
|
+
throw new Error(`invalid profile section "${name}" (expected mapping of options)`)
|
|
44
|
+
const result: Config = { palette: {}, foreground: "auto", background: "none", reduce: false, mappings: [], debug: false }
|
|
45
|
+
const options: Record<string, QuantoneColor> = {}
|
|
46
|
+
for (const name of new Set([ "default", "*", profile ])) {
|
|
47
|
+
const section = (Object.hasOwn(sections, name) ? sections[name] : null) ?? {}
|
|
48
|
+
for (const [ key, val ] of Object.entries(section as Record<string, unknown>)) {
|
|
49
|
+
if (key === "foreground" || key === "background") {
|
|
50
|
+
if (!(typeof val === "number" || typeof val === "string"))
|
|
51
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected color, "auto", or "none")`)
|
|
52
|
+
result[key] = val
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
if (key === "reduce") {
|
|
56
|
+
if (typeof val !== "boolean")
|
|
57
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected boolean)`)
|
|
58
|
+
result.reduce = val
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
if (key === "mappings") {
|
|
62
|
+
if (!(Array.isArray(val) && val.every((v) => typeof v === "string")))
|
|
63
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected list of color mappings)`)
|
|
64
|
+
result.mappings.push(...val)
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
if (!Object.hasOwn(optionKeys, key))
|
|
68
|
+
throw new Error(`unknown option "${key}" in section "${name}"`)
|
|
69
|
+
const isColor = optionKeys[key as keyof QuantoneOptions]
|
|
70
|
+
if (!(typeof val === "number" || (isColor && typeof val === "string")))
|
|
71
|
+
throw new Error(`invalid value of option "${key}" in section "${name}" (expected ${isColor ? "color" : "number"})`)
|
|
72
|
+
options[key] = val
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
result.palette = options as QuantoneOptions
|
|
76
|
+
return result
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* write a fatal error message and exit */
|
|
80
|
+
const fatalError = (context: string, err?: unknown): never => {
|
|
81
|
+
if (err) {
|
|
82
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
83
|
+
process.stderr.write(`ansi-quantone: ERROR: ${context}: ${msg}\n`)
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
process.stderr.write(`ansi-quantone: ERROR: ${context}\n`)
|
|
87
|
+
process.exit(1)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* ==== CLI entry point ==== */
|
|
91
|
+
|
|
92
|
+
const main = async (): Promise<void> => {
|
|
93
|
+
/* parse command-line arguments */
|
|
94
|
+
const argv = await yargs(hideBin(process.argv))
|
|
95
|
+
.usage("Usage: $0 " +
|
|
96
|
+
"[-C|--config <config-file>] " +
|
|
97
|
+
"[-p|--profile <name>] " +
|
|
98
|
+
"[-n|--neutral <color>] " +
|
|
99
|
+
"[-a|--accent <color>] " +
|
|
100
|
+
"[-s|--shades <count>] " +
|
|
101
|
+
"[-c|--chromatic <count>] " +
|
|
102
|
+
"[-l|--floor <percent>] " +
|
|
103
|
+
"[-u|--ceiling <percent>] " +
|
|
104
|
+
"[-t|--threshold <chroma>] " +
|
|
105
|
+
"[-S|--shift <degrees>] " +
|
|
106
|
+
"[-F|--foreground <color>|auto|none] " +
|
|
107
|
+
"[-B|--background <color>|auto|none] " +
|
|
108
|
+
"[-r|--reduce] " +
|
|
109
|
+
"[-m|--map <fg>/<bg>:<fg>/<bg>[,...]] " +
|
|
110
|
+
"[-d|--debug] " +
|
|
111
|
+
"[--] " +
|
|
112
|
+
"<command> [<args>...]"
|
|
113
|
+
)
|
|
114
|
+
.option("C", {
|
|
115
|
+
alias: "config",
|
|
116
|
+
type: "string",
|
|
117
|
+
description: "path to config file"
|
|
118
|
+
})
|
|
119
|
+
.option("p", {
|
|
120
|
+
alias: "profile",
|
|
121
|
+
type: "string",
|
|
122
|
+
default: "default",
|
|
123
|
+
description: "name of config profile section (merged after \"default\" and \"*\")"
|
|
124
|
+
})
|
|
125
|
+
.option("n", {
|
|
126
|
+
alias: "neutral",
|
|
127
|
+
type: "string",
|
|
128
|
+
description: "base color of the neutral ramp"
|
|
129
|
+
})
|
|
130
|
+
.option("a", {
|
|
131
|
+
alias: "accent",
|
|
132
|
+
type: "string",
|
|
133
|
+
description: "base color of the chromatic ramps"
|
|
134
|
+
})
|
|
135
|
+
.option("s", {
|
|
136
|
+
alias: "shades",
|
|
137
|
+
type: "number",
|
|
138
|
+
description: "number of shades per ramp"
|
|
139
|
+
})
|
|
140
|
+
.option("c", {
|
|
141
|
+
alias: "chromatic",
|
|
142
|
+
type: "number",
|
|
143
|
+
description: "number of chromatic hue segments"
|
|
144
|
+
})
|
|
145
|
+
.option("l", {
|
|
146
|
+
alias: "floor",
|
|
147
|
+
type: "number",
|
|
148
|
+
description: "OKLCH lightness floor of the chromatic ramps in percent"
|
|
149
|
+
})
|
|
150
|
+
.option("u", {
|
|
151
|
+
alias: "ceiling",
|
|
152
|
+
type: "number",
|
|
153
|
+
description: "OKLCH lightness inset below white of the chromatic ramps in percent"
|
|
154
|
+
})
|
|
155
|
+
.option("t", {
|
|
156
|
+
alias: "threshold",
|
|
157
|
+
type: "number",
|
|
158
|
+
description: "OKLCH chroma below which a color maps onto the neutral ramp"
|
|
159
|
+
})
|
|
160
|
+
.option("S", {
|
|
161
|
+
alias: "shift",
|
|
162
|
+
type: "number",
|
|
163
|
+
description: "OKLCH hue rotation of the input colors in degrees"
|
|
164
|
+
})
|
|
165
|
+
.option("F", {
|
|
166
|
+
alias: "foreground",
|
|
167
|
+
type: "string",
|
|
168
|
+
description: "input color of the terminal default foreground (or \"auto\" or \"none\")"
|
|
169
|
+
})
|
|
170
|
+
.option("B", {
|
|
171
|
+
alias: "background",
|
|
172
|
+
type: "string",
|
|
173
|
+
description: "input color of the terminal default background (or \"auto\" or \"none\")"
|
|
174
|
+
})
|
|
175
|
+
.option("r", {
|
|
176
|
+
alias: "reduce",
|
|
177
|
+
type: "boolean",
|
|
178
|
+
description: "reduce RGB/truecolor input colors onto the nearest ANSI color 0-255"
|
|
179
|
+
})
|
|
180
|
+
.option("m", {
|
|
181
|
+
alias: "map",
|
|
182
|
+
type: "string",
|
|
183
|
+
description: "color mapping(s) of input colors (repeatable or comma-separated)"
|
|
184
|
+
})
|
|
185
|
+
.option("d", {
|
|
186
|
+
alias: "debug",
|
|
187
|
+
type: "boolean",
|
|
188
|
+
default: false,
|
|
189
|
+
description: "enable debug mode (with debug overlay toggled by \"CTRL+a d\")"
|
|
190
|
+
})
|
|
191
|
+
.parserConfiguration({
|
|
192
|
+
"halt-at-non-option": true
|
|
193
|
+
})
|
|
194
|
+
.strict(true)
|
|
195
|
+
.help().alias("h", "help")
|
|
196
|
+
.version().alias("V", "version")
|
|
197
|
+
.parseAsync()
|
|
198
|
+
const configPath = argv.C ?? path.join(os.homedir(), ".ansi-quantone.yaml")
|
|
199
|
+
const appArgs = argv._
|
|
200
|
+
if (appArgs.length === 0)
|
|
201
|
+
fatalError("no command specified")
|
|
202
|
+
const app = String(appArgs[0])
|
|
203
|
+
const restArgs = appArgs.slice(1).map(String)
|
|
204
|
+
|
|
205
|
+
/* pass-through mode: without a TTY on stdout, run the application
|
|
206
|
+
directly, as recoloring through a PTY would break piped output */
|
|
207
|
+
if (!process.stdout.isTTY) {
|
|
208
|
+
const child = spawn(app, restArgs, { stdio: "inherit" })
|
|
209
|
+
for (const signal of [ "SIGTERM", "SIGHUP" ] as const)
|
|
210
|
+
process.on(signal, () => { child.kill(signal) })
|
|
211
|
+
child.on("error", (err) => {
|
|
212
|
+
fatalError(`failed to spawn command "${app}"`, err)
|
|
213
|
+
})
|
|
214
|
+
child.on("exit", (code, signal) => {
|
|
215
|
+
if (signal !== null)
|
|
216
|
+
process.kill(process.pid, signal)
|
|
217
|
+
else
|
|
218
|
+
process.exit(code ?? 1)
|
|
219
|
+
})
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/* read and parse the config file (an explicitly given one has to exist) */
|
|
224
|
+
let config: Config = { palette: {}, foreground: "auto", background: "none", reduce: false, mappings: [], debug: false }
|
|
225
|
+
if (argv.C !== undefined || fs.existsSync(configPath)) {
|
|
226
|
+
try {
|
|
227
|
+
config = parseConfig(fs.readFileSync(configPath, "utf-8"), argv.p)
|
|
228
|
+
}
|
|
229
|
+
catch (err: unknown) {
|
|
230
|
+
fatalError(`failed to read and parse config file "${configPath}"`, err)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const options = config.palette
|
|
234
|
+
|
|
235
|
+
/* let the command-line options override the config file options
|
|
236
|
+
(where the color mappings of the command-line, a repeatable option
|
|
237
|
+
with a comma-separated list each, are appended to the ones of the config file) */
|
|
238
|
+
if (argv.n !== undefined) options.neutral = argv.n
|
|
239
|
+
if (argv.a !== undefined) options.accent = argv.a
|
|
240
|
+
if (argv.s !== undefined) options.shades = argv.s
|
|
241
|
+
if (argv.c !== undefined) options.chromatic = argv.c
|
|
242
|
+
if (argv.l !== undefined) options.floor = argv.l
|
|
243
|
+
if (argv.u !== undefined) options.ceiling = argv.u
|
|
244
|
+
if (argv.t !== undefined) options.threshold = argv.t
|
|
245
|
+
if (argv.S !== undefined) options.shift = argv.S
|
|
246
|
+
if (argv.F !== undefined) config.foreground = argv.F
|
|
247
|
+
if (argv.B !== undefined) config.background = argv.B
|
|
248
|
+
if (argv.r !== undefined) config.reduce = argv.r
|
|
249
|
+
config.debug = argv.d
|
|
250
|
+
config.mappings.push(...([] as string[]).concat(argv.m ?? []).flatMap((m) => m.split(",")))
|
|
251
|
+
|
|
252
|
+
/* run the application with recoloring on the process stdio */
|
|
253
|
+
const exitCode = await run(config, app, restArgs)
|
|
254
|
+
process.exit(exitCode)
|
|
255
|
+
}
|
|
256
|
+
main().catch((err: unknown) => {
|
|
257
|
+
fatalError(err instanceof Error ? err.message : String(err))
|
|
258
|
+
})
|
|
259
|
+
|