xonecas.io
64875
Mc

mini-coder / commit 2b3535e

fix: apply themeing consistently

xonecas2026-09-25 2b3535e860fcb6ffd18c974f6b23641844fc3792 parent 9f5da1c

6 files changed

src/tui/highlight.ts+16−71
@@ -5,72 +5,16 @@ import Parser from "tree-sitter";
55 import JavaScript from "tree-sitter-javascript";
66 import TypeScript from "tree-sitter-typescript";
77 import Markdown from "@tree-sitter-grammars/tree-sitter-markdown";
8+import { HEADINGS, INLINE_CODE, NORMAL_BG, NORMAL_FG, STYLES, channels, sgrPlain, type Style } from "./theme.ts";
89
910 /**
1011 * Syntax highlighting for committed scrollback, once, over the whole block.
1112 * Grammar queries ship with the grammar packages and are consumed as data, so
12- * a capture name is the only thing that reaches the palette. Colors are the
13- * TokyoNight `moon` palette, mapped the way `folke/tokyonight.nvim` maps
14- * capture names to highlight groups. Truecolor only.
13+ * a capture name is the only thing that reaches the palette. The colours come
14+ * from the TokyoNight `night` palette in `theme.ts`, mapped the way
15+ * `folke/tokyonight.nvim` maps capture names to highlight groups. Truecolor only.
1516 */
1617
17-interface Style {
18- fg?: string;
19- bg?: string;
20- bold?: boolean;
21- italic?: boolean;
22- underline?: boolean;
23-}
24-
25-/**
26- * Headings take their level's color from TokyoNight's rainbow, over a 10%
27- * tint of it, as `@markup.heading.N.markdown` does. The grammar's own query
28- * names one heading color for all levels, so the level comes from the marker.
29- */
30-const HEADINGS: Style[] = [
31- { fg: "#82aaff", bg: "#2c314a" },
32- { fg: "#ffc777", bg: "#38343c" },
33- { fg: "#c3e88d", bg: "#32383f" },
34- { fg: "#4fd6be", bg: "#263644" },
35- { fg: "#c099ff", bg: "#32304a" },
36- { fg: "#fca7ea", bg: "#383148" },
37-].map((style) => ({ ...style, bold: true }));
38-
39-/** Inline code: Helix's `@markup.raw.markdown_inline`, not Neovim's plain one. */
40-const INLINE_CODE: Style = { fg: "#82aaff", bg: "#444a73" };
41-
42-/** Capture name to color. A dotted name falls back to its parent. */
43-const STYLES: Record<string, Style | undefined> = {
44- comment: { fg: "#636da6" },
45- constant: { fg: "#ff966c" },
46- "constant.builtin": { fg: "#65bcff" },
47- constructor: { fg: "#c099ff" },
48- escape: { fg: "#c099ff" },
49- function: { fg: "#82aaff" },
50- "function.builtin": { fg: "#65bcff" },
51- "function.method": { fg: "#82aaff" },
52- keyword: { fg: "#fca7ea" },
53- number: { fg: "#ff966c" },
54- operator: { fg: "#89ddff" },
55- property: { fg: "#4fd6be" },
56- "punctuation.bracket": { fg: "#828bb8" },
57- "punctuation.delimiter": { fg: "#89ddff" },
58- "punctuation.special": { fg: "#89ddff" },
59- string: { fg: "#c3e88d" },
60- "string.special": { fg: "#65bcff" },
61- type: { fg: "#65bcff" },
62- "type.builtin": { fg: "#589ed7" },
63- variable: { fg: "#c8d3f5" },
64- "variable.builtin": { fg: "#ff757f" },
65- "variable.parameter": { fg: "#ffc777" },
66- // Markdown's queries predate the `@markup` rename; these are the old names.
67- "text.emphasis": { italic: true },
68- "text.strong": { bold: true },
69- "text.literal": { fg: "#c3e88d" },
70- "text.uri": { underline: true },
71- "text.reference": { fg: "#65bcff" },
72-};
73-
7418 function styleFor(capture: string): Style | undefined {
7519 for (let name = capture; ; name = name.slice(0, name.lastIndexOf("."))) {
7620 const style = STYLES[name];
@@ -152,24 +96,24 @@ function parse(syntax: Syntax, text: string): Parser.SyntaxNode {
15296
15397 /**
15498 * A style's own attributes, with the defaults spelled out: a span that names no
155- * color must not inherit the color of the span or token before it.
99+ * color must not inherit the color of the span or token before it, and the
100+ * fallback is the palette's `Normal`, never the terminal's own pair.
156101 */
157102 function sgr(style: Style): string {
158- const channel = (code: number, hex: string): string =>
159- `${code};2;${[1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join(";")};`;
160103 let out = "\x1b[";
161104 if (style.bold) out += "1;";
162105 if (style.italic) out += "3;";
163106 if (style.underline) out += "4;";
164- out += style.fg === undefined ? "39;" : channel(38, style.fg);
165- out += style.bg === undefined ? "49;" : channel(48, style.bg);
166- return `${out.slice(0, -1)}m`;
107+ out += `38;2;${channels(style.fg ?? NORMAL_FG)};48;2;${channels(style.bg ?? NORMAL_BG)}m`;
108+ return out;
167109 }
168110
169111 /**
170112 * Wraps every span in its color. Spans nest, so the innermost one wins; text
171- * outside any span is reset to plain. The trailing reset closes the whole text,
172- * so a span may cross a line break and the last line still ends plain.
113+ * outside any span falls back to `Normal`. The trailing close restores the same
114+ * pair, so a span may cross a line break and the last line still ends plain —
115+ * and no cell is ever left to the terminal. Because each row is painted on its
116+ * own, a span that outlives a line break re-asserts itself on the next line.
173117 */
174118 function paint(text: string, spans: Span[]): string {
175119 const events: { at: number; open: boolean; span: Span }[] = [];
@@ -187,13 +131,14 @@ function paint(text: string, spans: Span[]): string {
187131 if (end <= at) return;
188132 const style = active.length === 0 ? undefined : active[active.length - 1].style;
189133 if (style === undefined) {
190- if (!plain) out += "\x1b[0m";
134+ if (!plain) out += sgrPlain();
191135 plain = true;
192136 } else {
193137 out += sgr(style);
194138 plain = false;
195139 }
196- out += text.slice(at, end);
140+ const chunk = text.slice(at, end);
141+ out += style === undefined ? chunk : chunk.replace(/\n/g, `\n${sgr(style)}`);
197142 at = end;
198143 };
199144 for (const event of events) {
@@ -202,7 +147,7 @@ function paint(text: string, spans: Span[]): string {
202147 else active.splice(active.indexOf(event.span), 1);
203148 }
204149 emit(text.length);
205- return plain ? out : `${out}\x1b[0m`;
150+ return plain ? out : `${out}${sgrPlain()}`;
206151 }
207152
208153 /**
src/tui/stream.ts+2−0
@@ -5,6 +5,8 @@ import { dim } from "./styles.ts";
55 export interface BodyLine {
66 text: string;
77 style?: (text: string) => string;
8+ /** Row background, painted across the row's trailing cells by the renderer. */
9+ bg?: string;
810 }
911
1012 /** The text after the last newline: what is still incomplete. */
src/tui/styles.ts+18−10
@@ -1,24 +1,32 @@
1-/** Dim foreground (SGR 2), restored to normal intensity (SGR 22). */
1+import { NORMAL_FG, PALETTE, sgrFg } from "./theme.ts";
2+
3+/**
4+ * Inline foreground styling from the palette. The foreground is restored by
5+ * re-asserting `Normal`'s, never by resetting: the row's background, set by the
6+ * renderer, must survive, and no cell may fall back to the terminal's colours.
7+ */
8+function foreground(text: string, hex: string): string {
9+ return `${sgrFg(hex)}${text}${sgrFg(NORMAL_FG)}`;
10+}
11+
12+/** `Comment`, the colour diff context lines share. */
213 export function dim(text: string): string {
3- return `\x1b[2m${text}\x1b[22m`;
14+ return foreground(text, PALETTE.comment);
415 }
516
6-/** Red foreground (SGR 31), restored to the default foreground (SGR 39). */
717 export function red(text: string): string {
8- return `\x1b[31m${text}\x1b[39m`;
18+ return foreground(text, PALETTE.red);
919 }
1020
11-/** Green foreground (SGR 32), restored to the default foreground (SGR 39). */
1221 export function green(text: string): string {
13- return `\x1b[32m${text}\x1b[39m`;
22+ return foreground(text, PALETTE.green);
1423 }
1524
16-/** Yellow foreground (SGR 33), restored to the default foreground (SGR 39). */
1725 export function yellow(text: string): string {
18- return `\x1b[33m${text}\x1b[39m`;
26+ return foreground(text, PALETTE.yellow);
1927 }
2028
21-/** Cyan foreground (SGR 36), restored to the default foreground (SGR 39). */
29+/** Diff hunk headers; the palette's `blue` sits closest to the old ANSI cyan. */
2230 export function cyan(text: string): string {
23- return `\x1b[36m${text}\x1b[39m`;
31+ return foreground(text, PALETTE.blue);
2432 }
src/tui/theme.ts+134−0
@@ -0,0 +1,134 @@
1+/**
2+ * The one palette: TokyoNight `night`, transcribed from `folke/tokyonight.nvim`
3+ * (`lua/tokyonight/colors/night.lua` and its generated highlight groups). Every
4+ * colour the TUI writes comes from here, foreground and background alike, so no
5+ * row can fall back to the terminal's own colours. Truecolor only.
6+ */
7+export const PALETTE = {
8+ bg: "#1a1b26",
9+ bg_dark: "#16161e",
10+ bg_dark1: "#0c0e14",
11+ bg_highlight: "#292e42",
12+ fg: "#c0caf5",
13+ fg_dark: "#a9b1d6",
14+ fg_gutter: "#3b4261",
15+ comment: "#565f89",
16+ dark3: "#545c7e",
17+ dark5: "#737aa2",
18+ terminal_black: "#414868",
19+ blue: "#7aa2f7",
20+ blue0: "#3d59a1",
21+ blue1: "#2ac3de",
22+ blue2: "#0db9d7",
23+ blue5: "#89ddff",
24+ blue6: "#b4f9f8",
25+ blue7: "#394b70",
26+ cyan: "#7dcfff",
27+ green: "#9ece6a",
28+ green1: "#73daca",
29+ green2: "#41a6b5",
30+ magenta: "#bb9af7",
31+ magenta2: "#ff007c",
32+ orange: "#ff9e64",
33+ purple: "#9d7cd8",
34+ red: "#f7768e",
35+ red1: "#db4b4b",
36+ teal: "#1abc9c",
37+ yellow: "#e0af68",
38+} as const;
39+
40+/** `Normal`: the pair every row is painted with, and restored to. */
41+export const NORMAL_FG = PALETTE.fg;
42+export const NORMAL_BG = PALETTE.bg;
43+
44+/** Diff row backgrounds, from the `DiffAdd`/`DiffDelete` groups. */
45+export const DIFF_ADD = "#243e4a";
46+export const DIFF_DELETE = "#4a272f";
47+
48+/** `r;g;b` for one `#rrggbb`. */
49+export function channels(hex: string): string {
50+ return [1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join(";");
51+}
52+
53+/**
54+ * Explicit colour, never a reset: a `39`/`49` or a `0` would hand the rest of
55+ * the row back to the host terminal, which is the leak this palette closes.
56+ */
57+export function sgrFg(hex: string): string {
58+ return `\x1b[38;2;${channels(hex)}m`;
59+}
60+
61+export function sgrBg(hex: string): string {
62+ return `\x1b[48;2;${channels(hex)}m`;
63+}
64+
65+/**
66+ * Attributes off, then `Normal`. The state a row starts and ends in: bold, dim
67+ * and the like must not bleed past the span that set them, and a reset (`0`,
68+ * or a bare colour off) would drop the row back onto the host terminal.
69+ */
70+export function sgrPlain(): string {
71+ return `\x1b[22;23;24;38;2;${channels(NORMAL_FG)};48;2;${channels(NORMAL_BG)}m`;
72+}
73+
74+export interface Style {
75+ fg?: string;
76+ bg?: string;
77+ bold?: boolean;
78+ italic?: boolean;
79+ underline?: boolean;
80+}
81+
82+/**
83+ * Capture name to colour, mapped the way `folke/tokyonight.nvim` maps capture
84+ * names to highlight groups. A dotted name falls back to its parent.
85+ */
86+export const STYLES: Record<string, Style | undefined> = {
87+ comment: { fg: PALETTE.comment },
88+ constant: { fg: PALETTE.orange },
89+ "constant.builtin": { fg: PALETTE.blue1 },
90+ constructor: { fg: PALETTE.magenta },
91+ escape: { fg: PALETTE.magenta },
92+ function: { fg: PALETTE.blue },
93+ "function.builtin": { fg: PALETTE.blue1 },
94+ "function.method": { fg: PALETTE.blue },
95+ keyword: { fg: PALETTE.purple },
96+ number: { fg: PALETTE.orange },
97+ operator: { fg: PALETTE.blue5 },
98+ property: { fg: PALETTE.green1 },
99+ "punctuation.bracket": { fg: PALETTE.fg_dark },
100+ "punctuation.delimiter": { fg: PALETTE.blue5 },
101+ "punctuation.special": { fg: PALETTE.blue5 },
102+ string: { fg: PALETTE.green },
103+ "string.special": { fg: PALETTE.blue1 },
104+ type: { fg: PALETTE.blue1 },
105+ "type.builtin": { fg: "#27a1b9" }, // a per-variant literal, not a palette slot
106+ variable: { fg: PALETTE.fg },
107+ "variable.builtin": { fg: PALETTE.red },
108+ "variable.parameter": { fg: PALETTE.yellow },
109+ // Markdown's queries predate the `@markup` rename; these are the old names.
110+ "text.emphasis": { italic: true },
111+ "text.strong": { bold: true },
112+ "text.literal": { fg: PALETTE.green },
113+ "text.uri": { underline: true },
114+ "text.reference": { fg: PALETTE.blue1 },
115+};
116+
117+/**
118+ * Headings take their level's colour from TokyoNight's rainbow, over a 10% tint
119+ * of it, as `@markup.heading.N.markdown` does. The grammar's own query names one
120+ * heading colour for all levels, so the level comes from the marker.
121+ */
122+export const HEADINGS: Style[] = [
123+ { fg: PALETTE.blue, bg: "#24293b" },
124+ { fg: PALETTE.yellow, bg: "#2e2a2d" },
125+ { fg: PALETTE.green, bg: "#272d2d" },
126+ { fg: PALETTE.teal, bg: "#1a2b32" },
127+ { fg: PALETTE.magenta, bg: "#2a283b" },
128+ { fg: PALETTE.purple, bg: "#272538" },
129+ { fg: PALETTE.orange, bg: "#31282c" },
130+ { fg: PALETTE.red, bg: "#302430" },
131+].map((style) => ({ ...style, bold: true }));
132+
133+/** Inline code, `@markup.raw.markdown_inline`. */
134+export const INLINE_CODE: Style = { fg: PALETTE.blue, bg: PALETTE.terminal_black };
src/tui/tui.ts+47−15
@@ -6,6 +6,7 @@ import { Editor } from "./editor.ts";
66 import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
77 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
88 import { cyan, dim, green, red } from "./styles.ts";
9+import { DIFF_ADD, DIFF_DELETE, NORMAL_BG, sgrBg, sgrPlain } from "./theme.ts";
910 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
1011
1112 /** Status-row spinner frames; the only animation in the TUI. */
@@ -33,14 +34,18 @@ function callSummary(name: string, args: JsonObject): string {
3334 return JSON.stringify(args);
3435 }
3536
36-/** Unified-diff color for one `edit` body line; file headers are stripped. */
37-function diffStyle(line: string): ((text: string) => string) | undefined {
38- if (line.startsWith("@@")) return cyan;
39- if (line.startsWith("+")) return green;
40- if (line.startsWith("-")) return red;
41- if (line.startsWith("\\ No newline")) return dim;
42- if (line.startsWith(" ")) return dim;
43- return undefined;
37+/**
38+ * Unified-diff styling for one `edit` body line; file headers are stripped.
39+ * Additions and deletions carry the `DiffAdd`/`DiffDelete` background, so every
40+ * cell of the line — the trailing ones `paintRow` erases included — is tinted.
41+ */
42+function diffLine(line: string): BodyLine {
43+ if (line.startsWith("@@")) return { text: line, style: cyan };
44+ if (line.startsWith("+")) return { text: line, style: green, bg: DIFF_ADD };
45+ if (line.startsWith("-")) return { text: line, style: red, bg: DIFF_DELETE };
46+ if (line.startsWith("\\ No newline")) return { text: line, style: dim };
47+ if (line.startsWith(" ")) return { text: line, style: dim };
48+ return { text: line };
4449 }
4550
4651 /** Display rewrite of a tool result, by tool name. */
@@ -59,7 +64,7 @@ function resultLines(name: string, text: string, isError: boolean): BodyLine[] {
5964 while (lines.length > 0 && EDIT_HEADER.test(lines[0])) lines.shift();
6065 diff = true;
6166 }
62- return lines.map((line) => ({ text: line, style: diff ? diffStyle(line) : undefined }));
67+ return lines.map((line) => (diff ? diffLine(line) : { text: line }));
6368 }
6469
6570 /**
@@ -69,12 +74,34 @@ function resultLines(name: string, text: string, isError: boolean): BodyLine[] {
6974 */
7075 function renderRows(lines: BodyLine[], width: number): string[] {
7176 return lines.flatMap((line) => {
72- const style = line.style;
77+ const { style, bg } = line;
7378 const wrapped = wrapLine(expandTabs(sanitize(line.text)), width);
74- return style === undefined ? wrapped : wrapped.map((row) => (row === "" ? row : style(row)));
79+ return wrapped.map((row) => {
80+ if (row === "") return row;
81+ const styled = style === undefined ? row : style(row);
82+ // The row's own background opens and closes it. A markdown span that
83+ // carries a background (a heading tint, inline code) can still be open
84+ // where the row ends, and `paintRow`'s trailing erase fills with whatever
85+ // background is current — so the row has to hand it back its own.
86+ const rowBg = sgrBg(bg ?? NORMAL_BG);
87+ return `${rowBg}${styled}${rowBg}`;
88+ });
7589 });
7690 }
7791
92+/**
93+ * Paints one written row: the palette's pair, the row's own styling, then
94+ * `ESC[K` — erase to the end of the line, which the terminal does *with the
95+ * row's current background*. The row's trailing cells take the row's own
96+ * background (the diff tints included) without a glyph being written for them,
97+ * so a short row has no cell left to the host. Padding with spaces would look
98+ * the same, but a terminal that reflows on a narrower resize would then wrap
99+ * every row and lose the live region's anchor.
100+ */
101+function paintRow(line: string): string {
102+ return `${sgrPlain()}${line}\x1b[K`;
103+}
104+
78105 /** `renderRows` plus the display-only elision applied to long tool bodies. */
79106 function bodyRows(lines: BodyLine[], width: number): string[] {
80107 const rows = renderRows(lines, width);
@@ -103,7 +130,10 @@ class LiveRegion {
103130 let out = "";
104131 if (this.cursorUp > 0) out += `\x1b[${this.cursorUp}B`;
105132 if (this.rows > 1) out += `\x1b[${this.rows - 1}A`;
106- out += "\r\x1b[J";
133+ // `ESC[J` erases with the *current* background, so the palette's is set
134+ // first: without it the erase bites host-coloured holes and the region
135+ // flashes the terminal's background while typing.
136+ out += `\r${sgrPlain()}\x1b[J`;
107137 this.rows = 0;
108138 this.cursorUp = 0;
109139 return out;
@@ -459,11 +489,13 @@ class Tui {
459489 const blank = line === "" || (this.separator && this.wrote);
460490 this.separator = false;
461491 if (blank && this.wrote && !this.lastBlank) {
462- this.scroll += "\r\n";
492+ // A separator is a row the app occupies, so it is painted like any other:
493+ // a bare newline would leave the host terminal showing through it.
494+ this.scroll += `${paintRow("")}\r\n`;
463495 this.lastBlank = true;
464496 }
465497 if (line !== "") {
466- this.scroll += `${line}\r\n`;
498+ this.scroll += `${paintRow(line)}\r\n`;
467499 this.wrote = true;
468500 this.lastBlank = false;
469501 }
@@ -521,7 +553,7 @@ class Tui {
521553 const body = rows.slice(rows.length - keep);
522554 const editor = this.editor.render(width, Math.max(1, height - status.length - body.length));
523555
524- const lines = [...body, ...status, ...editor.rows];
556+ const lines = [...body, ...status, ...editor.rows].map(paintRow);
525557 let cursorRow = status.length + body.length + editor.cursorRow;
526558 if (cursorRow >= lines.length) cursorRow = lines.length - 1;
527559
tokyo-night-theme-nigth-variant.md+0−32
@@ -1,32 +0,0 @@
1-# Theme: Tokyo Night, Night variant
2-
3-## Current state
4-
5-mini-coder has one palette and it is dark, but only the syntax highlighting
6-carries any of it, and it carries the wrong variant: the hexes in
7-`src/tui/highlight.ts` are Tokyo Night **moon**, not **night**. No palette
8-definition exists outside that file.
9-
10-Everything the TUI itself writes — banner, status row, `> ` echo, ` | ` and
11-` ! ` body prefixes, `-> ` call lines, `[complete · Ns]`, `! cancelled`,
12-`! <error>`, `/help` — is styled with the terminal's default foreground (ANSI
13-30–37, or `SGR 2` on top of it) and no background at all. So on a light
14-terminal the chrome is unreadable and the moon tokens wash out; on a dark one
15-the screen is only half in Tokyo Night. The ux-walk recorded it as finding 5:
16-`ux-walk-20260924/ux-walk-20260924.md`, shots
17-`ux-walk-20260924/shots/s10-light-{idle,tools,markdown}-100x30-0{1,2,3}.png`.
18-
19-## Source of truth
20-
21-https://github.com/folke/tokyonight.nvim we use the **Night** variant as the
22-default.
23-
24-## Verification
25-
26-Repeat the exact same test that saw this issue in ux-walk and assert that both syntax highlight looks correct on dark background even if the terminal is set to light mode.
27-
28-# Goals:
29-
30-- Fix the theme pallete so it's accurate to folke's night variant.
31-- Ensure the app's foreground and background consistently use the theme's colors, and
32-no terminal defaults are visible inside mini-coder.