@hncode/hncode 0.8.1 → 0.8.3

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