forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.ts
More file actions
316 lines (263 loc) · 9.18 KB
/
Copy pathselect.ts
File metadata and controls
316 lines (263 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { ANSI, isTTY, parseKey } from './ansi';
export interface MenuItem<T = string> {
label: string;
value: T;
hint?: string;
disabled?: boolean;
separator?: boolean;
/** Non-selectable label row (section heading). */
kind?: 'heading';
color?: 'red' | 'green' | 'yellow' | 'cyan';
}
export interface SelectOptions {
message: string;
subtitle?: string;
/** Override the help line shown at the bottom of the menu. */
help?: string;
/**
* Clear the terminal before each render (opt-in).
* Useful for nested flows where previous logs make menus feel cluttered.
*/
clearScreen?: boolean;
}
const ESCAPE_TIMEOUT_MS = 50;
const ANSI_REGEX = new RegExp("\\x1b\\[[0-9;]*m", "g");
const ANSI_LEADING_REGEX = new RegExp("^\\x1b\\[[0-9;]*m");
function stripAnsi(input: string): string {
return input.replace(ANSI_REGEX, '');
}
function truncateAnsi(input: string, maxVisibleChars: number): string {
if (maxVisibleChars <= 0) return '';
const visible = stripAnsi(input);
if (visible.length <= maxVisibleChars) return input;
const suffix = maxVisibleChars >= 3 ? '...' : '.'.repeat(maxVisibleChars);
const keep = Math.max(0, maxVisibleChars - suffix.length);
let out = '';
let i = 0;
let kept = 0;
while (i < input.length && kept < keep) {
// Preserve ANSI sequences without counting them.
if (input[i] === '\x1b') {
const m = input.slice(i).match(ANSI_LEADING_REGEX);
if (m) {
out += m[0];
i += m[0].length;
continue;
}
}
out += input[i];
i += 1;
kept += 1;
}
if (out.includes('\x1b[')) {
return `${out}${ANSI.reset}${suffix}`;
}
return out + suffix;
}
function getColorCode(color: MenuItem['color']): string {
switch (color) {
case 'red': return ANSI.red;
case 'green': return ANSI.green;
case 'yellow': return ANSI.yellow;
case 'cyan': return ANSI.cyan;
default: return '';
}
}
export async function select<T>(
items: MenuItem<T>[],
options: SelectOptions
): Promise<T | null> {
if (!isTTY()) {
throw new Error('Interactive select requires a TTY terminal');
}
if (items.length === 0) {
throw new Error('No menu items provided');
}
const isSelectable = (i: MenuItem<T>) => !i.disabled && !i.separator && i.kind !== 'heading';
const enabledItems = items.filter(isSelectable);
if (enabledItems.length === 0) {
throw new Error('All items disabled');
}
if (enabledItems.length === 1) {
return enabledItems[0]!.value;
}
const { message, subtitle } = options;
const { stdin, stdout } = process;
let cursor = items.findIndex(isSelectable);
if (cursor === -1) cursor = 0; // Fallback, though validation above should prevent this
let escapeTimeout: ReturnType<typeof setTimeout> | null = null;
let isCleanedUp = false;
let renderedLines = 0;
const render = () => {
const columns = stdout.columns ?? 80;
const rows = stdout.rows ?? 24;
const shouldClearScreen = options.clearScreen === true;
const previousRenderedLines = renderedLines;
if (shouldClearScreen) {
stdout.write(ANSI.clearScreen + ANSI.moveTo(1, 1));
} else if (previousRenderedLines > 0) {
stdout.write(ANSI.up(previousRenderedLines));
}
let linesWritten = 0;
const writeLine = (line: string) => {
stdout.write(`${ANSI.clearLine}${line}\n`);
linesWritten += 1;
};
// Subtitle renders as 3 lines:
// 1) blank "│" spacer, 2) subtitle line, 3) blank line. Header is counted separately.
const subtitleLines = subtitle ? 3 : 0;
const fixedLines = 1 + subtitleLines + 2; // header + subtitle + (help + bottom)
// Keep a small safety margin so the final newline doesn't scroll the terminal.
const maxVisibleItems = Math.max(1, Math.min(items.length, rows - fixedLines - 1));
// If the menu is taller than the viewport, only render a window around the cursor.
// This prevents terminal scrollback spam (e.g. repeated headers when pressing arrows).
let windowStart = 0;
let windowEnd = items.length;
if (items.length > maxVisibleItems) {
windowStart = cursor - Math.floor(maxVisibleItems / 2);
windowStart = Math.max(0, Math.min(windowStart, items.length - maxVisibleItems));
windowEnd = windowStart + maxVisibleItems;
}
const visibleItems = items.slice(windowStart, windowEnd);
const headerMessage = truncateAnsi(message, Math.max(1, columns - 4));
writeLine(`${ANSI.dim}┌ ${ANSI.reset}${headerMessage}`);
if (subtitle) {
writeLine(`${ANSI.dim}│${ANSI.reset}`);
const sub = truncateAnsi(subtitle, Math.max(1, columns - 4));
writeLine(`${ANSI.cyan}◆${ANSI.reset} ${sub}`);
writeLine("");
}
for (let i = 0; i < visibleItems.length; i++) {
const itemIndex = windowStart + i;
const item = visibleItems[i];
if (!item) continue;
if (item.separator) {
writeLine(`${ANSI.dim}│${ANSI.reset}`);
continue;
}
if (item.kind === 'heading') {
const heading = truncateAnsi(`${ANSI.dim}${ANSI.bold}${item.label}${ANSI.reset}`, Math.max(1, columns - 6));
writeLine(`${ANSI.cyan}│${ANSI.reset} ${heading}`);
continue;
}
const isSelected = itemIndex === cursor;
const colorCode = getColorCode(item.color);
let labelText: string;
if (item.disabled) {
labelText = `${ANSI.dim}${item.label} (unavailable)${ANSI.reset}`;
} else if (isSelected) {
labelText = colorCode ? `${colorCode}${item.label}${ANSI.reset}` : item.label;
if (item.hint) labelText += ` ${ANSI.dim}${item.hint}${ANSI.reset}`;
} else {
labelText = colorCode
? `${ANSI.dim}${colorCode}${item.label}${ANSI.reset}`
: `${ANSI.dim}${item.label}${ANSI.reset}`;
if (item.hint) labelText += ` ${ANSI.dim}${item.hint}${ANSI.reset}`;
}
// Prevent wrapping: cursor positioning relies on a fixed line count.
labelText = truncateAnsi(labelText, Math.max(1, columns - 8));
if (isSelected) {
writeLine(`${ANSI.cyan}│${ANSI.reset} ${ANSI.green}●${ANSI.reset} ${labelText}`);
} else {
writeLine(`${ANSI.cyan}│${ANSI.reset} ${ANSI.dim}○${ANSI.reset} ${labelText}`);
}
}
const windowHint = items.length > visibleItems.length
? ` (${windowStart + 1}-${windowEnd}/${items.length})`
: '';
const helpText = options.help ?? `Up/Down to select | Enter: confirm | Esc: back${windowHint}`;
const help = truncateAnsi(helpText, Math.max(1, columns - 6));
writeLine(`${ANSI.cyan}│${ANSI.reset} ${ANSI.dim}${help}${ANSI.reset}`);
writeLine(`${ANSI.cyan}└${ANSI.reset}`);
if (!shouldClearScreen && previousRenderedLines > linesWritten) {
const extra = previousRenderedLines - linesWritten;
for (let i = 0; i < extra; i++) {
writeLine("");
}
}
renderedLines = linesWritten;
};
return new Promise((resolve) => {
const wasRaw = stdin.isRaw ?? false;
const cleanup = () => {
if (isCleanedUp) return;
isCleanedUp = true;
if (escapeTimeout) {
clearTimeout(escapeTimeout);
escapeTimeout = null;
}
try {
stdin.removeListener('data', onKey);
stdin.setRawMode(wasRaw);
stdin.pause();
stdout.write(ANSI.show);
} catch {
// Intentionally ignored - cleanup is best-effort
}
process.removeListener('SIGINT', onSignal);
process.removeListener('SIGTERM', onSignal);
};
const onSignal = () => {
cleanup();
resolve(null);
};
const finishWithValue = (value: T | null) => {
cleanup();
resolve(value);
};
const findNextSelectable = (from: number, direction: 1 | -1): number => {
if (items.length === 0) return from;
let next = from;
do {
next = (next + direction + items.length) % items.length;
} while (items[next]?.disabled || items[next]?.separator || items[next]?.kind === 'heading');
return next;
};
const onKey = (data: Buffer) => {
if (escapeTimeout) {
clearTimeout(escapeTimeout);
escapeTimeout = null;
}
const action = parseKey(data);
switch (action) {
case 'up':
cursor = findNextSelectable(cursor, -1);
render();
return;
case 'down':
cursor = findNextSelectable(cursor, 1);
render();
return;
case 'enter':
finishWithValue(items[cursor]?.value ?? null);
return;
case 'escape':
finishWithValue(null);
return;
case 'escape-start':
// Bare escape byte - wait to see if more bytes coming (arrow key sequence)
escapeTimeout = setTimeout(() => {
finishWithValue(null);
}, ESCAPE_TIMEOUT_MS);
return;
default:
// Unknown key - ignore
return;
}
};
process.once('SIGINT', onSignal);
process.once('SIGTERM', onSignal);
try {
stdin.setRawMode(true);
} catch {
// Failed to enable raw mode - cleanup and return null
cleanup();
resolve(null);
return;
}
stdin.resume();
stdout.write(ANSI.hide);
render();
stdin.on('data', onKey);
});
}