xonecas.io
64875
Mc

mini-coder / commit f8f2fca

refactor(cli): split input helpers from readline loop

xonecas2026-03-14 f8f2fca5797ce193b5a710b5c0f6898f354e074a parent 9f76dd6

3 files changed

src/cli/input-buffer.ts+101−0
@@ -0,0 +1,101 @@
1+const PASTE_TOKEN_START = 0xe000;
2+const PASTE_TOKEN_END = 0xf8ff;
3+
4+export function createPasteToken(
5+ buf: string,
6+ pasteTokens: ReadonlyMap<string, string>,
7+): string {
8+ for (let code = PASTE_TOKEN_START; code <= PASTE_TOKEN_END; code++) {
9+ const token = String.fromCharCode(code);
10+ if (!buf.includes(token) && !pasteTokens.has(token)) return token;
11+ }
12+ throw new Error("Too many pasted chunks in a single prompt");
13+}
14+
15+export function pasteLabel(text: string): string {
16+ const lines = text.split("\n");
17+ const first = lines[0] ?? "";
18+ const preview = first.length > 40 ? `${first.slice(0, 40)}…` : first;
19+ const extra = lines.length - 1;
20+ const more = extra > 0 ? ` +${extra} more line${extra === 1 ? "" : "s"}` : "";
21+ return `[pasted: "${preview}"${more}]`;
22+}
23+
24+function processInputBuffer(
25+ buf: string,
26+ pasteTokens: ReadonlyMap<string, string>,
27+ replacer: (ch: string, pasted: string | undefined) => string,
28+): string {
29+ let out = "";
30+ for (let i = 0; i < buf.length; i++) {
31+ const ch = buf[i] ?? "";
32+ out += replacer(ch, pasteTokens.get(ch));
33+ }
34+ return out;
35+}
36+
37+export function renderInputBuffer(
38+ buf: string,
39+ pasteTokens: ReadonlyMap<string, string>,
40+): string {
41+ return processInputBuffer(buf, pasteTokens, (ch, pasted) =>
42+ pasted ? pasteLabel(pasted) : ch,
43+ );
44+}
45+
46+export function expandInputBuffer(
47+ buf: string,
48+ pasteTokens: ReadonlyMap<string, string>,
49+): string {
50+ return processInputBuffer(buf, pasteTokens, (ch, pasted) => pasted ?? ch);
51+}
52+
53+export function pruneInputPasteTokens(
54+ pasteTokens: ReadonlyMap<string, string>,
55+ ...buffers: readonly string[]
56+): Map<string, string> {
57+ const referenced = buffers.join("");
58+ const next = new Map<string, string>();
59+ for (const [token, text] of pasteTokens) {
60+ if (referenced.includes(token)) next.set(token, text);
61+ }
62+ return next;
63+}
64+
65+export function getVisualCursor(
66+ buf: string,
67+ cursor: number,
68+ pasteTokens: ReadonlyMap<string, string>,
69+): number {
70+ let visual = 0;
71+ for (let i = 0; i < Math.min(cursor, buf.length); i++) {
72+ const ch = buf[i] ?? "";
73+ const pasted = pasteTokens.get(ch);
74+ visual += pasted ? pasteLabel(pasted).length : 1;
75+ }
76+ return visual;
77+}
78+
79+export function buildPromptDisplay(
80+ text: string,
81+ cursor: number,
82+ maxLen: number,
83+): { display: string; cursor: number } {
84+ const clampedCursor = Math.max(0, Math.min(cursor, text.length));
85+ if (maxLen <= 0) return { display: "", cursor: 0 };
86+ if (text.length <= maxLen) return { display: text, cursor: clampedCursor };
87+
88+ let start = Math.max(0, clampedCursor - maxLen);
89+ const end = Math.min(text.length, start + maxLen);
90+ if (end - start < maxLen) start = Math.max(0, end - maxLen);
91+
92+ let display = text.slice(start, end);
93+ if (start > 0 && display.length > 0) display = `…${display.slice(1)}`;
94+ if (end < text.length && display.length > 0)
95+ display = `${display.slice(0, -1)}…`;
96+
97+ return {
98+ display,
99+ cursor: Math.min(clampedCursor - start, display.length),
100+ };
101+}
src/cli/input-control.ts+27−0
@@ -0,0 +1,27 @@
1+const ESC = "\x1B";
2+const CTRL_C = "\x03";
3+
4+const ESC_BYTE = ESC.charCodeAt(0);
5+const CTRL_C_BYTE = CTRL_C.charCodeAt(0);
6+
7+export function getTurnControlAction(
8+ chunk: Uint8Array,
9+): "cancel" | "quit" | null {
10+ if (chunk.length === 1 && chunk[0] === ESC_BYTE) return "cancel";
11+ for (const byte of chunk) {
12+ if (byte === CTRL_C_BYTE) return "quit";
13+ }
14+ return null;
15+}
16+
17+export function getSubagentControlAction(
18+ chunk: Uint8Array,
19+): "cancel" | "quit" | null {
20+ for (const byte of chunk) {
21+ if (byte === CTRL_C_BYTE) return "quit";
22+ }
23+ for (const byte of chunk) {
24+ if (byte === ESC_BYTE) return "cancel";
25+ }
26+ return null;
27+}
src/cli/input.ts+24−131
@@ -7,7 +7,19 @@ import {
77 isImageFilename,
88 loadImageFile,
99 } from "./image-types.ts";
10+import {
11+ buildPromptDisplay,
12+ createPasteToken,
13+ expandInputBuffer,
14+ getVisualCursor,
15+ pruneInputPasteTokens,
16+ renderInputBuffer,
17+} from "./input-buffer.ts";
1018 import { getInputCompletion } from "./input-completion.ts";
19+import {
20+ getSubagentControlAction,
21+ getTurnControlAction,
22+} from "./input-control.ts";
1123 import { terminal } from "./terminal-io.ts";
1224
1325 // ─── ANSI escape sequences ────────────────────────────────────────────────────
@@ -35,8 +47,6 @@ const CTRL_K = "\x0B";
3547 const CTRL_L = "\x0C";
3648 const CTRL_R = "\x12";
3749 const TAB = "\x09";
38-const ESC_BYTE = ESC.charCodeAt(0);
39-const CTRL_C_BYTE = CTRL_C.charCodeAt(0);
4050
4151 // ─── Types ────────────────────────────────────────────────────────────────────
4252
@@ -49,6 +59,18 @@ export type InputResult =
4959 | { type: "command"; command: string; args: string }
5060 | { type: "shell"; command: string };
5161
62+export {
63+ buildPromptDisplay,
64+ expandInputBuffer,
65+ pasteLabel,
66+ pruneInputPasteTokens,
67+ renderInputBuffer,
68+} from "./input-buffer.ts";
69+export {
70+ getSubagentControlAction,
71+ getTurnControlAction,
72+} from "./input-control.ts";
73+
5274 // ─── Image detection ─────────────────────────────────────────────────────────
5375
5476 /**
@@ -149,33 +171,6 @@ async function readKey(reader: StreamReader): Promise<string> {
149171 // handler — NOT via the shared ReadableStream reader — so that the watcher and
150172 // readline() never race for the same bytes. The caller must call the returned
151173 // stop() in a finally block.
152-export function getTurnControlAction(
153- chunk: Uint8Array,
154-): "cancel" | "quit" | null {
155- // A bare ESC press sends exactly one byte (0x1B). Multi-byte escape
156- // sequences (arrow keys, function keys, etc.) always follow with more
157- // bytes in the same chunk, so we only cancel on a lone ESC.
158- if (chunk.length === 1 && chunk[0] === ESC_BYTE) return "cancel";
159- for (const byte of chunk) {
160- if (byte === CTRL_C_BYTE) return "quit";
161- }
162- return null;
163-}
164-
165-export function getSubagentControlAction(
166- chunk: Uint8Array,
167-): "cancel" | "quit" | null {
168- // During custom-command subagent runs there is no interactive line editing,
169- // so any ESC byte should interrupt (terminals may encode ESC as multi-byte
170- // CSI/SS3 or other escape-prefixed sequences).
171- for (const byte of chunk) {
172- if (byte === CTRL_C_BYTE) return "quit";
173- }
174- for (const byte of chunk) {
175- if (byte === ESC_BYTE) return "cancel";
176- }
177- return null;
178-}
179174
180175 type WatchForCancelOptions = {
181176 allowSubagentEsc?: boolean;
@@ -236,108 +231,6 @@ export function watchForCancel(
236231
237232 // ─── Paste handling ───────────────────────────────────────────────────────────
238233
239-const PASTE_TOKEN_START = 0xe000;
240-const PASTE_TOKEN_END = 0xf8ff;
241-
242-function createPasteToken(
243- buf: string,
244- pasteTokens: ReadonlyMap<string, string>,
245-): string {
246- for (let code = PASTE_TOKEN_START; code <= PASTE_TOKEN_END; code++) {
247- const token = String.fromCharCode(code);
248- if (!buf.includes(token) && !pasteTokens.has(token)) return token;
249- }
250- throw new Error("Too many pasted chunks in a single prompt");
251-}
252-
253-export function pasteLabel(text: string): string {
254- const lines = text.split("\n");
255- const first = lines[0] ?? "";
256- const preview = first.length > 40 ? `${first.slice(0, 40)}…` : first;
257- const extra = lines.length - 1;
258- const more = extra > 0 ? ` +${extra} more line${extra === 1 ? "" : "s"}` : "";
259- return `[pasted: "${preview}"${more}]`;
260-}
261-
262-function processInputBuffer(
263- buf: string,
264- pasteTokens: ReadonlyMap<string, string>,
265- replacer: (ch: string, pasted: string | undefined) => string,
266-): string {
267- let out = "";
268- for (let i = 0; i < buf.length; i++) {
269- const ch = buf[i] ?? "";
270- out += replacer(ch, pasteTokens.get(ch));
271- }
272- return out;
273-}
274-
275-export function renderInputBuffer(
276- buf: string,
277- pasteTokens: ReadonlyMap<string, string>,
278-): string {
279- return processInputBuffer(buf, pasteTokens, (ch, pasted) =>
280- pasted ? pasteLabel(pasted) : ch,
281- );
282-}
283-
284-export function expandInputBuffer(
285- buf: string,
286- pasteTokens: ReadonlyMap<string, string>,
287-): string {
288- return processInputBuffer(buf, pasteTokens, (ch, pasted) => pasted ?? ch);
289-}
290-
291-export function pruneInputPasteTokens(
292- pasteTokens: ReadonlyMap<string, string>,
293- ...buffers: readonly string[]
294-): Map<string, string> {
295- const referenced = buffers.join("");
296- const next = new Map<string, string>();
297- for (const [token, text] of pasteTokens) {
298- if (referenced.includes(token)) next.set(token, text);
299- }
300- return next;
301-}
302-
303-function getVisualCursor(
304- buf: string,
305- cursor: number,
306- pasteTokens: ReadonlyMap<string, string>,
307-): number {
308- let visual = 0;
309- for (let i = 0; i < Math.min(cursor, buf.length); i++) {
310- const ch = buf[i] ?? "";
311- const pasted = pasteTokens.get(ch);
312- visual += pasted ? pasteLabel(pasted).length : 1;
313- }
314- return visual;
315-}
316-
317-export function buildPromptDisplay(
318- text: string,
319- cursor: number,
320- maxLen: number,
321-): { display: string; cursor: number } {
322- const clampedCursor = Math.max(0, Math.min(cursor, text.length));
323- if (maxLen <= 0) return { display: "", cursor: 0 };
324- if (text.length <= maxLen) return { display: text, cursor: clampedCursor };
325-
326- let start = Math.max(0, clampedCursor - maxLen);
327- const end = Math.min(text.length, start + maxLen);
328- if (end - start < maxLen) start = Math.max(0, end - maxLen);
329-
330- let display = text.slice(start, end);
331- if (start > 0 && display.length > 0) display = `…${display.slice(1)}`;
332- if (end < text.length && display.length > 0)
333- display = `${display.slice(0, -1)}…`;
334-
335- return {
336- display,
337- cursor: Math.min(clampedCursor - start, display.length),
338- };
339-}
340-
341234 // ─── Main readline function ───────────────────────────────────────────────────
342235
343236 const PROMPT = c.green("▶ ");