xonecas.io
64875
Mc

mini-coder / commit f8b3eb2

fix: stabilize tui text input handlers

xonecas2026-04-06 f8b3eb2a5f39e05bd9319c7d071774a418296c4d parent 6208adb

3 files changed

TODO.md+0−2
@@ -9,8 +9,6 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
99
1010 ### MUST FIX
1111
12-- [ ] **TextInput cursor is off by one after line wrapping** (`ui.ts` / cel-tui TextInput integration) — when the input wraps onto a new line, the visual cursor position drifts by one column from the actual insertion point. My current suspicion is that wrapping is happening at the cell level, but the cursor position is being computed against the logical text position without staying in sync with where the wrapped cell actually lands on the next line. That makes editing long prompts confusing a …
13-
1412 - [ ] **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 …
1513
1614 - [ ] **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.
src/ui.test.ts+40−0
@@ -16,11 +16,13 @@ import { DEFAULT_THEME } from "./theme.ts";
1616 import {
1717 buildConversationLog,
1818 buildHelpText,
19+ createInputController,
1920 type HelpRenderState,
2021 handleInput,
2122 type PendingToolCall,
2223 previewToolRenderLines,
2324 renderAssistantMessage,
25+ renderInputArea,
2426 renderStreamingResponse,
2527 renderToolResult,
2628 type ToolRenderLine,
@@ -245,6 +247,44 @@ describe("ui rendering", () => {
245247 }
246248 });
247249
250+ test("renderInputArea reuses stable TextInput handlers from the input controller", () => {
251+ const state = createTestState();
252+
253+ try {
254+ const controller = createInputController(state);
255+ const first = renderInputArea(controller);
256+ const second = renderInputArea(controller);
257+
258+ expect(first.type).toBe("vstack");
259+ expect(second.type).toBe("vstack");
260+ if (first.type !== "vstack" || second.type !== "vstack") {
261+ throw new Error("Expected input area wrappers to be VStack nodes");
262+ }
263+
264+ const firstInput = first.children[0];
265+ const secondInput = second.children[0];
266+ expect(firstInput?.type).toBe("textinput");
267+ expect(secondInput?.type).toBe("textinput");
268+ if (!firstInput || !secondInput) {
269+ throw new Error("Expected text input children to exist");
270+ }
271+ if (firstInput.type !== "textinput" || secondInput.type !== "textinput") {
272+ throw new Error("Expected text input child nodes");
273+ }
274+
275+ expect(firstInput.props.onChange).toBe(controller.onChange);
276+ expect(firstInput.props.onFocus).toBe(controller.onFocus);
277+ expect(firstInput.props.onBlur).toBe(controller.onBlur);
278+ expect(firstInput.props.onKeyPress).toBe(controller.onKeyPress);
279+ expect(secondInput.props.onChange).toBe(firstInput.props.onChange);
280+ expect(secondInput.props.onFocus).toBe(firstInput.props.onFocus);
281+ expect(secondInput.props.onBlur).toBe(firstInput.props.onBlur);
282+ expect(secondInput.props.onKeyPress).toBe(firstInput.props.onKeyPress);
283+ } finally {
284+ state.db.close();
285+ }
286+ });
287+
248288 test("buildHelpText includes current reasoning and verbose state", () => {
249289 const helpState: HelpRenderState = {
250290 providers: new Map(),
src/ui.ts+80−34
@@ -895,6 +895,84 @@ function renderOverlay(state: AppState): Node {
895895 );
896896 }
897897
898+// ---------------------------------------------------------------------------
899+// Input area
900+// ---------------------------------------------------------------------------
901+
902+/** Stable callbacks for the controlled TextInput. */
903+export interface InputController {
904+ /** Update the controlled input value and re-render. */
905+ onChange: (value: string) => void;
906+ /** Mark the input as focused and re-render. */
907+ onFocus: () => void;
908+ /** Mark the input as blurred and re-render. */
909+ onBlur: () => void;
910+ /** Intercept submit/autocomplete keys before default editing runs. */
911+ onKeyPress: (key: string) => boolean | undefined;
912+}
913+
914+/**
915+ * Create stable handlers for the main TextInput.
916+ *
917+ * cel-tui keys TextInput cursor/scroll state by the `onChange` function
918+ * reference, so these callbacks must be created once and reused across
919+ * renders.
920+ *
921+ * @param state - Application state used by the handlers.
922+ * @returns Stable callbacks for the controlled TextInput.
923+ */
924+export function createInputController(state: AppState): InputController {
925+ return {
926+ onChange: (value) => {
927+ inputValue = value;
928+ cel.render();
929+ },
930+ onFocus: () => {
931+ inputFocused = true;
932+ cel.render();
933+ },
934+ onBlur: () => {
935+ inputFocused = false;
936+ cel.render();
937+ },
938+ onKeyPress: (key) => {
939+ if (key === "enter") {
940+ const raw = inputValue;
941+ inputValue = "";
942+ cel.render();
943+ handleInput(raw, state);
944+ return false;
945+ }
946+ if (key === "tab" && inputValue.startsWith("/")) {
947+ showCommandAutocomplete(state);
948+ return false;
949+ }
950+ },
951+ };
952+}
953+
954+/**
955+ * Render the padded input area.
956+ *
957+ * @param controller - Stable TextInput callbacks.
958+ * @returns The input area node.
959+ */
960+export function renderInputArea(controller: InputController): Node {
961+ return VStack({ padding: { x: 1 } }, [
962+ TextInput({
963+ flex: 1,
964+ maxHeight: 10,
965+ value: inputValue,
966+ onChange: controller.onChange,
967+ placeholder: Text("message…", { fgColor: "color08" }),
968+ focused: inputFocused,
969+ onFocus: controller.onFocus,
970+ onBlur: controller.onBlur,
971+ onKeyPress: controller.onKeyPress,
972+ }),
973+ ]);
974+}
975+
898976 // ---------------------------------------------------------------------------
899977 // Command handlers
900978 // ---------------------------------------------------------------------------
@@ -1628,6 +1706,7 @@ async function gracefulExit(state: AppState): Promise<void> {
16281706 */
16291707 export function startUI(state: AppState): void {
16301708 const terminal = new ProcessTerminal();
1709+ const inputController = createInputController(state);
16311710 cel.init(terminal);
16321711
16331712 cel.viewport(() => {
@@ -1673,40 +1752,7 @@ export function startUI(state: AppState): void {
16731752 renderDivider(state, cols),
16741753
16751754 // ── Input area ──
1676- VStack({ padding: { x: 1 } }, [
1677- TextInput({
1678- flex: 1,
1679- maxHeight: 10,
1680- value: inputValue,
1681- onChange: (value) => {
1682- inputValue = value;
1683- cel.render();
1684- },
1685- placeholder: Text("message…", { fgColor: "color08" }),
1686- focused: inputFocused,
1687- onFocus: () => {
1688- inputFocused = true;
1689- cel.render();
1690- },
1691- onBlur: () => {
1692- inputFocused = false;
1693- cel.render();
1694- },
1695- onKeyPress: (key) => {
1696- if (key === "enter") {
1697- const raw = inputValue;
1698- inputValue = "";
1699- cel.render();
1700- handleInput(raw, state);
1701- return false;
1702- }
1703- if (key === "tab" && inputValue.startsWith("/")) {
1704- showCommandAutocomplete(state);
1705- return false;
1706- }
1707- },
1708- }),
1709- ]),
1755+ renderInputArea(inputController),
17101756
17111757 // ── Static divider ──
17121758 Divider({ fgColor: state.theme.divider }),