xonecas.io
6615
Yc

yoctoselect / commit 0729c91

fix: parse stdin buffer correctly to prevent swallowed input

The onData handler compared buf.toString() against exact strings like
"\x1b[B". When the terminal delivered multiple keys in a single data
event (e.g. arrow-down + typed char as "\x1b[Bv"), nothing matched
and input was silently dropped.

New src/input.ts tokenizes the buffer in a loop, consuming escape
sequences and emitting typed InputEvents. select.ts now processes
all events from each data chunk before re-rendering.

Includes 22 tests covering single keys, mixed sequences, and edge
cases like ESC-followed-by-non-bracket.

xonecas2026-03-22 0729c919fe1e4bcd4ec36c9ad986031bc30b17dc parent 91dcc5b

4 files changed

AGENTS.md+3−1
@@ -56,8 +56,10 @@ On each keystroke: move cursor up to the search line, clear from there down, re-
5656 ```
5757 src/
5858 index.ts — public API (re-exports select)
59- select.ts — select() implementation: stdin handling, render, filter
59+ select.ts — select() implementation: event handling, render, filter
60+ input.ts — stdin buffer parsing: tokenizes raw bytes into InputEvents
6061 filter.ts — prefix match logic
62+ viewport.ts — scroll offset and highlight movement (pure functions)
6163 ansi.ts — ANSI escape helpers (cursor movement, clear lines)
6264 tests/
6365 filter.test.ts — test prefix matching
src/input.test.ts+119−0
@@ -0,0 +1,119 @@
1+import { describe, expect, test } from "bun:test";
2+import { parseInput } from "./input.ts";
3+
4+describe("parseInput", () => {
5+ test("single printable character", () => {
6+ expect(parseInput("a")).toEqual([{ type: "char", char: "a" }]);
7+ });
8+
9+ test("multiple printable characters", () => {
10+ expect(parseInput("abc")).toEqual([
11+ { type: "char", char: "a" },
12+ { type: "char", char: "b" },
13+ { type: "char", char: "c" },
14+ ]);
15+ });
16+
17+ test("space character", () => {
18+ expect(parseInput(" ")).toEqual([{ type: "char", char: " " }]);
19+ });
20+
21+ test("enter (CR)", () => {
22+ expect(parseInput("\r")).toEqual([{ type: "enter" }]);
23+ });
24+
25+ test("enter (LF)", () => {
26+ expect(parseInput("\n")).toEqual([{ type: "enter" }]);
27+ });
28+
29+ test("backspace (DEL 0x7f)", () => {
30+ expect(parseInput("\x7f")).toEqual([{ type: "backspace" }]);
31+ });
32+
33+ test("backspace (BS 0x08)", () => {
34+ expect(parseInput("\x08")).toEqual([{ type: "backspace" }]);
35+ });
36+
37+ test("bare ESC", () => {
38+ expect(parseInput("\x1b")).toEqual([{ type: "escape" }]);
39+ });
40+
41+ test("arrow up", () => {
42+ expect(parseInput("\x1b[A")).toEqual([{ type: "up" }]);
43+ });
44+
45+ test("arrow down", () => {
46+ expect(parseInput("\x1b[B")).toEqual([{ type: "down" }]);
47+ });
48+
49+ test("home key", () => {
50+ expect(parseInput("\x1b[H")).toEqual([{ type: "home" }]);
51+ });
52+
53+ test("home key (alternate)", () => {
54+ expect(parseInput("\x1b[1~")).toEqual([{ type: "home" }]);
55+ });
56+
57+ test("end key", () => {
58+ expect(parseInput("\x1b[F")).toEqual([{ type: "end" }]);
59+ });
60+
61+ test("end key (alternate)", () => {
62+ expect(parseInput("\x1b[4~")).toEqual([{ type: "end" }]);
63+ });
64+
65+ // THE BUG: mixed sequences in a single data event
66+ test("arrow down followed by printable char", () => {
67+ expect(parseInput("\x1b[Bv")).toEqual([
68+ { type: "down" },
69+ { type: "char", char: "v" },
70+ ]);
71+ });
72+
73+ test("arrow up followed by multiple chars", () => {
74+ expect(parseInput("\x1b[Aabc")).toEqual([
75+ { type: "up" },
76+ { type: "char", char: "a" },
77+ { type: "char", char: "b" },
78+ { type: "char", char: "c" },
79+ ]);
80+ });
81+
82+ test("two arrow keys in sequence", () => {
83+ expect(parseInput("\x1b[B\x1b[B")).toEqual([
84+ { type: "down" },
85+ { type: "down" },
86+ ]);
87+ });
88+
89+ test("char then arrow then char", () => {
90+ expect(parseInput("x\x1b[Ay")).toEqual([
91+ { type: "char", char: "x" },
92+ { type: "up" },
93+ { type: "char", char: "y" },
94+ ]);
95+ });
96+
97+ test("arrow followed by enter", () => {
98+ expect(parseInput("\x1b[B\r")).toEqual([
99+ { type: "down" },
100+ { type: "enter" },
101+ ]);
102+ });
103+
104+ test("ESC followed by non-bracket is ESC + char", () => {
105+ expect(parseInput("\x1ba")).toEqual([
106+ { type: "escape" },
107+ { type: "char", char: "a" },
108+ ]);
109+ });
110+
111+ test("unknown escape sequence is skipped", () => {
112+ // \x1b[C is arrow right — not handled, should be skipped
113+ expect(parseInput("\x1b[Ca")).toEqual([{ type: "char", char: "a" }]);
114+ });
115+
116+ test("empty string returns empty array", () => {
117+ expect(parseInput("")).toEqual([]);
118+ });
119+});
src/input.ts+80−0
@@ -0,0 +1,80 @@
1+export type InputEvent =
2+ | { type: "char"; char: string }
3+ | { type: "escape" }
4+ | { type: "enter" }
5+ | { type: "backspace" }
6+ | { type: "up" }
7+ | { type: "down" }
8+ | { type: "home" }
9+ | { type: "end" };
10+
11+const CSI_SEQUENCES: Record<string, InputEvent> = {
12+ A: { type: "up" },
13+ B: { type: "down" },
14+ H: { type: "home" },
15+ F: { type: "end" },
16+ "1~": { type: "home" },
17+ "4~": { type: "end" },
18+};
19+
20+export function parseInput(data: string): InputEvent[] {
21+ const events: InputEvent[] = [];
22+ let i = 0;
23+
24+ while (i < data.length) {
25+ const ch = data[i] as string;
26+
27+ if (ch === "\r" || ch === "\n") {
28+ events.push({ type: "enter" });
29+ i++;
30+ continue;
31+ }
32+
33+ if (ch === "\x7f" || ch === "\x08") {
34+ events.push({ type: "backspace" });
35+ i++;
36+ continue;
37+ }
38+
39+ if (ch === "\x1b") {
40+ // Check for CSI sequence: ESC [
41+ if (data[i + 1] === "[") {
42+ // Try 4-char sequences first (e.g. \x1b[1~ or \x1b[4~)
43+ const fourChar = data.slice(i + 2, i + 4);
44+ const event4 = CSI_SEQUENCES[fourChar];
45+ if (event4) {
46+ events.push({ ...event4 });
47+ i += 4;
48+ continue;
49+ }
50+
51+ // Try 3-char sequences (e.g. \x1b[A)
52+ const oneChar = data[i + 2];
53+ const event3 = oneChar ? CSI_SEQUENCES[oneChar] : undefined;
54+ if (event3) {
55+ events.push({ ...event3 });
56+ i += 3;
57+ continue;
58+ }
59+
60+ // Unknown CSI sequence — skip ESC [ and the next char
61+ i += 3;
62+ continue;
63+ }
64+
65+ // Bare ESC (not followed by '[')
66+ events.push({ type: "escape" });
67+ i++;
68+ continue;
69+ }
70+
71+ // Printable characters
72+ if (ch >= " ") {
73+ events.push({ type: "char", char: ch });
74+ }
75+ // Control chars we don't handle are silently skipped
76+ i++;
77+ }
78+
79+ return events;
80+}
src/select.ts+52−61
@@ -2,6 +2,7 @@ import * as c from "yoctocolors";
22 import { clearDown, cursorUp, hideCursor, showCursor, write } from "./ansi.ts";
33 import { type NormalizedItem, normalizeItems, prefixFilter } from "./filter.ts";
44 import type { SelectOptions } from "./types.ts";
5+import { type InputEvent, parseInput } from "./input.ts";
56 import { adjustScroll, moveDown, moveUp } from "./viewport.ts";
67
78 const DEFAULT_MAX_VISIBLE = 20;
@@ -89,77 +90,67 @@ export async function select(opts: SelectOptions): Promise<string | null> {
8990 resolve(result);
9091 }
9192
92- function onData(buf: Buffer): void {
93- const key = buf.toString();
93+ function handleEvent(event: InputEvent): boolean {
94+ switch (event.type) {
95+ case "escape":
96+ cleanup(null);
97+ return true;
98+
99+ case "enter": {
100+ const filtered = getFiltered();
101+ const selected = filtered[highlightIndex];
102+ cleanup(selected?.value ?? null);
103+ return true;
104+ }
94105
95- // ESC (but not escape sequence)
96- if (key === "\x1b") {
97- cleanup(null);
98- return;
99- }
106+ case "backspace":
107+ if (query.length > 0) {
108+ query = query.slice(0, -1);
109+ highlightIndex = 0;
110+ scrollOffset = 0;
111+ }
112+ return false;
113+
114+ case "up": {
115+ const filtered = getFiltered();
116+ highlightIndex = moveUp(highlightIndex, filtered.length);
117+ updateScroll();
118+ return false;
119+ }
100120
101- // Enter
102- if (key === "\r" || key === "\n") {
103- const filtered = getFiltered();
104- const selected = filtered[highlightIndex];
105- cleanup(selected?.value ?? null);
106- return;
107- }
121+ case "down": {
122+ const filtered = getFiltered();
123+ highlightIndex = moveDown(highlightIndex, filtered.length);
124+ updateScroll();
125+ return false;
126+ }
108127
109- // Backspace
110- if (key === "\x7f" || key === "\x08") {
111- if (query.length > 0) {
112- query = query.slice(0, -1);
128+ case "home":
113129 highlightIndex = 0;
114130 scrollOffset = 0;
115- }
116- render();
117- return;
118- }
119-
120- // Arrow up
121- if (key === "\x1b[A") {
122- const filtered = getFiltered();
123- highlightIndex = moveUp(highlightIndex, filtered.length);
124- updateScroll();
125- render();
126- return;
127- }
128-
129- // Arrow down
130- if (key === "\x1b[B") {
131- const filtered = getFiltered();
132- highlightIndex = moveDown(highlightIndex, filtered.length);
133- updateScroll();
134- render();
135- return;
136- }
131+ return false;
137132
138- // Home
139- if (key === "\x1b[H" || key === "\x1b[1~") {
140- highlightIndex = 0;
141- scrollOffset = 0;
142- render();
143- return;
144- }
133+ case "end": {
134+ const filtered = getFiltered();
135+ highlightIndex = Math.max(0, filtered.length - 1);
136+ updateScroll();
137+ return false;
138+ }
145139
146- // End
147- if (key === "\x1b[F" || key === "\x1b[4~") {
148- const filtered = getFiltered();
149- highlightIndex = Math.max(0, filtered.length - 1);
150- updateScroll();
151- render();
152- return;
140+ case "char":
141+ query += event.char;
142+ highlightIndex = 0;
143+ scrollOffset = 0;
144+ return false;
153145 }
146+ }
154147
155- // Printable characters
156- if (key.length === 1 && key >= " ") {
157- query += key;
158- highlightIndex = 0;
159- scrollOffset = 0;
160- render();
161- return;
148+ function onData(buf: Buffer): void {
149+ const events = parseInput(buf.toString());
150+ for (const event of events) {
151+ if (handleEvent(event)) return;
162152 }
153+ render();
163154 }
164155
165156 stdin.on("data", onData);