xonecas.io
6615
Yc

yoctoselect / commit 64d4bfb

feat: select() interactive list picker

xonecas2026-03-21 64d4bfb326302267404ad753ed7a3d22d4da3679 parent edeea31

3 files changed

knip.json+1−1
@@ -1 +1 @@
1-{ "ignoreBinaries": ["tsc"], "ignoreDependencies": ["yoctocolors"] }
1+{ "ignoreBinaries": ["tsc"] }
src/filter.test.ts+1−1
@@ -21,7 +21,7 @@ describe("normalizeItems", () => {
2121 const result = normalizeItems([
2222 { label: "colored", value: "v", filterText: "plain" },
2323 ]);
24- expect(result[0]!.filterText).toBe("plain");
24+ expect(result[0]?.filterText).toBe("plain");
2525 });
2626 });
2727
src/select.ts+165−2
@@ -1,5 +1,168 @@
1+import * as c from "yoctocolors";
2+import { clearDown, cursorUp, hideCursor, showCursor, write } from "./ansi.ts";
3+import { type NormalizedItem, normalizeItems, prefixFilter } from "./filter.ts";
14 import type { SelectOptions } from "./types.ts";
25
3-export async function select(_opts: SelectOptions): Promise<string | null> {
4- return null;
6+const DEFAULT_MAX_VISIBLE = 20;
7+const DEFAULT_PLACEHOLDER = "type to filter...";
8+
9+export async function select(opts: SelectOptions): Promise<string | null> {
10+ const allItems = normalizeItems(opts.items);
11+ const maxVisible = opts.maxVisible ?? DEFAULT_MAX_VISIBLE;
12+ const placeholder = opts.placeholder ?? DEFAULT_PLACEHOLDER;
13+
14+ let query = "";
15+ let highlightIndex = 0;
16+ let renderedLines = 0;
17+
18+ function getFiltered(): NormalizedItem[] {
19+ return prefixFilter(allItems, query);
20+ }
21+
22+ function render(): void {
23+ // Move up and clear previous render
24+ if (renderedLines > 0) {
25+ write(cursorUp(renderedLines) + clearDown());
26+ }
27+
28+ const filtered = getFiltered();
29+ const visible = filtered.slice(0, maxVisible);
30+ const overflow = filtered.length - visible.length;
31+
32+ // Search line
33+ const searchText = query || c.dim(placeholder);
34+ write(` ${c.dim("search:")} ${searchText}\n`);
35+
36+ // Items
37+ if (filtered.length === 0) {
38+ write(` ${c.dim("no matches")}\n`);
39+ renderedLines = 2;
40+ return;
41+ }
42+
43+ for (let i = 0; i < visible.length; i++) {
44+ const item = visible[i];
45+ if (!item) continue;
46+ if (i === highlightIndex) {
47+ write(` ${c.cyan("›")} ${c.cyan(item.label)}\n`);
48+ } else {
49+ write(` ${item.label}\n`);
50+ }
51+ }
52+
53+ if (overflow > 0) {
54+ write(` ${c.dim(`${overflow} more`)}\n`);
55+ }
56+
57+ renderedLines = 1 + visible.length + (overflow > 0 ? 1 : 0);
58+ }
59+
60+ function clear(): void {
61+ if (renderedLines > 0) {
62+ write(cursorUp(renderedLines) + clearDown());
63+ renderedLines = 0;
64+ }
65+ }
66+
67+ function clampHighlight(filtered: NormalizedItem[]): void {
68+ const max = Math.min(filtered.length, maxVisible) - 1;
69+ if (highlightIndex > max) highlightIndex = Math.max(0, max);
70+ }
71+
72+ return new Promise<string | null>((resolve) => {
73+ const stdin = process.stdin;
74+ const wasRaw = stdin.isRaw;
75+
76+ stdin.setRawMode(true);
77+ stdin.resume();
78+ write(hideCursor());
79+ render();
80+
81+ function cleanup(result: string | null): void {
82+ stdin.removeListener("data", onData);
83+ stdin.setRawMode(wasRaw);
84+ stdin.pause();
85+ clear();
86+ write(showCursor());
87+ resolve(result);
88+ }
89+
90+ function onData(buf: Buffer): void {
91+ const key = buf.toString();
92+
93+ // ESC (but not escape sequence)
94+ if (key === "\x1b") {
95+ cleanup(null);
96+ return;
97+ }
98+
99+ // Enter
100+ if (key === "\r" || key === "\n") {
101+ const filtered = getFiltered();
102+ const visible = filtered.slice(0, maxVisible);
103+ const selected = visible[highlightIndex];
104+ cleanup(selected?.value ?? null);
105+ return;
106+ }
107+
108+ // Backspace
109+ if (key === "\x7f" || key === "\x08") {
110+ if (query.length > 0) {
111+ query = query.slice(0, -1);
112+ highlightIndex = 0;
113+ }
114+ render();
115+ return;
116+ }
117+
118+ // Arrow up
119+ if (key === "\x1b[A") {
120+ const filtered = getFiltered();
121+ const max = Math.min(filtered.length, maxVisible);
122+ if (max > 0) {
123+ highlightIndex = highlightIndex <= 0 ? max - 1 : highlightIndex - 1;
124+ }
125+ render();
126+ return;
127+ }
128+
129+ // Arrow down
130+ if (key === "\x1b[B") {
131+ const filtered = getFiltered();
132+ const max = Math.min(filtered.length, maxVisible);
133+ if (max > 0) {
134+ highlightIndex = highlightIndex >= max - 1 ? 0 : highlightIndex + 1;
135+ }
136+ render();
137+ return;
138+ }
139+
140+ // Home
141+ if (key === "\x1b[H" || key === "\x1b[1~") {
142+ highlightIndex = 0;
143+ render();
144+ return;
145+ }
146+
147+ // End
148+ if (key === "\x1b[F" || key === "\x1b[4~") {
149+ const filtered = getFiltered();
150+ const max = Math.min(filtered.length, maxVisible);
151+ highlightIndex = Math.max(0, max - 1);
152+ render();
153+ return;
154+ }
155+
156+ // Printable characters
157+ if (key.length === 1 && key >= " ") {
158+ query += key;
159+ highlightIndex = 0;
160+ clampHighlight(getFiltered());
161+ render();
162+ return;
163+ }
164+ }
165+
166+ stdin.on("data", onData);
167+ });
5168 }