xonecas.io
64875
Mc

mini-coder / commit 1bd4ec0

fix: align tui theme with terminal colors

xonecas2026-04-06 1bd4ec0c3d990df2f88d7b4c9584c9bea78e2d53 parent f8b3eb2

6 files changed

TODO.md+0−8
@@ -9,18 +9,10 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
99
1010 ### MUST FIX
1111
12-- [ ] **Streaming Markdown wraps can corrupt the UI until the turn finishes** (`ui.ts` / cel-tui Markdown integration) — some styled Markdown lines break badly at wrap boundaries during streaming, causing duplicated/corrupted rendering in the conversation log. The corruption clears itself once the assistant turn completes, which suggests a bug in the incremental render path around wrapped styled content rather than persisted message rendering. Fix: reproduce with a regression test and adjust t …
13-
1412 - [ ] **Status bar context usage percentage is wrong** (`ui.ts`) — the token usage indicator reports a much higher fraction of the model context window than is actually in use (for example, showing roughly half full when usage is still low). That makes the context meter untrustworthy and could mislead users about when compaction is needed. Fix: audit the percentage calculation and ensure it uses the correct token totals against the active model's real context window.
1513
16-- [ ] **Conversation log scroll speed is too slow** (`ui.ts`) — scrolling the conversation log appears to move only one line at a time, which makes navigating longer sessions tedious. The scroll step should feel responsive and usable for large transcripts. Fix: audit the wheel/key scroll increment and tune it to scroll by a larger chunk per interaction.
17-
18-- [ ] **UI theming is broken on non-dark background terminals** This is on us. `cel-tui` explicitly handles the terminal bg and fg; we hardcoded colors without any thought for how the theme feels or looks. Awful work.
19-
2014 ### High
2115
22-- [ ] **Animated turn divider flickers a cursor-shaped artifact at the pulse edge** (`ui.ts`) — while the agent is running, the divider animation shows a flickering cursor-like mark on the right edge of the bright moving segment. So far this was only observed in Alacritty; Ghostty renders the same animation correctly, which suggests a terminal-specific rendering or cursor interaction issue. That makes the busy indicator look glitchy and suggests the animated segment rendering is leaving a stal …
23-
2416 - [ ] **Uncaught tool handler exceptions** (`agent.ts`) — if a plugin handler (or a built-in on a filesystem error like EPERM) throws, the agent loop crashes. The exception propagates through `submitMessage` (try/finally, no catch) to a `.catch(console.error)` in `handleInput` — invisible in the TUI. The conversation is left inconsistent: `tool_start` emitted but `tool_end` never fires, tool result message never appended. Fix: wrap the `handler()` call in try/catch, produce an error `ToolExe …
2517
2618 - [ ] **`/undo` race condition** (`ui.ts`) — `/undo` aborts the agent loop then immediately calls `undoLastTurn`, but `abort()` is asynchronous. The agent loop may still write an `AssistantMessage` (stopReason "aborted") to the same turn after `undoLastTurn` has deleted it, leaving orphaned messages in the DB. Fix: store the agent loop promise as module state; in `handleUndoCommand`, await it (via `.finally()`) before calling `undoLastTurn`.
spec.md+19−11
@@ -438,25 +438,33 @@ All UI colors are defined in a single `Theme` object. The UI never hardcodes col
438438 ```ts
439439 interface Theme {
440440 /** User message background. */
441- userMsgBg: string;
441+ userMsgBg: Color | undefined;
442+ /** Muted informational text, placeholders, and helper copy. */
443+ mutedText: Color | undefined;
444+ /** Primary accent for important labels and interactive highlights. */
445+ accentText: Color | undefined;
446+ /** Secondary accent for supplementary labels like git status. */
447+ secondaryAccentText: Color | undefined;
442448 /** Tool output left border and text. */
443- toolBorder: string;
444- toolText: string;
449+ toolBorder: Color | undefined;
450+ toolText: Color | undefined;
445451 /** Diff colors in edit tool output. */
446- diffAdded: string;
447- diffRemoved: string;
452+ diffAdded: Color | undefined;
453+ diffRemoved: Color | undefined;
448454 /** Divider line color (idle state). */
449- divider: string;
455+ divider: Color | undefined;
450456 /** Divider scanning pulse highlight color (active state). */
451- dividerPulse: string;
452- /** Status bar foreground. */
453- statusText: string;
457+ dividerPulse: Color | undefined;
458+ /** Status bar foreground. Uses the terminal default when undefined. */
459+ statusText: Color | undefined;
454460 /** Error text. */
455- error: string;
461+ error: Color | undefined;
462+ /** Overlay modal background. */
463+ overlayBg: Color | undefined;
456464 }
457465 ```
458466
459-The default theme uses terminal palette colors (e.g., `color08` for dimmed, `color02` for green, `color01` for red) so it adapts to the user's terminal color scheme. Theme values are cel-tui color strings.
467+The default theme uses terminal palette colors where semantic accents help (`color08` for muted text, `color04`/`color05` for accents, `color02` for green, `color01` for red) and leaves shared foreground text on the terminal default where that gives better contrast. Theme values are cel-tui colors; `undefined` means "use the terminal default".
460468
461469 ### Status bar
462470
src/theme.test.ts+21−9
@@ -1,17 +1,29 @@
11 import { describe, expect, test } from "bun:test";
22 import { DEFAULT_THEME, mergeThemes } from "./theme.ts";
33
4+const THEME_KEYS = [
5+ "userMsgBg",
6+ "mutedText",
7+ "accentText",
8+ "secondaryAccentText",
9+ "toolBorder",
10+ "toolText",
11+ "diffAdded",
12+ "diffRemoved",
13+ "divider",
14+ "dividerPulse",
15+ "statusText",
16+ "error",
17+ "overlayBg",
18+] as const;
19+
420 describe("theme", () => {
521 test("DEFAULT_THEME has all required keys", () => {
6- expect(DEFAULT_THEME.userMsgBg).toBeString();
7- expect(DEFAULT_THEME.toolBorder).toBeString();
8- expect(DEFAULT_THEME.toolText).toBeString();
9- expect(DEFAULT_THEME.diffAdded).toBeString();
10- expect(DEFAULT_THEME.diffRemoved).toBeString();
11- expect(DEFAULT_THEME.divider).toBeString();
12- expect(DEFAULT_THEME.dividerPulse).toBeString();
13- expect(DEFAULT_THEME.statusText).toBeString();
14- expect(DEFAULT_THEME.error).toBeString();
22+ expect(Object.keys(DEFAULT_THEME).sort()).toEqual([...THEME_KEYS].sort());
23+ });
24+
25+ test("DEFAULT_THEME uses the terminal default foreground for shared status text", () => {
26+ expect(DEFAULT_THEME.statusText).toBeUndefined();
1527 });
1628
1729 test("mergeThemes with no overrides returns base unchanged", () => {
src/theme.ts+24−12
@@ -12,6 +12,9 @@
1212
1313 import type { Color } from "@cel-tui/types";
1414
15+/** A cel-tui palette color or the terminal default when undefined. */
16+type ThemeColor = Color | undefined;
17+
1518 // ---------------------------------------------------------------------------
1619 // Types
1720 // ---------------------------------------------------------------------------
@@ -24,25 +27,31 @@ import type { Color } from "@cel-tui/types";
2427 */
2528 export interface Theme {
2629 /** User message background. */
27- userMsgBg: Color;
30+ userMsgBg: ThemeColor;
31+ /** Muted informational text, placeholders, and helper copy. */
32+ mutedText: ThemeColor;
33+ /** Primary accent for important labels and interactive highlights. */
34+ accentText: ThemeColor;
35+ /** Secondary accent for supplementary labels like git status. */
36+ secondaryAccentText: ThemeColor;
2837 /** Tool output left border and text. */
29- toolBorder: Color;
38+ toolBorder: ThemeColor;
3039 /** Tool output text. */
31- toolText: Color;
40+ toolText: ThemeColor;
3241 /** Diff added line color (green). */
33- diffAdded: Color;
42+ diffAdded: ThemeColor;
3443 /** Diff removed line color (red). */
35- diffRemoved: Color;
44+ diffRemoved: ThemeColor;
3645 /** Divider line color (idle state). */
37- divider: Color;
46+ divider: ThemeColor;
3847 /** Divider scanning pulse highlight color (active state). */
39- dividerPulse: Color;
48+ dividerPulse: ThemeColor;
4049 /** Status bar foreground. */
41- statusText: Color;
50+ statusText: ThemeColor;
4251 /** Error text. */
43- error: Color;
52+ error: ThemeColor;
4453 /** Overlay modal background. */
45- overlayBg: Color;
54+ overlayBg: ThemeColor;
4655 }
4756
4857 // ---------------------------------------------------------------------------
@@ -57,13 +66,16 @@ export interface Theme {
5766 */
5867 export const DEFAULT_THEME: Theme = {
5968 userMsgBg: "color08",
69+ mutedText: "color08",
70+ accentText: "color04",
71+ secondaryAccentText: "color05",
6072 toolBorder: "color08",
6173 toolText: "color08",
6274 diffAdded: "color02",
6375 diffRemoved: "color01",
6476 divider: "color08",
65- dividerPulse: "color07",
66- statusText: "color07",
77+ dividerPulse: "color04",
78+ statusText: undefined,
6779 error: "color01",
6880 overlayBg: "color08",
6981 };
src/ui.test.ts+119−3
@@ -12,7 +12,7 @@ import {
1212 DEFAULT_VERBOSE,
1313 } from "./index.ts";
1414 import { createSession, loadMessages, openDatabase } from "./session.ts";
15-import { DEFAULT_THEME } from "./theme.ts";
15+import { DEFAULT_THEME, type Theme } from "./theme.ts";
1616 import {
1717 buildConversationLog,
1818 buildHelpText,
@@ -23,6 +23,7 @@ import {
2323 previewToolRenderLines,
2424 renderAssistantMessage,
2525 renderInputArea,
26+ renderStatusBar,
2627 renderStreamingResponse,
2728 renderToolResult,
2829 type ToolRenderLine,
@@ -47,6 +48,25 @@ function collectText(node: Node | null): string[] {
4748 return node.children.flatMap((child) => collectText(child));
4849 }
4950
51+function findTextNode(node: Node | null, content: string): Node | null {
52+ if (!node) {
53+ return null;
54+ }
55+ if (node.type === "text") {
56+ return node.content === content ? node : null;
57+ }
58+ if (node.type === "textinput") {
59+ return null;
60+ }
61+ for (const child of node.children) {
62+ const found = findTextNode(child, content);
63+ if (found) {
64+ return found;
65+ }
66+ }
67+ return null;
68+}
69+
5070 function createTestState(): AppState {
5171 const db = openDatabase(":memory:");
5272 const cwd = "/tmp/mini-coder-ui-test";
@@ -116,11 +136,21 @@ describe("ui rendering", () => {
116136 const node = renderAssistantMessage(message, {
117137 ...RENDER_OPTS,
118138 showReasoning: true,
139+ theme: { ...DEFAULT_THEME, mutedText: "color03" },
119140 });
120141 const text = collectText(node);
142+ const thinkingNode = findTextNode(
143+ node,
144+ "I should inspect the tests first.",
145+ );
121146
122147 expect(text).toContain("I should inspect the tests first.");
123148 expect(text).toContain("Done.");
149+ expect(thinkingNode).not.toBeNull();
150+ if (!thinkingNode || thinkingNode.type !== "text") {
151+ throw new Error("Expected thinking node to be a text node");
152+ }
153+ expect(thinkingNode.props.fgColor).toBe("color03");
124154 });
125155
126156 test("renderStreamingResponse keeps text and tool output inside one top-level container", () => {
@@ -252,8 +282,8 @@ describe("ui rendering", () => {
252282
253283 try {
254284 const controller = createInputController(state);
255- const first = renderInputArea(controller);
256- const second = renderInputArea(controller);
285+ const first = renderInputArea(state.theme, controller);
286+ const second = renderInputArea(state.theme, controller);
257287
258288 expect(first.type).toBe("vstack");
259289 expect(second.type).toBe("vstack");
@@ -272,6 +302,9 @@ describe("ui rendering", () => {
272302 throw new Error("Expected text input child nodes");
273303 }
274304
305+ expect(firstInput.props.placeholder?.props.fgColor).toBe(
306+ state.theme.mutedText,
307+ );
275308 expect(firstInput.props.onChange).toBe(controller.onChange);
276309 expect(firstInput.props.onFocus).toBe(controller.onFocus);
277310 expect(firstInput.props.onBlur).toBe(controller.onBlur);
@@ -285,6 +318,89 @@ describe("ui rendering", () => {
285318 }
286319 });
287320
321+ test("renderToolResult uses themed accent colors for tool headers", () => {
322+ const theme: Theme = {
323+ ...DEFAULT_THEME,
324+ accentText: "color04",
325+ secondaryAccentText: "color05",
326+ };
327+
328+ const shellNode = renderToolResult(
329+ "shell",
330+ { command: "seq 1 3" },
331+ "1\n2\n3",
332+ false,
333+ { ...RENDER_OPTS, theme },
334+ );
335+ const shellHeader = findTextNode(shellNode, "$ seq 1 3");
336+ expect(shellHeader).not.toBeNull();
337+ if (!shellHeader || shellHeader.type !== "text") {
338+ throw new Error("Expected shell header to be a text node");
339+ }
340+ expect(shellHeader.props.fgColor).toBe(theme.accentText);
341+
342+ const pluginNode = renderToolResult("plugin-tool", {}, "ok", false, {
343+ ...RENDER_OPTS,
344+ theme,
345+ });
346+ const pluginHeader = findTextNode(pluginNode, "plugin-tool");
347+ expect(pluginHeader).not.toBeNull();
348+ if (!pluginHeader || pluginHeader.type !== "text") {
349+ throw new Error("Expected plugin header to be a text node");
350+ }
351+ expect(pluginHeader.props.fgColor).toBe(theme.secondaryAccentText);
352+ });
353+
354+ test("renderStatusBar uses themed accent colors for git and model info", () => {
355+ const faux = registerFauxProvider();
356+ const state = createTestState();
357+ state.model = faux.getModel();
358+ state.git = {
359+ root: state.cwd,
360+ branch: "main",
361+ staged: 1,
362+ modified: 2,
363+ untracked: 3,
364+ ahead: 4,
365+ behind: 0,
366+ };
367+ state.theme = {
368+ ...DEFAULT_THEME,
369+ accentText: "color04",
370+ secondaryAccentText: "color05",
371+ statusText: undefined,
372+ } satisfies Theme;
373+
374+ try {
375+ const node = renderStatusBar(state);
376+
377+ expect(node.type).toBe("vstack");
378+ if (node.type !== "vstack") {
379+ throw new Error("Expected status bar to be a VStack");
380+ }
381+ expect(node.props.fgColor).toBeUndefined();
382+
383+ const gitNode = findTextNode(node, "main +1 ~2 ?3 ▲ 4");
384+ const modelNode = findTextNode(
385+ node,
386+ `${state.model.provider}/${state.model.id} · med`,
387+ );
388+ expect(gitNode).not.toBeNull();
389+ expect(modelNode).not.toBeNull();
390+ if (!gitNode || !modelNode) {
391+ throw new Error("Expected git and model text nodes");
392+ }
393+ if (gitNode.type !== "text" || modelNode.type !== "text") {
394+ throw new Error("Expected git and model nodes to be text");
395+ }
396+ expect(gitNode.props.fgColor).toBe(state.theme.secondaryAccentText);
397+ expect(modelNode.props.fgColor).toBe(state.theme.accentText);
398+ } finally {
399+ faux.unregister();
400+ state.db.close();
401+ }
402+ });
403+
288404 test("buildHelpText includes current reasoning and verbose state", () => {
289405 const helpState: HelpRenderState = {
290406 providers: new Map(),
src/ui.ts+62−37
@@ -361,7 +361,7 @@ export function renderAssistantMessage(
361361 VStack({ padding: { x: 1 } }, [
362362 Text(thinking, {
363363 wrap: "word",
364- fgColor: "color08",
364+ fgColor: opts.theme.mutedText,
365365 italic: true,
366366 }),
367367 ]),
@@ -497,13 +497,13 @@ function renderToolLine(line: ToolRenderLine, theme: Theme): Node {
497497 case "command":
498498 case "path":
499499 return Text(line.text, {
500- fgColor: "color06",
500+ fgColor: theme.accentText,
501501 bold: true,
502502 wrap: "word",
503503 });
504504 case "toolName":
505505 return Text(line.text, {
506- fgColor: "color05",
506+ fgColor: theme.secondaryAccentText,
507507 bold: true,
508508 wrap: "word",
509509 });
@@ -738,7 +738,7 @@ export function renderStreamingResponse(
738738 VStack({ padding: { x: 1 } }, [
739739 Text(streaming.thinking, {
740740 wrap: "word",
741- fgColor: "color08",
741+ fgColor: opts.theme.mutedText,
742742 italic: true,
743743 }),
744744 ]),
@@ -765,10 +765,10 @@ export function renderStreamingResponse(
765765 // ---------------------------------------------------------------------------
766766
767767 /** Render an internal UI message in the conversation log. */
768-function renderUiMessage(msg: UiMessage): Node {
768+function renderUiMessage(msg: UiMessage, theme: Theme): Node {
769769 return VStack({ padding: { x: 1 } }, [
770770 Text(msg.content, {
771- fgColor: "color08",
771+ fgColor: theme.mutedText,
772772 italic: true,
773773 wrap: "word",
774774 }),
@@ -798,7 +798,7 @@ export function buildConversationLog(state: AppState): Node[] {
798798
799799 for (const msg of state.messages) {
800800 if (msg.role === "ui") {
801- pushConversationNode(renderUiMessage(msg));
801+ pushConversationNode(renderUiMessage(msg, state.theme));
802802 } else if (msg.role === "user") {
803803 pushConversationNode(renderUserMessage(msg, state.theme));
804804 } else if (msg.role === "assistant") {
@@ -847,7 +847,7 @@ export function buildConversationLog(state: AppState): Node[] {
847847 state.model
848848 ? "Ready. Type a message to start."
849849 : "No providers configured. Use /login to authenticate.",
850- { fgColor: "color08", italic: true },
850+ { fgColor: state.theme.mutedText, italic: true },
851851 ),
852852 ]),
853853 );
@@ -887,7 +887,10 @@ function renderOverlay(state: AppState): Node {
887887 padding: { x: 1 },
888888 },
889889 [
890- Text(activeOverlay!.title, { bold: true, fgColor: "color06" }),
890+ Text(activeOverlay!.title, {
891+ bold: true,
892+ fgColor: state.theme.accentText,
893+ }),
891894 activeOverlay!.select(),
892895 ],
893896 ),
@@ -895,6 +898,42 @@ function renderOverlay(state: AppState): Node {
895898 );
896899 }
897900
901+// ---------------------------------------------------------------------------
902+// Status bar
903+// ---------------------------------------------------------------------------
904+
905+/**
906+ * Render the two-line status bar.
907+ *
908+ * @param state - Application state.
909+ * @returns The status bar node.
910+ */
911+export function renderStatusBar(state: AppState): Node {
912+ return VStack(
913+ {
914+ height: 2,
915+ padding: { x: 1 },
916+ fgColor: state.theme.statusText,
917+ },
918+ [
919+ HStack({}, [
920+ Text(abbreviatePath(state.cwd)),
921+ Spacer(),
922+ Text(formatGitStatus(state), {
923+ fgColor: state.theme.secondaryAccentText,
924+ }),
925+ ]),
926+ HStack({}, [
927+ Text(formatModelInfo(state), {
928+ fgColor: state.theme.accentText,
929+ }),
930+ Spacer(),
931+ Text(formatUsage(state)),
932+ ]),
933+ ],
934+ );
935+}
936+
898937 // ---------------------------------------------------------------------------
899938 // Input area
900939 // ---------------------------------------------------------------------------
@@ -954,17 +993,21 @@ export function createInputController(state: AppState): InputController {
954993 /**
955994 * Render the padded input area.
956995 *
996+ * @param theme - Active UI theme.
957997 * @param controller - Stable TextInput callbacks.
958998 * @returns The input area node.
959999 */
960-export function renderInputArea(controller: InputController): Node {
1000+export function renderInputArea(
1001+ theme: Theme,
1002+ controller: InputController,
1003+): Node {
9611004 return VStack({ padding: { x: 1 } }, [
9621005 TextInput({
9631006 flex: 1,
9641007 maxHeight: 10,
9651008 value: inputValue,
9661009 onChange: controller.onChange,
967- placeholder: Text("message…", { fgColor: "color08" }),
1010+ placeholder: Text("message…", { fgColor: theme.mutedText }),
9681011 focused: inputFocused,
9691012 onFocus: controller.onFocus,
9701013 onBlur: controller.onBlur,
@@ -1004,7 +1047,7 @@ function handleModelCommand(state: AppState): void {
10041047 maxVisible: OVERLAY_MAX_VISIBLE,
10051048 placeholder: "type to filter models...",
10061049 focused: true,
1007- highlightColor: "color06",
1050+ highlightColor: state.theme.accentText,
10081051 onSelect: (value) => {
10091052 const picked = models.find((m) => `${m.provider}/${m.id}` === value);
10101053 if (picked) {
@@ -1041,7 +1084,7 @@ function handleEffortCommand(state: AppState): void {
10411084 maxVisible: OVERLAY_MAX_VISIBLE,
10421085 placeholder: "type to filter...",
10431086 focused: true,
1044- highlightColor: "color06",
1087+ highlightColor: state.theme.accentText,
10451088 onSelect: (value) => {
10461089 state.effort = value as ThinkingLevel;
10471090 dismissOverlay();
@@ -1078,7 +1121,7 @@ function handleSessionCommand(state: AppState): void {
10781121 maxVisible: OVERLAY_MAX_VISIBLE,
10791122 placeholder: "type to filter sessions...",
10801123 focused: true,
1081- highlightColor: "color06",
1124+ highlightColor: state.theme.accentText,
10821125 onSelect: (sessionId) => {
10831126 if (sessionId !== state.session.id) {
10841127 const picked = sessions.find((s) => s.id === sessionId);
@@ -1217,7 +1260,7 @@ function handleLoginCommand(state: AppState): void {
12171260 maxVisible: OVERLAY_MAX_VISIBLE,
12181261 placeholder: "type to filter providers...",
12191262 focused: true,
1220- highlightColor: "color06",
1263+ highlightColor: state.theme.accentText,
12211264 onSelect: (providerId) => {
12221265 dismissOverlay();
12231266 const provider = oauthProviders.find((p) => p.id === providerId);
@@ -1300,7 +1343,7 @@ function handleLogoutCommand(state: AppState): void {
13001343 maxVisible: OVERLAY_MAX_VISIBLE,
13011344 placeholder: "type to filter providers...",
13021345 focused: true,
1303- highlightColor: "color06",
1346+ highlightColor: state.theme.accentText,
13041347 onSelect: (providerId) => {
13051348 delete state.oauthCredentials[providerId];
13061349 saveOAuthCredentials(state.oauthCredentials);
@@ -1460,7 +1503,7 @@ function showCommandAutocomplete(state: AppState): void {
14601503 maxVisible: OVERLAY_MAX_VISIBLE,
14611504 placeholder: "type to filter commands...",
14621505 focused: true,
1463- highlightColor: "color06",
1506+ highlightColor: state.theme.accentText,
14641507 onSelect: (value) => {
14651508 dismissOverlay();
14661509 handleInput(`/${value}`, state);
@@ -1752,31 +1795,13 @@ export function startUI(state: AppState): void {
17521795 renderDivider(state, cols),
17531796
17541797 // ── Input area ──
1755- renderInputArea(inputController),
1798+ renderInputArea(state.theme, inputController),
17561799
17571800 // ── Static divider ──
17581801 Divider({ fgColor: state.theme.divider }),
17591802
17601803 // ── Status bar (2 lines) ──
1761- VStack(
1762- {
1763- height: 2,
1764- padding: { x: 1 },
1765- fgColor: state.theme.statusText,
1766- },
1767- [
1768- HStack({}, [
1769- Text(abbreviatePath(state.cwd)),
1770- Spacer(),
1771- Text(formatGitStatus(state), { fgColor: "color05" }),
1772- ]),
1773- HStack({}, [
1774- Text(formatModelInfo(state), { fgColor: "color06" }),
1775- Spacer(),
1776- Text(formatUsage(state)),
1777- ]),
1778- ],
1779- ),
1804+ renderStatusBar(state),
17801805 ],
17811806 );