@hncode/hncode 0.7.7 → 0.7.8

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/src/colors.js CHANGED
@@ -1,556 +1,586 @@
1
- // Cyan-blue visual theme for hncode. Adapts to the terminal's color depth:
2
- // uses true-color when available, 256/16-color fallbacks otherwise.
3
-
4
- function colorDepth() {
5
- try { return process.stdout.getColorDepth?.() || 0; } catch { return 0; }
6
- }
7
- const DEPTH = colorDepth();
8
- export const TRUECOLOR = DEPTH >= 24 || process.env.COLORTERM === 'truecolor' || /^xterm.*-256color$|.*-truecolor$/.test(process.env.TERM || '');
9
-
10
- function rgb(r, g, b) { return TRUECOLOR ? `\x1b[38;2;${r};${g};${b}m` : `\x1b[38;5;${ansi256(r, g, b)}m`; }
11
- function brgb(r, g, b) { return TRUECOLOR ? `\x1b[48;2;${r};${g};${b}m` : `\x1b[48;5;${ansi256(r, g, b)}m`; }
12
-
13
- // Quantize a truecolor triple to the nearest 256-colour xterm index.
14
- //
15
- // Two different palettes live in xterm-256 and they must not be confused:
16
- // * the 6x6x6 COLOUR CUBE (16..231): each channel has 6 levels — 0, then
17
- // 95,135,175,215,255 — so the step is 40 above a floor of 35, not 51;
18
- // * the GREY RAMP (232..255): 24 levels from rgb(8) to rgb(238), steps of 10.
19
- //
20
- // The grey case used to be `232 + Math.round(v / 51)`, which collapses a whole range of
21
- // greys onto a few ramp entries: rgb(46,46,46) — the panel surface — quantised to 233,
22
- // i.e. rgb(18,18,18), three steps darker than asked for. The panel then read as a
23
- // near-black hole instead of a raised surface. Quantising against the ramp's own step
24
- // fixes that.
25
- function ansi256(r, g, b) {
26
- const cube = (v) => (v < 48 ? 0 : v < 115 ? 1 : Math.min(5, Math.round((v - 35) / 40)));
27
- // A triple that is (nearly) neutral belongs on the grey ramp, which has finer steps
28
- // than the cube's 6 levels: rgb(46,46,46) is not representable in the cube at all.
29
- const max = Math.max(r, g, b);
30
- const min = Math.min(r, g, b);
31
- if (max - min <= 8) {
32
- const v = (r + g + b) / 3;
33
- const n = Math.max(0, Math.min(23, Math.round((v - 8) / 10)));
34
- return 232 + n;
35
- }
36
- return 16 + 36 * cube(r) + 6 * cube(g) + cube(b);
37
- }
38
-
39
- // Brand accent: cyan-blue.
40
- // A BACKGROUND escape at the terminal's own colour depth: the exact triple under
41
- // truecolor, the nearest palette index otherwise.
42
- //
43
- // Exported because a caller computing a NEW colour at paint time — the backdrop dimming
44
- // does, when it pulls a fill toward the page — has to state its result in the same notation
45
- // the untouched fills use. Emitting `48;2;…` unconditionally loses the fill entirely on a
46
- // 256-colour terminal, which silently ignores what it does not implement.
47
- export const bgRgb = brgb;
48
-
49
- export const TEAL = rgb(0, 184, 219);
50
- export const CYAN = rgb(0, 215, 255);
51
- export const BLUE = rgb(66, 135, 245);
52
- export const FG = TEAL; // primary fg
53
- export const BG = rgb(0, 18, 26);
54
- export const BORDER = rgb(70, 130, 180);
55
-
56
- // Named accent colours per theme. `auto` keeps the brand cyan-blue and lets the
57
- // terminal's own palette show through for everything else; `dark`/`light` pick
58
- // accent triples that stay readable on that background.
59
- const THEMES = {
60
- // `auto` is deliberately NOT a table here: it is a CHOICE, resolved by `setTheme` into
61
- // `light` or `dark` from the terminal's background. A table named `auto` shadows that,
62
- // and did — it was a fixed dark palette with brand-cyan text, while its own label claimed
63
- // it followed the terminal.
64
- dark: {
65
- fg: [255, 255, 255], bg: [0, 18, 26], // White foreground for normal text
66
- fg: [255, 255, 255], bg: [0, 18, 26], // White foreground for normal text
67
- teal: [0, 184, 219], cyan: [0, 215, 255], blue: [66, 135, 245], border: [70, 130, 180],
68
- // The semantic colours. They were missing, and `setTheme` guards each assignment, so
69
- // they kept the previous theme's values — a theme that only looks right after a
70
- // particular one was applied.
71
- red: [230, 80, 80], green: [120, 200, 120], yellow: [230, 200, 100], orange: [240, 160, 80],
72
- white: [255, 255, 255], gray: [130, 140, 150],
73
- hover: [190, 245, 255], selBg: [0, 70, 84],
74
- surface: [46, 46, 46],
75
- },
76
- light: {
77
- fg: [26, 26, 26], bg: [255, 255, 255],
78
- teal: [0, 102, 128], cyan: [0, 122, 153], blue: [10, 77, 158], border: [138, 155, 176],
79
- hover: [0, 78, 102], selBg: [200, 230, 239],
80
- red: [179, 38, 30], green: [30, 122, 60], yellow: [160, 120, 0], orange: [178, 90, 0],
81
- white: [26, 26, 26], gray: [85, 85, 85],
82
- // A LIGHT surface is a grey that is darker than the white page but far lighter than
83
- // the dark themes' — a panel has to read as raised in whichever direction the theme
84
- // goes, and reusing #2e2e2e on white would be a black box.
85
- surface: [232, 232, 232],
86
- },
87
- // The four below are the same SHAPE as `dark` with the accent hue moved, so every
88
- // derived colour in `setTheme` (the scrollbar shades, hover, and the light/dark branch
89
- // test) keeps working with no special case. Each keeps ONE accent family varying only in
90
- // lightness, because the context bar's five fills read as a gradient and a second hue in
91
- // that ramp makes the bar look broken.
92
- nord: {
93
- fg: [216, 222, 233], bg: [46, 52, 64],
94
- teal: [136, 192, 208], cyan: [143, 188, 187], blue: [129, 161, 193], border: [94, 129, 172],
95
- hover: [236, 239, 244], selBg: [59, 66, 82],
96
- red: [191, 97, 106], green: [163, 190, 140], yellow: [235, 203, 139], orange: [208, 135, 112],
97
- white: [236, 239, 244], gray: [129, 135, 148],
98
- surface: [59, 66, 82],
99
- },
100
- dracula: {
101
- fg: [248, 248, 242], bg: [40, 42, 54],
102
- teal: [139, 233, 253], cyan: [139, 233, 253], blue: [98, 114, 164], border: [98, 114, 164],
103
- hover: [255, 121, 198], selBg: [68, 71, 90],
104
- red: [255, 85, 85], green: [80, 250, 123], yellow: [241, 250, 140], orange: [255, 184, 108],
105
- white: [248, 248, 242], gray: [98, 114, 164],
106
- surface: [68, 71, 90],
107
- },
108
- gruvbox: {
109
- fg: [235, 219, 178], bg: [40, 40, 40],
110
- teal: [142, 192, 124], cyan: [131, 165, 152], blue: [69, 133, 136], border: [146, 131, 116],
111
- hover: [250, 189, 47], selBg: [60, 56, 54],
112
- red: [251, 73, 52], green: [184, 187, 38], yellow: [250, 189, 47], orange: [254, 128, 25],
113
- white: [235, 219, 178], gray: [146, 131, 116],
114
- surface: [60, 56, 54],
115
- },
116
- mono: {
117
- // No hue at all: every accent is a grey, so the UI is readable on a monochrome
118
- // terminal or for anyone who needs colour off without losing the structure that
119
- // colour was carrying. The lightness steps are what keep the context bar legible.
120
- fg: [230, 230, 230], bg: [18, 18, 18],
121
- teal: [190, 190, 190], cyan: [210, 210, 210], blue: [150, 150, 150], border: [110, 110, 110],
122
- hover: [255, 255, 255], selBg: [60, 60, 60],
123
- red: [200, 90, 90], green: [140, 190, 140], yellow: [200, 190, 120], orange: [210, 160, 110],
124
- white: [240, 240, 240], gray: [130, 130, 130],
125
- surface: [42, 42, 42],
126
- },
127
- catppuccin: {
128
- // Mocha: a warm-tinted dark with pastel accents. Every accent is desaturated toward
129
- // the background so long replies do not glare, which is the whole point of the palette.
130
- fg: [205, 214, 244], bg: [30, 30, 46],
131
- teal: [137, 180, 250], cyan: [137, 220, 235], blue: [137, 180, 250], border: [108, 112, 134],
132
- hover: [203, 166, 247], selBg: [49, 50, 68],
133
- red: [243, 139, 168], green: [166, 227, 161], yellow: [249, 226, 175], orange: [250, 179, 135],
134
- white: [205, 214, 244], gray: [127, 132, 156],
135
- surface: [49, 50, 68],
136
- },
137
- tokyo: {
138
- // Night: a blue-black with high-contrast accents. Distinct from nord, which is greyed
139
- // out; this keeps the blue in the background and lets the cyan carry the structure.
140
- fg: [192, 202, 245], bg: [26, 27, 38],
141
- teal: [125, 207, 255], cyan: [125, 207, 255], blue: [122, 162, 247], border: [86, 95, 137],
142
- hover: [187, 154, 247], selBg: [41, 46, 66],
143
- red: [247, 118, 142], green: [158, 206, 106], yellow: [224, 175, 104], orange: [255, 158, 100],
144
- white: [192, 202, 245], gray: [86, 95, 137],
145
- surface: [41, 46, 66],
146
- },
147
- solar: {
148
- // Solarized LIGHT. Included because `light` is a plain neutral and solarized's even
149
- // hue ramp is the best-tested light palette in the terminal world. The dark variant is
150
- // `solar-dark`, so both exist; the light one is the one worth having.
151
- fg: [101, 123, 131], bg: [253, 246, 227],
152
- teal: [38, 139, 210], cyan: [38, 139, 210], blue: [108, 113, 196], border: [147, 161, 161],
153
- hover: [211, 54, 130], selBg: [238, 232, 213],
154
- red: [220, 50, 47], green: [133, 153, 0], yellow: [181, 137, 0], orange: [203, 75, 22],
155
- white: [253, 246, 227], gray: [147, 161, 161],
156
- surface: [238, 232, 213],
157
- },
158
- rose: {
159
- // Pine: a muted mauve dark, between dracula's saturation and nord's restraint.
160
- fg: [224, 222, 244], bg: [25, 23, 36],
161
- teal: [196, 167, 231], cyan: [137, 180, 250], blue: [137, 180, 250], border: [110, 106, 134],
162
- hover: [196, 167, 231], selBg: [31, 29, 46],
163
- red: [215, 130, 126], green: [156, 207, 216], yellow: [234, 157, 173], orange: [235, 188, 186],
164
- white: [224, 222, 244], gray: [110, 106, 134],
165
- surface: [31, 29, 46],
166
- },
167
- };
168
- /** Theme name -> one-line description, for the /theme picker. */
169
- export const THEME_LABELS = {
170
- auto: 'follow the terminal: light theme on a light background, dark otherwise',
171
- dark: 'dark teal background, white text',
172
- light: 'light background, darkened accents',
173
- nord: 'cool blue-grey',
174
- dracula: 'high-contrast purple and pink',
175
- gruvbox: 'warm retro earth tones',
176
- catppuccin: 'soft pastel, warm-tinted dark',
177
- tokyo: 'blue-black, high-contrast accents',
178
- solar: 'even hue ramp, light background',
179
- rose: 'muted mauve dark',
180
- mono: 'greyscale, no hue',
181
- };
182
-
183
- /** `auto` is a name you can set without being a palette, so it is listed separately from
184
- * the tables — see `setTheme` for how it resolves. */
185
- export const AUTO_THEME = 'auto';
186
-
187
- /** Whether a theme name is one we know. */
188
- export function hasTheme(name) {
189
- return name === AUTO_THEME || Object.prototype.hasOwnProperty.call(THEMES, name);
190
- }
191
-
192
- /** The names a user may choose, `auto` first because it is the default. */
193
- export const THEME_NAMES = [AUTO_THEME, ...Object.keys(THEMES)];
194
-
195
- /** True when a theme paints on a light background. Resolves `auto` first, so a caller
196
- * asking about the CURRENT theme gets the answer for what is actually on screen. */
197
- export function isLightTheme(name) {
198
- const resolved = name === AUTO_THEME ? (autoIsLight ? 'light' : 'dark') : name;
199
- const t = THEMES[resolved];
200
- return !!(t && Array.isArray(t.bg) && t.bg[0] > 128);
201
- }
202
-
203
- export const C = {
204
- reset: '\x1b[0m',
205
- bold: '\x1b[1m',
206
- dim: '\x1b[2m',
207
- // Italic (SGR 3). Used for the BODY of a reasoning block — the prose reads as
208
- // an aside rather than as the answer. Must be closed with SGR 23 (`endItalic`),
209
- // NOT with `reset`, because resetting also drops the surrounding dim/gray that
210
- // the same row depends on. Terminals without italic fall back to normal text,
211
- // so this is a decoration, never load-bearing.
212
- italic: '\x1b[3m',
213
- endItalic: '\x1b[23m',
214
- muted: '\x1b[90m',
215
- teal: TEAL,
216
- cyan: CYAN,
217
- // The mark's shadow: the same cyan at 40% brightness. `dim` (SGR 2) is applied by the TERMINAL and most render
218
- // it barely darker; `teal` is only ~15% off `cyan`. Neither reads as a shadow behind the letters.
219
- cyanDeep: rgb(0, 86, 102),
220
- // The startup heading: the grey the annotations use, lifted towards white. The wordmark above it owns the cyan,
221
- // so the name under it is a lighter neutral rather than a second colour competing with the mark.
222
- heading: rgb(200, 210, 220),
223
- // DIFF COLOURS ARE DERIVED FROM THE THEME, not fixed.
224
- //
225
- // A diff paints two layers: the whole changed LINE gets a wash, and the words that actually differ get a
226
- // stronger one. Those have to sit on THIS theme's background — the Islands Dark tints read as a warm grey on
227
- // gruvbox and as nothing at all on `light`. `setTheme` recomputes all four from the theme's own `green`, `red`
228
- // and `bg`, so switching themes recolours the diff with everything else. The values here are the dark defaults,
229
- // used before any theme is applied.
230
- diffAddBg: brgb(34, 58, 38),
231
- diffAddWordBg: brgb(52, 96, 58),
232
- diffDelBg: brgb(74, 32, 34),
233
- diffDelWordBg: brgb(112, 44, 48),
234
- diffAddFg: rgb(133, 208, 143),
235
- diffDelFg: rgb(255, 176, 168),
236
- // A code comment. `setTheme` mixes it from the palette's foreground towards its background, so it is quiet without
237
- // disappearing — `C.gray` is tuned for the page and vanishes on a diff wash in several themes.
238
- comment: '\x1b[90m',
239
- fg: FG,
240
- bg: BG,
241
- bgTeal: brgb(0, 184, 219),
242
- bgDim: brgb(12, 24, 30),
243
- // Fills for the segmented context bar: five blues in one family, deepening towards
244
- // black, plus a neutral free segment. Background colours, because the bar is read by
245
- // FILL rather than by any text — a segment is usually only a few columns wide, so a
246
- // letter inside it would be unreadable noise.
247
- //
248
- // The steps are deliberately narrow. Widening them makes the bar easier to tell apart
249
- // at a glance but harder to keep a dark terminal readable, and the darkest member has
250
- // to stay distinguishable from the page background it sits on.
251
- ctxSystem: brgb(35, 48, 95), // deepest navy
252
- ctxPrompt: brgb(43, 61, 120), // navy
253
- ctxAssistant: brgb(52, 74, 146), // indigo
254
- ctxThinking: brgb(77, 107, 254), // brand blue, the brightest used segment
255
- ctxTools: brgb(90, 124, 255), // light blue
256
- ctxFree: brgb(46, 46, 46), // neutral grey: "nothing here yet"
257
- // The SURFACE colour for a floating panel (picker menus, dialogs). A NEUTRAL GREY,
258
- // deliberately separate from the theme's `bg`:
259
- //
260
- // * the theme background is `[0, 18, 26]` — a dark TEAL. A raised surface should be
261
- // neutral in every theme; only the ACCENTS carry the brand hue. Reusing `bg` for
262
- // the panel made every popup read blue, which is not what a panel is.
263
- // * `applyTheme` used to overwrite this with `t.bg`, so the value here only applied
264
- // before a theme was set. It is no longer touched by the theme.
265
- //
266
- // xterm-256's grey ramp runs 232..255 from rgb(8) to rgb(238) in steps of 10, so 236
267
- // is rgb(48,48,48) = #303030 — clearly lifted off a near-black terminal without
268
- // washing the text out. Truecolor gets `#2e2e2e`, the same shade written exactly.
269
- bgPanel: TRUECOLOR ? '\x1b[48;2;46;46;46m' : '\x1b[48;5;236m',
270
- // The code-surface background, set by setTheme from the theme's own page colour. A placeholder until then, so
271
- // code drawn before the first setTheme still has a surface.
272
- codeBg: TRUECOLOR ? '\x1b[48;2;12;20;26m' : '\x1b[48;5;233m',
273
- // The terminal-surface background, set by setTheme from the theme's page colour. Placeholder until then.
274
- termBg: TRUECOLOR ? '\x1b[48;2;0;14;21m' : '\x1b[48;5;234m',
275
- // The composer's neutral-grey surface. Placeholder until setTheme runs.
276
- composerBg: TRUECOLOR ? '\x1b[48;2;44;44;46m' : '\x1b[48;5;237m',
277
- border: BORDER,
278
- red: '\x1b[91m',
279
- green: '\x1b[92m',
280
- yellow: '\x1b[93m',
281
- magenta: '\x1b[95m',
282
- gray: '\x1b[90m',
283
- white: '\x1b[97m',
284
- orange: TRUECOLOR ? rgb(255, 140, 0) : '\x1b[38;5;208m',
285
- // Cyan pushed slightly toward green — used for the Swarm mode badge so it reads
286
- // as its own mode next to Plan (pure cyan) and Focus (blue) while staying in the
287
- // theme's blue-green family. 38;5;43 is rgb(0,215,175): red at zero and green
288
- // only 40 above blue, i.e. still clearly cyan with a green cast. 38;5;85 was the
289
- // first attempt and read as plain GREEN — its red channel is lifted to 95 and
290
- // green pins at 255, which is a spring green, not a green-tinted cyan.
291
- spring: TRUECOLOR ? rgb(0, 215, 175) : '\x1b[38;5;43m',
292
- // Git branch in the status line: yellow pulled about half way toward white, so
293
- // it stays clearly "yellow" (not cream/ivory) while reading brighter than the
294
- // plain 93 yellow on a dark background. rgb(255,219,110) keeps red at max,
295
- // green near it, and blue around 43% — that ratio is what keeps the hue
296
- // yellow rather than dropping into orange.
297
- branch: TRUECOLOR ? rgb(255, 219, 110) : '\x1b[38;5;222m',
298
- // Shell-mode (`!`) accent — violet, matching kimi-code's shellMode token so the
299
- // bash-mode editor border and the `!` read as "shell", distinct from the cyan
300
- // prompt and the yellow git badge. #BD93F9 dark / #7C3AED light.
301
- shellMode: TRUECOLOR ? rgb(189, 147, 249) : '\x1b[38;5;141m',
302
- bgRed: '\x1b[41m',
303
- bgGreen: '\x1b[42m',
304
- // Selection highlight (mouse text selection) and the scrollbar gutter.
305
- // Selection uses a dark teal background so white text stays readable.
306
- selBg: TRUECOLOR ? brgb(0, 70, 84) : '\x1b[48;5;23m', // Original dark cyan background
307
- // Reset ONLY the background colour, keeping the current foreground. Used to end
308
- // a selection highlight so text after it keeps its own colour (a bare ESC[0m
309
- // would also reset the foreground, turning a cyan/white run grey).
310
- bgReset: '\x1b[49m',
311
- // Scrollbar, styled after OpenTUI (what cline uses): a solid block thumb on a
312
- // near-black track. Only the FOREGROUND colour is stateful — the track is always the
313
- // background — because that is how OpenTUI's SliderRenderable paints a cell:
314
- // foreground = thumb, background = track. A `█` covers both columns with the
315
- // foreground, while `▀`/`▄` expose one half to the track, so the two colours must
316
- // stay DISTINCT; giving a half-cell the thumb colour as its background is what made
317
- // it read as a second square, and it also survived the modal backdrop's faint
318
- // attribute, which fades the foreground only.
319
- // track #252527 thumb #9a9ea3 (OpenTUI's defaults, inlined)
320
- // hover #d2d6da active #ffffff (brighter thumb, not a new hue)
321
- scrollTrackBg: TRUECOLOR ? brgb(0x25, 0x25, 0x27) : '\x1b[48;5;235m',
322
- scrollTrackFg: TRUECOLOR ? rgb(0x25, 0x25, 0x27) : '\x1b[38;5;235m',
323
- scrollThumbBg: TRUECOLOR ? brgb(0x9a, 0x9e, 0xa3) : '\x1b[48;5;246m',
324
- scrollThumbFg: TRUECOLOR ? rgb(0x9a, 0x9e, 0xa3) : '\x1b[38;5;246m',
325
- scrollThumbHoverBg: TRUECOLOR ? brgb(0xd2, 0xd6, 0xda) : '\x1b[48;5;252m',
326
- scrollThumbHoverFg: TRUECOLOR ? rgb(0xd2, 0xd6, 0xda) : '\x1b[38;5;252m',
327
- scrollThumbActiveBg: TRUECOLOR ? brgb(0xff, 0xff, 0xff) : '\x1b[48;5;231m',
328
- scrollThumbActiveFg: TRUECOLOR ? rgb(0xff, 0xff, 0xff) : '\x1b[38;5;231m',
329
- // FOREGROUND aliases kept for the todo/queue resize rule, which draws a `─` line
330
- // and therefore needs a foreground, not the block's background pair.
331
- scrollThumbActive: TRUECOLOR ? rgb(0xff, 0xff, 0xff) : '\x1b[38;5;231m',
332
- scrollThumbHover: TRUECOLOR ? rgb(0xd2, 0xd6, 0xda) : '\x1b[38;5;252m',
333
- // Legacy foreground forms, still referenced by the gutter-hover path.
334
- scrollTrack: '\x1b[90m',
335
- scrollThumb: TRUECOLOR ? rgb(0, 184, 219) : '\x1b[38;5;37m',
336
- // Hover highlight for clickable rows (menu items, picker options, form
337
- // fields, composer rows). An explicit bright foreground + BOLD rather than a
338
- // bare BOLD: BOLD only brightens the 16-colour palette (it does nothing to a
339
- // true-color row), which is why hover used to be invisible on most rows.
340
- // tintRange() re-applies this so the row's own SGR codes cannot cancel it.
341
- hover: '\x1b[1m' + rgb(190, 245, 255),
342
- };
343
-
344
- export function style(text, ...codes) {
345
- return codes.join('') + text + '\x1b[0m';
346
- }
347
-
348
- // Linearly interpolate between two RGB triples and return an ANSI colour string.
349
- // Used for the pulsing Working indicator (orange pulses to a brighter/darker
350
- // shade over a 2s cycle).
351
- export function lerpColor(r1, g1, b1, r2, g2, b2, t) {
352
- const ti = Math.max(0, Math.min(1, t));
353
- const r = Math.round(r1 + (r2 - r1) * ti);
354
- const g = Math.round(g1 + (g2 - g1) * ti);
355
- const b = Math.round(b1 + (b2 - b1) * ti);
356
- return rgb(r, g, b);
357
- }
358
-
359
- // Which concrete theme `auto` resolves to right now. `setTheme` keeps it so a repaint or a
360
- // `/theme auto` after a terminal probe lands on the same answer.
361
- let autoIsLight = false;
362
-
363
- // The theme `setTheme` last applied, resolved (so `auto` reports the concrete name it
364
- // became). Read by the render cache: a cached row holds finished escapes, so a palette
365
- // change has to invalidate them or the old colours survive the switch.
366
- let appliedTheme = '';
367
-
368
- /** The concrete theme in force, or `''` before the first `setTheme`. */
369
- export function currentTheme() { return appliedTheme; }
370
-
371
- /**
372
- * Tell `setTheme` whether the terminal has a light background.
373
- *
374
- * The probe lives in term-caps.js and this module is a leaf that imports nothing, so the
375
- * answer has to be handed in. Called once after the probe and again if the terminal is
376
- * resized on a different display.
377
- */
378
- export function setLightBackground(light) {
379
- autoIsLight = !!light;
380
- }
381
-
382
- // Apply a named theme IN PLACE. `C` is a module-level object and ESM imports are
383
- // live bindings, so mutating its properties here changes the colours everywhere
384
- // without re-importing. Returns true when the name was recognised.
385
- //
386
- // `auto` is a CHOICE, not a palette: it resolves to `light` on a light terminal and to
387
- // `dark` otherwise, and then applies that theme's colours. It used to be a fixed table —
388
- // a dark background with brand-cyan text — which is not what "auto" means and not what its
389
- // own label claimed ("on the terminal's own palette"). The user asking for auto is asking
390
- // not to have to think about it.
391
- export function setTheme(name) {
392
- const resolved = name === 'auto' ? (autoIsLight ? 'light' : 'dark') : name;
393
- const t = THEMES[resolved];
394
- if (!t) return false;
395
- appliedTheme = resolved;
396
-
397
-
398
-
399
- // Update foreground colors
400
- C.teal = rgb(...t.teal);
401
- C.fg = rgb(...t.fg);
402
- C.cyan = rgb(...t.cyan);
403
- C.blue = rgb(...t.blue);
404
- C.border = rgb(...t.border);
405
- // Update background colors
406
- C.bg = brgb(...t.bg);
407
- C.bgTeal = brgb(...t.teal);
408
- // The panel surface is NEUTRAL GREY and comes from the theme's own `surface` triple,
409
- // NOT from `bg`. It used to be set to `t.bg` (a dark teal), which made every popup
410
- // carry a blue cast; a raised surface must be neutral, with only the accents following
411
- // the brand hue. A light theme gets a light grey so the panel still reads as raised.
412
- if (t.surface) C.bgPanel = brgb(...t.surface);
413
- C.selBg = brgb(...t.selBg);
414
-
415
- // Scrollbar colours. The thumb is a BLOCK, so its colour goes on BOTH the
416
- // foreground and the background (a half-cell `▀`/`▄` shows the foreground, a full
417
- // `█` shows the background — they must match). Hover / drag are brighter shades
418
- // of the same hue, applied to the same pair, so the whole thumb changes together.
419
- const active = [
420
- Math.min(255, Math.round(t.teal[0] * 0.6 + 255 * 0.4)),
421
- Math.min(255, Math.round(t.teal[1] * 0.6 + 255 * 0.4)),
422
- Math.min(255, Math.round(t.teal[2] * 0.6 + 255 * 0.4)),
423
- ];
424
- const hover = t.hover || [190, 245, 255];
425
- C.scrollTrackFg = rgb(0x25, 0x25, 0x27);
426
- C.scrollTrackBg = brgb(0x25, 0x25, 0x27);
427
- C.scrollThumbFg = rgb(...t.teal);
428
- C.scrollThumbBg = brgb(...t.teal);
429
- C.scrollThumbHoverFg = rgb(...hover);
430
- C.scrollThumbHoverBg = brgb(...hover);
431
- C.scrollThumbActiveFg = rgb(...active);
432
- C.scrollThumbActiveBg = brgb(...active);
433
- // Foreground aliases for the todo/queue resize rule.
434
- C.scrollTrack = '\x1b[90m';
435
- C.scrollThumb = rgb(...t.teal);
436
- C.scrollThumbHover = rgb(...hover);
437
- C.scrollThumbActive = rgb(...active);
438
-
439
- // Hover stays visible on this theme's background (bright on dark, dark on light).
440
- C.hover = '\x1b[1m' + rgb(...(t.hover || [190, 245, 255]));
441
-
442
- // Semantic colors — swap to deep versions on light backgrounds.
443
- if (t.white) C.white = rgb(...t.white);
444
- if (t.gray) C.gray = rgb(...t.gray);
445
- if (t.red) C.red = rgb(...t.red);
446
- if (t.green) C.green = rgb(...t.green);
447
- if (t.yellow) C.yellow = rgb(...t.yellow);
448
- if (t.orange) C.orange = rgb(...t.orange);
449
- // The git branch colour is derived per theme: the pale yellow that reads well
450
- // on the dark background is nearly invisible on white, so on a light theme it
451
- // is darkened (keeping the same hue) instead of being left as-is.
452
- if (t.bg && t.bg[0] > 128) {
453
- C.branch = rgb(146, 106, 0); // dark amber for light backgrounds
454
- } else {
455
- C.branch = TRUECOLOR ? rgb(255, 219, 110) : '\x1b[38;5;222m';
456
- }
457
- // DIFF TINTS, mixed from this theme's own green/red towards its background.
458
- //
459
- // A line wash is a WEAK mix and a word wash a STRONGER one of the same pair: weak enough that ordinary text on it
460
- // still reads as text, strong enough that the block is visible without reading it. Mixing towards `t.bg` rather
461
- // than towards black is what makes a light theme work — a dark red wash on white is a black bar with letters in
462
- // it, while a pale red is a highlighter mark.
463
- const mix = (a, b, k) => a.map((v, i) => Math.round(v * k + b[i] * (1 - k)));
464
- const green = t.green || [120, 200, 120];
465
- const red = t.red || [230, 80, 80];
466
- // ABOUT HALF. Lower mixes disappear into a dark page — the wash has to be obvious without being read, and at
467
- // 16% an added block looked like ordinary text with a faint tint. Light backgrounds need slightly more still,
468
- // because the same distance in RGB is a smaller difference next to white.
469
- const isLight = t.bg && t.bg[0] > 128;
470
- const line = isLight ? 0.52 : 0.46;
471
- const word = isLight ? 0.68 : 0.62;
472
- C.diffAddBg = brgb(...mix(green, t.bg, line));
473
- C.diffAddWordBg = brgb(...mix(green, t.bg, word));
474
- C.diffDelBg = brgb(...mix(red, t.bg, line));
475
- C.diffDelWordBg = brgb(...mix(red, t.bg, word));
476
- // The sign and the text of a changed line are the same hue as its wash, pulled towards the theme's own foreground
477
- // so they read ON that wash — not the theme's plain green/red, which is tuned for the page background.
478
- C.diffAddFg = rgb(...mix(green, t.fg, 0.55));
479
- C.diffDelFg = rgb(...mix(red, t.fg, 0.55));
480
-
481
- // THE CODE-SURFACE BACKGROUND, one step off the page. Read rows, Write previews and diff CONTEXT lines sit on
482
- // it; diff `+`/`-` rows keep their green/red wash on top. Mixed from this theme's own page colour rather than
483
- // a fixed grey, so a light theme gets a light code surface and a dark one a dark one.
484
- //
485
- // The step depends on WHICH SIDE the page is. On a dark page the surface is one step LIGHTER (mix very little
486
- // foreground in); on a light page it is one step DARKER (mix a little foreground in). Same small distance either
487
- // way, because a code region must read as "same screen, slightly recessed", not as a panel.
488
- C.codeBg = brgb(...mix(t.bg, t.fg, isLight ? 0.93 : 0.94));
489
- // THE TERMINAL-SURFACE BACKGROUND: the same small step on the OTHER side of the page, so the ordering holds on
490
- // both kinds of theme — page < terminal < code on a dark one, terminal < code < page on a light one. Bash output
491
- // and the shell-mode echo sit on it: a command reads as "a terminal ran this" without becoming a black band,
492
- // which is what a deeper well looked like — a hole in the screen rather than a region of it.
493
- //
494
- // Towards BLACK on a dark theme and towards the foreground on a light one: mixing a dark page further towards its
495
- // own foreground would make it lighter, which is `codeBg`'s job, not this one.
496
- C.termBg = brgb(...mix(t.bg, isLight ? t.fg : [0, 0, 0], isLight ? 0.88 : 0.94));
497
- // THE COMPOSER SURFACE: a NEUTRAL GREY, not a tint of the page. The input area is the one region that should not
498
- // follow the theme's hue — a cyan-tinted box on a dark-teal page reads as "more page", while a grey one reads as
499
- // an input field. Neutral means equal RGB, so it is written as a grey and adjusted only for light vs dark. A
500
- // theme may override it with its own `composer` triple.
501
- C.composerBg = brgb(...(t.composer || (isLight ? [232, 232, 232] : [44, 44, 46])));
502
-
503
- // A comment: the palette's foreground, pulled most of the way towards its background so it reads as quiet. A dark
504
- // page can afford a looser mix; a light one cannot — solar's foreground is already a muted blue-grey, and at the
505
- // dark theme's ratio its comments came within 8 of the removal wash.
506
- C.comment = rgb(...mix(t.fg, t.bg, isLight ? 0.86 : 0.72));
507
-
508
- return true;
509
- }
510
-
511
- export function clearScreen() {
512
- return '\x1b[3J\x1b[H\x1b[2J';
513
- }
514
-
515
- // Clear screen and set background color based on current theme
516
- export function clearAndSetBg() {
517
- // Reset all attributes first
518
- const reset = '\x1b[0m';
519
- // Clear screen
520
- const clear = '\x1b[3J\x1b[H\x1b[2J';
521
- // Set background color using current C.bg value
522
- const bg = C.bg || '\x1b[48;2;0;18;26m'; // Default dark background
523
- return reset + clear + bg;
524
- }
525
- export function eraseLine() {
526
- return '\x1b[2K';
527
- }
528
- export function eraseScreen() {
529
- return '\x1b[2J';
530
- }
531
- export function moveTo(r, c) {
532
- return `\x1b[${r + 1};${c + 1}H`;
533
- }
534
- export function cursorUp(n) { return `\x1b[${n}A`; }
535
- export function cursorDown(n) { return `\x1b[${n}B`; }
536
- export function cursorLeft(n) { return `\x1b[${n}D`; }
537
- export function cursorForward(n) { return `\x1b[${n}C`; }
538
- export function hideCursor() { return '\x1b[?25l'; }
539
- export function showCursor() { return '\x1b[?25h'; }
540
- // Cursor shape: terminals accept DECSCUSR — 2 = block, 4 = underline, 6 = bar(I-beam);
541
- // 1/3/5 = blinking variants. Force a solid block cursor so positioning is always exact
542
- // and unaffected by I-beam alignment.
543
- export function blockCursor() { return '\x1b[2 q'; }
544
- export function underlineCursor() { return '\x1b[4 q'; }
545
- export function barCursor() { return '\x1b[6 q'; }
546
- export function alternateScreen(on) { return on ? '\x1b[?1049h' : '\x1b[?1049l'; }
547
- export function setScrollRegion(top, bottom) {
548
- if (process.platform === 'win32') return ''; // win TTY scroll regions are unreliable
549
- return `\x1b[${top + 1};${bottom + 1}r`;
550
- }
551
- export function resetScrollRegion() {
552
- return '\x1b[r';
553
- }
554
- export function setBracketed(on) {
555
- return `\x1b[?2004h${on ? '' : ''}`; // placeholder; real impl toggles bracketed paste
556
- }
1
+ // Cyan-blue visual theme for hncode. Adapts to the terminal's color depth:
2
+ // uses true-color when available, 256/16-color fallbacks otherwise.
3
+
4
+ function colorDepth() {
5
+ try { return process.stdout.getColorDepth?.() || 0; } catch { return 0; }
6
+ }
7
+ const DEPTH = colorDepth();
8
+ export const TRUECOLOR = DEPTH >= 24 || process.env.COLORTERM === 'truecolor' || /^xterm.*-256color$|.*-truecolor$/.test(process.env.TERM || '');
9
+
10
+ function rgb(r, g, b) { return TRUECOLOR ? `\x1b[38;2;${r};${g};${b}m` : `\x1b[38;5;${ansi256(r, g, b)}m`; }
11
+ function brgb(r, g, b) { return TRUECOLOR ? `\x1b[48;2;${r};${g};${b}m` : `\x1b[48;5;${ansi256(r, g, b)}m`; }
12
+
13
+ // Quantize a truecolor triple to the nearest 256-colour xterm index.
14
+ //
15
+ // Two different palettes live in xterm-256 and they must not be confused:
16
+ // * the 6x6x6 COLOUR CUBE (16..231): each channel has 6 levels — 0, then
17
+ // 95,135,175,215,255 — so the step is 40 above a floor of 35, not 51;
18
+ // * the GREY RAMP (232..255): 24 levels from rgb(8) to rgb(238), steps of 10.
19
+ //
20
+ // The grey case used to be `232 + Math.round(v / 51)`, which collapses a whole range of
21
+ // greys onto a few ramp entries: rgb(46,46,46) — the panel surface — quantised to 233,
22
+ // i.e. rgb(18,18,18), three steps darker than asked for. The panel then read as a
23
+ // near-black hole instead of a raised surface. Quantising against the ramp's own step
24
+ // fixes that.
25
+ function ansi256(r, g, b) {
26
+ const cube = (v) => (v < 48 ? 0 : v < 115 ? 1 : Math.min(5, Math.round((v - 35) / 40)));
27
+ // A triple that is (nearly) neutral belongs on the grey ramp, which has finer steps
28
+ // than the cube's 6 levels: rgb(46,46,46) is not representable in the cube at all.
29
+ const max = Math.max(r, g, b);
30
+ const min = Math.min(r, g, b);
31
+ if (max - min <= 8) {
32
+ const v = (r + g + b) / 3;
33
+ const n = Math.max(0, Math.min(23, Math.round((v - 8) / 10)));
34
+ return 232 + n;
35
+ }
36
+ return 16 + 36 * cube(r) + 6 * cube(g) + cube(b);
37
+ }
38
+
39
+ // Brand accent: cyan-blue.
40
+ // A BACKGROUND escape at the terminal's own colour depth: the exact triple under
41
+ // truecolor, the nearest palette index otherwise.
42
+ //
43
+ // Exported because a caller computing a NEW colour at paint time — the backdrop dimming
44
+ // does, when it pulls a fill toward the page — has to state its result in the same notation
45
+ // the untouched fills use. Emitting `48;2;…` unconditionally loses the fill entirely on a
46
+ // 256-colour terminal, which silently ignores what it does not implement.
47
+ export const bgRgb = brgb;
48
+
49
+ export const TEAL = rgb(0, 184, 219);
50
+ export const CYAN = rgb(0, 215, 255);
51
+ export const BLUE = rgb(66, 135, 245);
52
+ export const FG = TEAL; // primary fg
53
+ // `brgb`, not `rgb`: this constant is used as a BACKGROUND (`C.bg`), and a foreground sequence in a background
54
+ // slot is silently ignored — the cell keeps whatever background was already in force. It only ever looked correct
55
+ // because `setTheme` overwrites `C.bg` on startup, so the wrong escape survived in the initial value where nothing
56
+ // exercised it.
57
+ export const BG = brgb(0, 18, 26);
58
+ export const BORDER = rgb(70, 130, 180);
59
+
60
+ // Named accent colours per theme. `auto` keeps the brand cyan-blue and lets the
61
+ // terminal's own palette show through for everything else; `dark`/`light` pick
62
+ // accent triples that stay readable on that background.
63
+ const THEMES = {
64
+ // `auto` is deliberately NOT a table here: it is a CHOICE, resolved by `setTheme` into
65
+ // `light` or `dark` from the terminal's background. A table named `auto` shadows that,
66
+ // and did — it was a fixed dark palette with brand-cyan text, while its own label claimed
67
+ // it followed the terminal.
68
+ dark: {
69
+ fg: [255, 255, 255], bg: [0, 18, 26], // White foreground for normal text
70
+ fg: [255, 255, 255], bg: [0, 18, 26], // White foreground for normal text
71
+ teal: [0, 184, 219], cyan: [0, 215, 255], blue: [66, 135, 245], border: [70, 130, 180],
72
+ // The semantic colours. They were missing, and `setTheme` guards each assignment, so
73
+ // they kept the previous theme's values — a theme that only looks right after a
74
+ // particular one was applied.
75
+ red: [230, 80, 80], green: [120, 200, 120], yellow: [230, 200, 100], orange: [240, 160, 80],
76
+ white: [255, 255, 255], gray: [130, 140, 150],
77
+ hover: [190, 245, 255], selBg: [0, 70, 84],
78
+ surface: [46, 46, 46],
79
+ },
80
+ light: {
81
+ fg: [26, 26, 26], bg: [250, 250, 250],
82
+ teal: [0, 102, 128], cyan: [0, 122, 153], blue: [10, 77, 158], border: [138, 155, 176],
83
+ hover: [0, 78, 102], selBg: [200, 230, 239],
84
+ red: [179, 38, 30], green: [30, 122, 60], yellow: [160, 120, 0], orange: [178, 90, 0],
85
+ white: [26, 26, 26], gray: [85, 85, 85],
86
+ // A LIGHT surface is a grey that is darker than the white page but far lighter than
87
+ // the dark themes' — a panel has to read as raised in whichever direction the theme
88
+ // goes, and reusing #2e2e2e on white would be a black box.
89
+ surface: [232, 232, 232],
90
+ },
91
+ // The four below are the same SHAPE as `dark` with the accent hue moved, so every
92
+ // derived colour in `setTheme` (the scrollbar shades, hover, and the light/dark branch
93
+ // test) keeps working with no special case. Each keeps ONE accent family varying only in
94
+ // lightness, because the context bar's five fills read as a gradient and a second hue in
95
+ // that ramp makes the bar look broken.
96
+ nord: {
97
+ fg: [216, 222, 233], bg: [46, 52, 64],
98
+ teal: [136, 192, 208], cyan: [143, 188, 187], blue: [129, 161, 193], border: [94, 129, 172],
99
+ hover: [236, 239, 244], selBg: [59, 66, 82],
100
+ red: [191, 97, 106], green: [163, 190, 140], yellow: [235, 203, 139], orange: [208, 135, 112],
101
+ white: [236, 239, 244], gray: [129, 135, 148],
102
+ surface: [59, 66, 82],
103
+ },
104
+ dracula: {
105
+ fg: [248, 248, 242], bg: [40, 42, 54],
106
+ teal: [139, 233, 253], cyan: [139, 233, 253], blue: [98, 114, 164], border: [98, 114, 164],
107
+ hover: [255, 121, 198], selBg: [68, 71, 90],
108
+ red: [255, 85, 85], green: [80, 250, 123], yellow: [241, 250, 140], orange: [255, 184, 108],
109
+ white: [248, 248, 242], gray: [98, 114, 164],
110
+ surface: [68, 71, 90],
111
+ },
112
+ gruvbox: {
113
+ fg: [235, 219, 178], bg: [40, 40, 40],
114
+ teal: [142, 192, 124], cyan: [131, 165, 152], blue: [69, 133, 136], border: [146, 131, 116],
115
+ hover: [250, 189, 47], selBg: [60, 56, 54],
116
+ red: [251, 73, 52], green: [184, 187, 38], yellow: [250, 189, 47], orange: [254, 128, 25],
117
+ white: [235, 219, 178], gray: [146, 131, 116],
118
+ surface: [60, 56, 54],
119
+ },
120
+ mono: {
121
+ // No hue at all: every accent is a grey, so the UI is readable on a monochrome
122
+ // terminal or for anyone who needs colour off without losing the structure that
123
+ // colour was carrying. The lightness steps are what keep the context bar legible.
124
+ fg: [230, 230, 230], bg: [18, 18, 18],
125
+ teal: [190, 190, 190], cyan: [210, 210, 210], blue: [150, 150, 150], border: [110, 110, 110],
126
+ hover: [255, 255, 255], selBg: [60, 60, 60],
127
+ red: [200, 90, 90], green: [140, 190, 140], yellow: [200, 190, 120], orange: [210, 160, 110],
128
+ white: [240, 240, 240], gray: [130, 130, 130],
129
+ surface: [42, 42, 42],
130
+ },
131
+ catppuccin: {
132
+ // Mocha: a warm-tinted dark with pastel accents. Every accent is desaturated toward
133
+ // the background so long replies do not glare, which is the whole point of the palette.
134
+ fg: [205, 214, 244], bg: [30, 30, 46],
135
+ teal: [137, 180, 250], cyan: [137, 220, 235], blue: [137, 180, 250], border: [108, 112, 134],
136
+ hover: [203, 166, 247], selBg: [49, 50, 68],
137
+ red: [243, 139, 168], green: [166, 227, 161], yellow: [249, 226, 175], orange: [250, 179, 135],
138
+ white: [205, 214, 244], gray: [127, 132, 156],
139
+ surface: [49, 50, 68],
140
+ },
141
+ tokyo: {
142
+ // Night: a blue-black with high-contrast accents. Distinct from nord, which is greyed
143
+ // out; this keeps the blue in the background and lets the cyan carry the structure.
144
+ fg: [192, 202, 245], bg: [26, 27, 38],
145
+ teal: [125, 207, 255], cyan: [125, 207, 255], blue: [122, 162, 247], border: [86, 95, 137],
146
+ hover: [187, 154, 247], selBg: [41, 46, 66],
147
+ red: [247, 118, 142], green: [158, 206, 106], yellow: [224, 175, 104], orange: [255, 158, 100],
148
+ white: [192, 202, 245], gray: [86, 95, 137],
149
+ surface: [41, 46, 66],
150
+ },
151
+ solar: {
152
+ // Solarized LIGHT. Included because `light` is a plain neutral and solarized's even
153
+ // hue ramp is the best-tested light palette in the terminal world. The dark variant is
154
+ // `solar-dark`, so both exist; the light one is the one worth having.
155
+ fg: [101, 123, 131], bg: [253, 246, 227],
156
+ teal: [38, 139, 210], cyan: [38, 139, 210], blue: [108, 113, 196], border: [147, 161, 161],
157
+ hover: [211, 54, 130], selBg: [238, 232, 213],
158
+ red: [220, 50, 47], green: [133, 153, 0], yellow: [181, 137, 0], orange: [203, 75, 22],
159
+ white: [101, 123, 131], gray: [147, 161, 161],
160
+ surface: [238, 232, 213],
161
+ },
162
+ rose: {
163
+ // Pine: a muted mauve dark, between dracula's saturation and nord's restraint.
164
+ fg: [224, 222, 244], bg: [25, 23, 36],
165
+ teal: [196, 167, 231], cyan: [137, 180, 250], blue: [137, 180, 250], border: [110, 106, 134],
166
+ hover: [196, 167, 231], selBg: [31, 29, 46],
167
+ red: [215, 130, 126], green: [156, 207, 216], yellow: [234, 157, 173], orange: [235, 188, 186],
168
+ white: [224, 222, 244], gray: [110, 106, 134],
169
+ surface: [31, 29, 46],
170
+ },
171
+ };
172
+ /** Theme name -> one-line description, for the /theme picker. */
173
+ export const THEME_LABELS = {
174
+ auto: 'follow the terminal: light theme on a light background, dark otherwise',
175
+ dark: 'dark teal background, white text',
176
+ light: 'light background, darkened accents',
177
+ nord: 'cool blue-grey',
178
+ dracula: 'high-contrast purple and pink',
179
+ gruvbox: 'warm retro earth tones',
180
+ catppuccin: 'soft pastel, warm-tinted dark',
181
+ tokyo: 'blue-black, high-contrast accents',
182
+ solar: 'even hue ramp, light background',
183
+ rose: 'muted mauve dark',
184
+ mono: 'greyscale, no hue',
185
+ };
186
+
187
+ /** `auto` is a name you can set without being a palette, so it is listed separately from
188
+ * the tables — see `setTheme` for how it resolves. */
189
+ export const AUTO_THEME = 'auto';
190
+
191
+ /** Whether a theme name is one we know. */
192
+ export function hasTheme(name) {
193
+ return name === AUTO_THEME || Object.prototype.hasOwnProperty.call(THEMES, name);
194
+ }
195
+
196
+ /** The names a user may choose, `auto` first because it is the default. */
197
+ export const THEME_NAMES = [AUTO_THEME, ...Object.keys(THEMES)];
198
+
199
+ /** True when a theme paints on a light background. Resolves `auto` first, so a caller
200
+ * asking about the CURRENT theme gets the answer for what is actually on screen. */
201
+ export function isLightTheme(name) {
202
+ const resolved = name === AUTO_THEME ? (autoIsLight ? 'light' : 'dark') : name;
203
+ const t = THEMES[resolved];
204
+ return !!(t && Array.isArray(t.bg) && t.bg[0] > 128);
205
+ }
206
+
207
+ export const C = {
208
+ reset: '\x1b[0m',
209
+ bold: '\x1b[1m',
210
+ dim: '\x1b[2m',
211
+ // Italic (SGR 3). Used for the BODY of a reasoning block — the prose reads as
212
+ // an aside rather than as the answer. Must be closed with SGR 23 (`endItalic`),
213
+ // NOT with `reset`, because resetting also drops the surrounding dim/gray that
214
+ // the same row depends on. Terminals without italic fall back to normal text,
215
+ // so this is a decoration, never load-bearing.
216
+ italic: '\x1b[3m',
217
+ endItalic: '\x1b[23m',
218
+ muted: '\x1b[90m',
219
+ teal: TEAL,
220
+ cyan: CYAN,
221
+ // The mark's shadow: the same cyan at 40% brightness. `dim` (SGR 2) is applied by the TERMINAL and most render
222
+ // it barely darker; `teal` is only ~15% off `cyan`. Neither reads as a shadow behind the letters.
223
+ cyanDeep: rgb(0, 86, 102),
224
+ // The startup heading: the grey the annotations use, lifted towards white. The wordmark above it owns the cyan,
225
+ // so the name under it is a lighter neutral rather than a second colour competing with the mark.
226
+ heading: rgb(200, 210, 220),
227
+ // DIFF COLOURS ARE DERIVED FROM THE THEME, not fixed.
228
+ //
229
+ // A diff paints two layers: the whole changed LINE gets a wash, and the words that actually differ get a
230
+ // stronger one. Those have to sit on THIS theme's background — the Islands Dark tints read as a warm grey on
231
+ // gruvbox and as nothing at all on `light`. `setTheme` recomputes all four from the theme's own `green`, `red`
232
+ // and `bg`, so switching themes recolours the diff with everything else. The values here are the dark defaults,
233
+ // used before any theme is applied.
234
+ diffAddBg: brgb(34, 58, 38),
235
+ diffAddWordBg: brgb(52, 96, 58),
236
+ diffDelBg: brgb(74, 32, 34),
237
+ diffDelWordBg: brgb(112, 44, 48),
238
+ diffAddFg: rgb(133, 208, 143),
239
+ diffDelFg: rgb(255, 176, 168),
240
+ // A code comment. `setTheme` mixes it from the palette's foreground towards its background, so it is quiet without
241
+ // disappearing — `C.gray` is tuned for the page and vanishes on a diff wash in several themes.
242
+ comment: '\x1b[90m',
243
+ fg: FG,
244
+ bg: BG,
245
+ bgTeal: brgb(0, 184, 219),
246
+ bgDim: brgb(12, 24, 30),
247
+ // Fills for the segmented context bar: five blues in one family, deepening towards
248
+ // black, plus a neutral free segment. Background colours, because the bar is read by
249
+ // FILL rather than by any text — a segment is usually only a few columns wide, so a
250
+ // letter inside it would be unreadable noise.
251
+ //
252
+ // The steps are deliberately narrow. Widening them makes the bar easier to tell apart
253
+ // at a glance but harder to keep a dark terminal readable, and the darkest member has
254
+ // to stay distinguishable from the page background it sits on.
255
+ ctxSystem: brgb(35, 48, 95), // deepest navy
256
+ ctxPrompt: brgb(43, 61, 120), // navy
257
+ ctxAssistant: brgb(52, 74, 146), // indigo
258
+ ctxThinking: brgb(77, 107, 254), // brand blue, the brightest used segment
259
+ ctxTools: brgb(90, 124, 255), // light blue
260
+ ctxFree: brgb(46, 46, 46), // neutral grey: "nothing here yet"
261
+ // The SURFACE colour for a floating panel (picker menus, dialogs). A NEUTRAL GREY,
262
+ // deliberately separate from the theme's `bg`:
263
+ //
264
+ // * the theme background is `[0, 18, 26]` — a dark TEAL. A raised surface should be
265
+ // neutral in every theme; only the ACCENTS carry the brand hue. Reusing `bg` for
266
+ // the panel made every popup read blue, which is not what a panel is.
267
+ // * `applyTheme` used to overwrite this with `t.bg`, so the value here only applied
268
+ // before a theme was set. It is no longer touched by the theme.
269
+ //
270
+ // xterm-256's grey ramp runs 232..255 from rgb(8) to rgb(238) in steps of 10, so 236
271
+ // is rgb(48,48,48) = #303030 — clearly lifted off a near-black terminal without
272
+ // washing the text out. Truecolor gets `#2e2e2e`, the same shade written exactly.
273
+ bgPanel: TRUECOLOR ? '\x1b[48;2;46;46;46m' : '\x1b[48;5;236m',
274
+ // The code-surface background, set by setTheme from the theme's own page colour. A placeholder until then, so
275
+ // code drawn before the first setTheme still has a surface.
276
+ codeBg: TRUECOLOR ? '\x1b[48;2;12;20;26m' : '\x1b[48;5;233m',
277
+ // The terminal-surface background, set by setTheme from the theme's page colour. Placeholder until then.
278
+ termBg: TRUECOLOR ? '\x1b[48;2;0;14;21m' : '\x1b[48;5;234m',
279
+ // The composer's neutral-grey surface. Placeholder until setTheme runs.
280
+ composerBg: TRUECOLOR ? '\x1b[48;2;44;44;46m' : '\x1b[48;5;237m',
281
+ border: BORDER,
282
+ red: '\x1b[91m',
283
+ green: '\x1b[92m',
284
+ yellow: '\x1b[93m',
285
+ magenta: '\x1b[95m',
286
+ gray: '\x1b[90m',
287
+ white: '\x1b[97m',
288
+ orange: TRUECOLOR ? rgb(255, 140, 0) : '\x1b[38;5;208m',
289
+ // Cyan pushed slightly toward green — used for the Swarm mode badge so it reads
290
+ // as its own mode next to Plan (pure cyan) and Focus (blue) while staying in the
291
+ // theme's blue-green family. 38;5;43 is rgb(0,215,175): red at zero and green
292
+ // only 40 above blue, i.e. still clearly cyan with a green cast. 38;5;85 was the
293
+ // first attempt and read as plain GREEN — its red channel is lifted to 95 and
294
+ // green pins at 255, which is a spring green, not a green-tinted cyan.
295
+ spring: TRUECOLOR ? rgb(0, 215, 175) : '\x1b[38;5;43m',
296
+ // Git branch in the status line: yellow pulled about half way toward white, so
297
+ // it stays clearly "yellow" (not cream/ivory) while reading brighter than the
298
+ // plain 93 yellow on a dark background. rgb(255,219,110) keeps red at max,
299
+ // green near it, and blue around 43% — that ratio is what keeps the hue
300
+ // yellow rather than dropping into orange.
301
+ branch: TRUECOLOR ? rgb(255, 219, 110) : '\x1b[38;5;222m',
302
+ // Shell-mode (`!`) accent — violet, matching kimi-code's shellMode token so the
303
+ // bash-mode editor border and the `!` read as "shell", distinct from the cyan
304
+ // prompt and the yellow git badge. #BD93F9 dark / #7C3AED light.
305
+ shellMode: TRUECOLOR ? rgb(189, 147, 249) : '\x1b[38;5;141m',
306
+ bgRed: '\x1b[41m',
307
+ bgGreen: '\x1b[42m',
308
+ // Selection highlight (mouse text selection) and the scrollbar gutter.
309
+ // Selection uses a dark teal background so white text stays readable.
310
+ selBg: TRUECOLOR ? brgb(0, 70, 84) : '\x1b[48;5;23m', // Original dark cyan background
311
+ // Reset ONLY the background colour, keeping the current foreground. Used to end
312
+ // a selection highlight so text after it keeps its own colour (a bare ESC[0m
313
+ // would also reset the foreground, turning a cyan/white run grey).
314
+ bgReset: '\x1b[49m',
315
+ // Scrollbar, styled after OpenTUI (what cline uses): a solid block thumb on a
316
+ // near-black track. Only the FOREGROUND colour is stateful — the track is always the
317
+ // background — because that is how OpenTUI's SliderRenderable paints a cell:
318
+ // foreground = thumb, background = track. A `█` covers both columns with the
319
+ // foreground, while `▀`/`▄` expose one half to the track, so the two colours must
320
+ // stay DISTINCT; giving a half-cell the thumb colour as its background is what made
321
+ // it read as a second square, and it also survived the modal backdrop's faint
322
+ // attribute, which fades the foreground only.
323
+ // track #252527 thumb #9a9ea3 (OpenTUI's defaults, inlined)
324
+ // hover #d2d6da active #ffffff (brighter thumb, not a new hue)
325
+ scrollTrackBg: TRUECOLOR ? brgb(0x25, 0x25, 0x27) : '\x1b[48;5;235m',
326
+ scrollTrackFg: TRUECOLOR ? rgb(0x25, 0x25, 0x27) : '\x1b[38;5;235m',
327
+ scrollThumbBg: TRUECOLOR ? brgb(0x9a, 0x9e, 0xa3) : '\x1b[48;5;246m',
328
+ scrollThumbFg: TRUECOLOR ? rgb(0x9a, 0x9e, 0xa3) : '\x1b[38;5;246m',
329
+ scrollThumbHoverBg: TRUECOLOR ? brgb(0xd2, 0xd6, 0xda) : '\x1b[48;5;252m',
330
+ scrollThumbHoverFg: TRUECOLOR ? rgb(0xd2, 0xd6, 0xda) : '\x1b[38;5;252m',
331
+ scrollThumbActiveBg: TRUECOLOR ? brgb(0xff, 0xff, 0xff) : '\x1b[48;5;231m',
332
+ scrollThumbActiveFg: TRUECOLOR ? rgb(0xff, 0xff, 0xff) : '\x1b[38;5;231m',
333
+ // FOREGROUND aliases kept for the todo/queue resize rule, which draws a `─` line
334
+ // and therefore needs a foreground, not the block's background pair.
335
+ scrollThumbActive: TRUECOLOR ? rgb(0xff, 0xff, 0xff) : '\x1b[38;5;231m',
336
+ scrollThumbHover: TRUECOLOR ? rgb(0xd2, 0xd6, 0xda) : '\x1b[38;5;252m',
337
+ // Legacy foreground forms, still referenced by the gutter-hover path.
338
+ scrollTrack: '\x1b[90m',
339
+ scrollThumb: TRUECOLOR ? rgb(0, 184, 219) : '\x1b[38;5;37m',
340
+ // Hover highlight for clickable rows (menu items, picker options, form
341
+ // fields, composer rows). An explicit bright foreground + BOLD rather than a
342
+ // bare BOLD: BOLD only brightens the 16-colour palette (it does nothing to a
343
+ // true-color row), which is why hover used to be invisible on most rows.
344
+ // tintRange() re-applies this so the row's own SGR codes cannot cancel it.
345
+ hover: '\x1b[1m' + rgb(190, 245, 255),
346
+ };
347
+
348
+ export function style(text, ...codes) {
349
+ return codes.join('') + text + '\x1b[0m';
350
+ }
351
+
352
+ // Linearly interpolate between two RGB triples and return an ANSI colour string.
353
+ // Used for the pulsing Working indicator (orange pulses to a brighter/darker
354
+ // shade over a 2s cycle).
355
+ export function lerpColor(r1, g1, b1, r2, g2, b2, t) {
356
+ const ti = Math.max(0, Math.min(1, t));
357
+ const r = Math.round(r1 + (r2 - r1) * ti);
358
+ const g = Math.round(g1 + (g2 - g1) * ti);
359
+ const b = Math.round(b1 + (b2 - b1) * ti);
360
+ return rgb(r, g, b);
361
+ }
362
+
363
+ // Which concrete theme `auto` resolves to right now. `setTheme` keeps it so a repaint or a
364
+ // `/theme auto` after a terminal probe lands on the same answer.
365
+ let autoIsLight = false;
366
+
367
+ // The theme `setTheme` last applied, resolved (so `auto` reports the concrete name it
368
+ // became). Read by the render cache: a cached row holds finished escapes, so a palette
369
+ // change has to invalidate them or the old colours survive the switch.
370
+ let appliedTheme = '';
371
+
372
+ /** The concrete theme in force, or `''` before the first `setTheme`. */
373
+ export function currentTheme() { return appliedTheme; }
374
+
375
+ /**
376
+ * Tell `setTheme` whether the terminal has a light background.
377
+ *
378
+ * The probe lives in term-caps.js and this module is a leaf that imports nothing, so the
379
+ * answer has to be handed in. Called once after the probe and again if the terminal is
380
+ * resized on a different display.
381
+ */
382
+ export function setLightBackground(light) {
383
+ autoIsLight = !!light;
384
+ }
385
+
386
+ // Apply a named theme IN PLACE. `C` is a module-level object and ESM imports are
387
+ // live bindings, so mutating its properties here changes the colours everywhere
388
+ // without re-importing. Returns true when the name was recognised.
389
+ //
390
+ // `auto` is a CHOICE, not a palette: it resolves to `light` on a light terminal and to
391
+ // `dark` otherwise, and then applies that theme's colours. It used to be a fixed table —
392
+ // a dark background with brand-cyan text — which is not what "auto" means and not what its
393
+ // own label claimed ("on the terminal's own palette"). The user asking for auto is asking
394
+ // not to have to think about it.
395
+ export function setTheme(name) {
396
+ const resolved = name === 'auto' ? (autoIsLight ? 'light' : 'dark') : name;
397
+ const t = THEMES[resolved];
398
+ if (!t) return false;
399
+ appliedTheme = resolved;
400
+
401
+ // A light page inverts several surface decisions, so everything below needs this. Declared at the top because
402
+ // the scrollbar and the context bar — set before the diff section — use it too.
403
+ const isLight = t.bg && t.bg[0] > 128;
404
+ // Mix `a` towards `b`: k = 1 is all `a`, k = 0 is all `b`.
405
+ const mix = (a, b, k) => a.map((v, i) => Math.round(v * k + b[i] * (1 - k)));
406
+
407
+
408
+
409
+ // Update foreground colors
410
+ C.teal = rgb(...t.teal);
411
+ C.fg = rgb(...t.fg);
412
+ C.cyan = rgb(...t.cyan);
413
+ C.blue = rgb(...t.blue);
414
+ C.border = rgb(...t.border);
415
+ // Update background colors
416
+ C.bg = brgb(...t.bg);
417
+ C.bgTeal = brgb(...t.teal);
418
+ // The panel surface is NEUTRAL GREY and comes from the theme's own `surface` triple,
419
+ // NOT from `bg`. It used to be set to `t.bg` (a dark teal), which made every popup
420
+ // carry a blue cast; a raised surface must be neutral, with only the accents following
421
+ // the brand hue. A light theme gets a light grey so the panel still reads as raised.
422
+ if (t.surface) C.bgPanel = brgb(...t.surface);
423
+ C.selBg = brgb(...t.selBg);
424
+
425
+ // Scrollbar colours. The thumb is a BLOCK, so its colour goes on BOTH the
426
+ // foreground and the background (a half-cell `▀`/`▄` shows the foreground, a full
427
+ // `█` shows the background — they must match). Hover / drag are brighter shades
428
+ // of the same hue, applied to the same pair, so the whole thumb changes together.
429
+ const active = [
430
+ Math.min(255, Math.round(t.teal[0] * 0.6 + 255 * 0.4)),
431
+ Math.min(255, Math.round(t.teal[1] * 0.6 + 255 * 0.4)),
432
+ Math.min(255, Math.round(t.teal[2] * 0.6 + 255 * 0.4)),
433
+ ];
434
+ const hover = t.hover || [190, 245, 255];
435
+ // The rail: the page, one step darker, so it reads as part of the surface rather than a strip of grey
436
+ // from another interface. Was a fixed `#252527` in every theme.
437
+ const track = mix(t.bg, [0, 0, 0], isLight ? 0.93 : 0.86);
438
+ C.scrollTrackFg = rgb(...track);
439
+ C.scrollTrackBg = brgb(...track);
440
+ C.scrollThumbFg = rgb(...t.teal);
441
+ C.scrollThumbBg = brgb(...t.teal);
442
+ C.scrollThumbHoverFg = rgb(...hover);
443
+ C.scrollThumbHoverBg = brgb(...hover);
444
+ C.scrollThumbActiveFg = rgb(...active);
445
+ C.scrollThumbActiveBg = brgb(...active);
446
+ // Foreground aliases for the todo/queue resize rule.
447
+ C.scrollTrack = '\x1b[90m';
448
+ C.scrollThumb = rgb(...t.teal);
449
+ C.scrollThumbHover = rgb(...hover);
450
+ C.scrollThumbActive = rgb(...active);
451
+
452
+ // Hover stays visible on this theme's background (bright on dark, dark on light).
453
+ C.hover = '\x1b[1m' + rgb(...(t.hover || [190, 245, 255]));
454
+
455
+ // Semantic colors — swap to deep versions on light backgrounds.
456
+ if (t.white) C.white = rgb(...t.white);
457
+ if (t.gray) C.gray = rgb(...t.gray);
458
+ if (t.red) C.red = rgb(...t.red);
459
+ if (t.green) C.green = rgb(...t.green);
460
+ if (t.yellow) C.yellow = rgb(...t.yellow);
461
+ if (t.orange) C.orange = rgb(...t.orange);
462
+ // The git branch colour is derived per theme: the pale yellow that reads well
463
+ // on the dark background is nearly invisible on white, so on a light theme it
464
+ // is darkened (keeping the same hue) instead of being left as-is.
465
+ if (t.bg && t.bg[0] > 128) {
466
+ C.branch = rgb(146, 106, 0); // dark amber for light backgrounds
467
+ } else {
468
+ C.branch = TRUECOLOR ? rgb(255, 219, 110) : '\x1b[38;5;222m';
469
+ }
470
+ // DIFF TINTS, mixed from this theme's own green/red towards its background.
471
+ //
472
+ // A line wash is a WEAK mix and a word wash a STRONGER one of the same pair: weak enough that ordinary text on it
473
+ // still reads as text, strong enough that the block is visible without reading it. Mixing towards `t.bg` rather
474
+ // than towards black is what makes a light theme work — a dark red wash on white is a black bar with letters in
475
+ // it, while a pale red is a highlighter mark.
476
+ const green = t.green || [120, 200, 120];
477
+ const red = t.red || [230, 80, 80];
478
+ // ABOUT HALF. Lower mixes disappear into a dark page — the wash has to be obvious without being read, and at
479
+ // 16% an added block looked like ordinary text with a faint tint. Light backgrounds need slightly more still,
480
+ // because the same distance in RGB is a smaller difference next to white.
481
+ const line = isLight ? 0.52 : 0.46;
482
+ const word = isLight ? 0.68 : 0.62;
483
+ C.diffAddBg = brgb(...mix(green, t.bg, line));
484
+ C.diffAddWordBg = brgb(...mix(green, t.bg, word));
485
+ C.diffDelBg = brgb(...mix(red, t.bg, line));
486
+ C.diffDelWordBg = brgb(...mix(red, t.bg, word));
487
+ // The sign and the text of a changed line are the same hue as its wash, pulled towards the theme's own foreground
488
+ // so they read ON that wash — not the theme's plain green/red, which is tuned for the page background.
489
+ C.diffAddFg = rgb(...mix(green, t.fg, 0.55));
490
+ C.diffDelFg = rgb(...mix(red, t.fg, 0.55));
491
+
492
+ // THE CODE-SURFACE BACKGROUND, one step off the page. Read rows, Write previews and diff CONTEXT lines sit on
493
+ // it; diff `+`/`-` rows keep their green/red wash on top.
494
+ //
495
+ // The step is a VISIBLE DISTANCE, not a ratio. 6% of a near-black page doubles its brightness, while 6% of a
496
+ // white page is 16 levels out of 255 and reads as no change at all. A light page also has no brighter side to
497
+ // step toward, so its steps stay near white — 250 page, 241 code, 231 terminal — where a larger step turned the
498
+ // whole screen grey.
499
+ const codeStep = isLight ? 0.96 : 0.94;
500
+ C.codeBg = brgb(...mix(t.bg, t.fg, codeStep));
501
+
502
+ // THE CONTEXT BAR FOLLOWS THE THEME. These six were fixed constants in every palette, so a dracula or gruvbox
503
+ // session drew the same navy-to-blue bar as the default dark one — and its "free" segment was a neutral grey
504
+ // that matched neither the bar nor the page. The bar is a row of FILLS read at a glance, so each step is a mix
505
+ // of the theme accent into its own page: the free part is the plain page, and the used parts climb towards the
506
+ // accent.
507
+ const ctxBase = mix(t.bg, t.fg, codeStep); // = codeBg, the brightest surface
508
+ const ctxMix = (k) => brgb(...mix(ctxBase, t.teal, k));
509
+ C.ctxSystem = ctxMix(0.68);
510
+ C.ctxPrompt = ctxMix(0.56);
511
+ C.ctxAssistant = ctxMix(0.42);
512
+ C.ctxThinking = ctxMix(0.26);
513
+ C.ctxTools = ctxMix(0.10);
514
+ // The free segment is the page itself: "nothing here yet" should look like the surface it sits on, and a separate
515
+ // grey made the empty half of the bar look like a second widget.
516
+ C.ctxFree = brgb(...ctxBase);
517
+ // THE TERMINAL-SURFACE BACKGROUND: the same small step on the OTHER side of the page, so the ordering holds on
518
+ // both kinds of theme — page < terminal < code on a dark one, terminal < code < page on a light one. Bash output
519
+ // and the shell-mode echo sit on it: a command reads as "a terminal ran this" without becoming a black band,
520
+ // which is what a deeper well looked like — a hole in the screen rather than a region of it.
521
+ //
522
+ // Towards BLACK on a dark theme and towards the foreground on a light one: mixing a dark page further towards its
523
+ // own foreground would make it lighter, which is `codeBg`'s job, not this one.
524
+ C.termBg = brgb(...mix(t.bg, [0, 0, 0], isLight ? 0.925 : 0.78));
525
+ // THE COMPOSER SURFACE: NEUTRAL, and derived from the page. Neutral because a hue here reads as "more page"
526
+ // rather than as an input field; derived because a fixed grey meant every theme drew the default dark theme's
527
+ // input box. The three channels are set to the page's own LUMINANCE plus a step, so the field is grey while
528
+ // still belonging to the palette it sits in. A theme may override it with its own `composer` triple.
529
+ const pageLum = (t.bg[0] + t.bg[1] + t.bg[2]) / 3;
530
+ const neutral = Math.max(0, Math.min(255, Math.round(pageLum + (isLight ? -14 : 14))));
531
+ C.composerBg = brgb(...(t.composer || [neutral, neutral, neutral + (isLight ? 0 : 2)]));
532
+
533
+ // A comment: the palette's foreground, pulled most of the way towards its background so it reads as quiet. A dark
534
+ // page can afford a looser mix; a light one cannot — solar's foreground is already a muted blue-grey, and at the
535
+ // dark theme's ratio its comments came within 8 of the removal wash.
536
+ C.comment = rgb(...mix(t.fg, t.bg, isLight ? 0.86 : 0.72));
537
+
538
+ return true;
539
+ }
540
+
541
+ export function clearScreen() {
542
+ return '\x1b[3J\x1b[H\x1b[2J';
543
+ }
544
+
545
+ // Clear screen and set background color based on current theme
546
+ export function clearAndSetBg() {
547
+ // Reset all attributes first
548
+ const reset = '\x1b[0m';
549
+ // Clear screen
550
+ const clear = '\x1b[3J\x1b[H\x1b[2J';
551
+ // Set background color using current C.bg value
552
+ const bg = C.bg || '\x1b[48;2;0;18;26m'; // Default dark background
553
+ return reset + clear + bg;
554
+ }
555
+ export function eraseLine() {
556
+ return '\x1b[2K';
557
+ }
558
+ export function eraseScreen() {
559
+ return '\x1b[2J';
560
+ }
561
+ export function moveTo(r, c) {
562
+ return `\x1b[${r + 1};${c + 1}H`;
563
+ }
564
+ export function cursorUp(n) { return `\x1b[${n}A`; }
565
+ export function cursorDown(n) { return `\x1b[${n}B`; }
566
+ export function cursorLeft(n) { return `\x1b[${n}D`; }
567
+ export function cursorForward(n) { return `\x1b[${n}C`; }
568
+ export function hideCursor() { return '\x1b[?25l'; }
569
+ export function showCursor() { return '\x1b[?25h'; }
570
+ // Cursor shape: terminals accept DECSCUSR — 2 = block, 4 = underline, 6 = bar(I-beam);
571
+ // 1/3/5 = blinking variants. Force a solid block cursor so positioning is always exact
572
+ // and unaffected by I-beam alignment.
573
+ export function blockCursor() { return '\x1b[2 q'; }
574
+ export function underlineCursor() { return '\x1b[4 q'; }
575
+ export function barCursor() { return '\x1b[6 q'; }
576
+ export function alternateScreen(on) { return on ? '\x1b[?1049h' : '\x1b[?1049l'; }
577
+ export function setScrollRegion(top, bottom) {
578
+ if (process.platform === 'win32') return ''; // win TTY scroll regions are unreliable
579
+ return `\x1b[${top + 1};${bottom + 1}r`;
580
+ }
581
+ export function resetScrollRegion() {
582
+ return '\x1b[r';
583
+ }
584
+ export function setBracketed(on) {
585
+ return `\x1b[?2004h${on ? '' : ''}`; // placeholder; real impl toggles bracketed paste
586
+ }