xonecas.io
64875
Mc

mini-coder / commit 8c81d62

refactor(cli): extract input editing primitives

xonecas2026-03-14 8c81d62e65b83e1507e2009ec9ee02fe6705af8e parent c96779e

3 files changed

src/cli/input-editing.test.ts+35−0
@@ -0,0 +1,35 @@
1+import { describe, expect, test } from "bun:test";
2+import {
3+ deleteWordBackward,
4+ insertAtCursor,
5+ moveCursorWordLeft,
6+ moveCursorWordRight,
7+} from "./input-editing.ts";
8+
9+describe("input-editing", () => {
10+ test("insertAtCursor inserts text and advances cursor", () => {
11+ const next = insertAtCursor("hello world", 5, ",");
12+ expect(next).toEqual({
13+ buf: "hello, world",
14+ cursor: 6,
15+ });
16+ });
17+
18+ test("moveCursorWordLeft skips trailing spaces and previous word", () => {
19+ expect(moveCursorWordLeft("hello brave new world", 15)).toBe(12);
20+ expect(moveCursorWordLeft("hello world", 7)).toBe(0);
21+ });
22+
23+ test("moveCursorWordRight skips spaces then moves across next word", () => {
24+ expect(moveCursorWordRight("hello brave new world", 5)).toBe(11);
25+ expect(moveCursorWordRight("hello world", 5)).toBe(12);
26+ });
27+
28+ test("deleteWordBackward removes the previous word", () => {
29+ const next = deleteWordBackward("hello brave world", 12);
30+ expect(next).toEqual({
31+ buf: "hello world",
32+ cursor: 6,
33+ });
34+ });
35+});
src/cli/input-editing.ts+35−0
@@ -0,0 +1,35 @@
1+export function insertAtCursor(
2+ buf: string,
3+ cursor: number,
4+ text: string,
5+): { buf: string; cursor: number } {
6+ return {
7+ buf: buf.slice(0, cursor) + text + buf.slice(cursor),
8+ cursor: cursor + text.length,
9+ };
10+}
11+
12+export function moveCursorWordLeft(buf: string, cursor: number): number {
13+ let next = cursor;
14+ while (next > 0 && buf[next - 1] === " ") next--;
15+ while (next > 0 && buf[next - 1] !== " ") next--;
16+ return next;
17+}
18+
19+export function moveCursorWordRight(buf: string, cursor: number): number {
20+ let next = cursor;
21+ while (next < buf.length && buf[next] === " ") next++;
22+ while (next < buf.length && buf[next] !== " ") next++;
23+ return next;
24+}
25+
26+export function deleteWordBackward(
27+ buf: string,
28+ cursor: number,
29+): { buf: string; cursor: number } {
30+ const nextCursor = moveCursorWordLeft(buf, cursor);
31+ return {
32+ buf: buf.slice(0, nextCursor) + buf.slice(cursor),
33+ cursor: nextCursor,
34+ };
35+}
src/cli/input.ts+14−10
@@ -15,6 +15,12 @@ import {
1515 getSubagentControlAction,
1616 getTurnControlAction,
1717 } from "./input-control.ts";
18+import {
19+ deleteWordBackward as deleteWordBackwardInBuffer,
20+ insertAtCursor,
21+ moveCursorWordLeft,
22+ moveCursorWordRight,
23+} from "./input-editing.ts";
1824 import { tryExtractImageFromPaste } from "./input-images.ts";
1925 import { terminal } from "./terminal-io.ts";
2026
@@ -207,15 +213,15 @@ export async function readline(opts: { cwd?: string }): Promise<InputResult> {
207213 }
208214
209215 function insertText(text: string): void {
210- buf = buf.slice(0, cursor) + text + buf.slice(cursor);
211- cursor += text.length;
216+ const updated = insertAtCursor(buf, cursor, text);
217+ buf = updated.buf;
218+ cursor = updated.cursor;
212219 }
213220
214221 function deleteWordBackward(): void {
215- const end = cursor;
216- while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
217- while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
218- buf = buf.slice(0, cursor) + buf.slice(end);
222+ const updated = deleteWordBackwardInBuffer(buf, cursor);
223+ buf = updated.buf;
224+ cursor = updated.cursor;
219225 prunePasteTokens();
220226 renderPrompt();
221227 }
@@ -370,15 +376,13 @@ export async function readline(opts: { cwd?: string }): Promise<InputResult> {
370376 }
371377 // Alt+Left (word left): ESC b or ESC [1;3D
372378 if (raw === `${ESC}b` || raw === `${ESC}[1;3D`) {
373- while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
374- while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
379+ cursor = moveCursorWordLeft(buf, cursor);
375380 renderPrompt();
376381 continue;
377382 }
378383 // Alt+Right (word right): ESC f or ESC [1;3C
379384 if (raw === `${ESC}f` || raw === `${ESC}[1;3C`) {
380- while (cursor < buf.length && buf[cursor] === " ") cursor++;
381- while (cursor < buf.length && buf[cursor] !== " ") cursor++;
385+ cursor = moveCursorWordRight(buf, cursor);
382386 renderPrompt();
383387 continue;
384388 }