@k3_2o/pi-ask-user-question 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/index.ts +8 -0
- package/package.json +53 -5
- package/src/dialog.ts +676 -0
- package/src/normalize.ts +148 -0
- package/src/respond.ts +46 -0
- package/src/result-interpretation.ts +20 -0
- package/src/schema.ts +31 -0
- package/src/text-layout.ts +76 -0
- package/src/tool.ts +173 -0
- package/src/transcript.ts +305 -0
- package/src/types.ts +41 -0
- package/src/validate.ts +62 -0
package/src/dialog.ts
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
import type { Theme } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
import {
|
|
3
|
+
Input,
|
|
4
|
+
Key,
|
|
5
|
+
matchesKey,
|
|
6
|
+
truncateToWidth,
|
|
7
|
+
type Component,
|
|
8
|
+
type TUI,
|
|
9
|
+
visibleWidth,
|
|
10
|
+
} from '@earendil-works/pi-tui';
|
|
11
|
+
import { wordWrap } from './text-layout';
|
|
12
|
+
import type { CanonicalQuestion, DialogResult, ResultItem } from './types';
|
|
13
|
+
import { OTHER_LABEL, RECOMMENDED_SUFFIX, displayOptionLabels, sanitizeForDisplay } from './normalize';
|
|
14
|
+
|
|
15
|
+
interface PerQuestionState {
|
|
16
|
+
selected: Set<string>;
|
|
17
|
+
customInput?: string;
|
|
18
|
+
cursor: number;
|
|
19
|
+
scrollOffset: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface RowOption {
|
|
23
|
+
kind: 'option';
|
|
24
|
+
index: number;
|
|
25
|
+
label: string;
|
|
26
|
+
value: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
recommended: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface RowOther {
|
|
32
|
+
kind: 'other';
|
|
33
|
+
label: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type DialogRow = RowOption | RowOther;
|
|
37
|
+
|
|
38
|
+
const ELLIPSIS: TabToken = { text: '\u2026', current: false };
|
|
39
|
+
|
|
40
|
+
interface TabToken {
|
|
41
|
+
text: string;
|
|
42
|
+
current: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const SUBMIT_TAB_LABEL = 'Submit';
|
|
46
|
+
const QUESTIONS_LABEL = 'Questions';
|
|
47
|
+
const TAB_LABEL_MAX_WIDTH = 24;
|
|
48
|
+
|
|
49
|
+
const DESCRIPTION_GUTTER = ' ';
|
|
50
|
+
|
|
51
|
+
function isCtrlCPress(data: string): boolean {
|
|
52
|
+
return data === '\u0003' || data === '\u001b[99;5u';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function descriptionBudget(width: number): number {
|
|
56
|
+
return Math.max(1, width - DESCRIPTION_GUTTER.length);
|
|
57
|
+
}
|
|
58
|
+
const MAX_DESC_ROWS = 2;
|
|
59
|
+
const MAX_QUESTION_ROWS = 4;
|
|
60
|
+
|
|
61
|
+
interface ComponentOptions {
|
|
62
|
+
tui: TUI;
|
|
63
|
+
theme: Theme;
|
|
64
|
+
signal?: AbortSignal;
|
|
65
|
+
done: (result: DialogResult | null) => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function componentFor(
|
|
69
|
+
questions: CanonicalQuestion[],
|
|
70
|
+
options: ComponentOptions,
|
|
71
|
+
): Component & { dispose(): void } {
|
|
72
|
+
const { theme, tui, done, signal } = options;
|
|
73
|
+
|
|
74
|
+
const states: PerQuestionState[] = questions.map((question) => {
|
|
75
|
+
const recommended =
|
|
76
|
+
typeof question.recommended === 'number' && Number.isInteger(question.recommended) ? question.recommended : -1;
|
|
77
|
+
const lastOption = Math.max(0, question.options.length - 1);
|
|
78
|
+
return {
|
|
79
|
+
selected: new Set<string>(),
|
|
80
|
+
cursor: Math.min(Math.max(recommended, 0), lastOption),
|
|
81
|
+
scrollOffset: 0,
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
let questionIndex = 0;
|
|
86
|
+
let mode: 'options' | 'custom' = 'options';
|
|
87
|
+
|
|
88
|
+
const customInput = new Input({
|
|
89
|
+
prompt: '',
|
|
90
|
+
placeholder: 'Type your own answer',
|
|
91
|
+
placeholderStyle: (text: string) => theme.fg('dim', text),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function rowsFor(index: number): DialogRow[] {
|
|
95
|
+
const question = questions[index];
|
|
96
|
+
if (!question) return [];
|
|
97
|
+
const display = displayOptionLabels(question.options, question.recommended);
|
|
98
|
+
const rows: DialogRow[] = question.options.map((option, i) => ({
|
|
99
|
+
kind: 'option',
|
|
100
|
+
index: i,
|
|
101
|
+
label: display[i] ?? sanitizeForDisplay(option.label),
|
|
102
|
+
value: option.value,
|
|
103
|
+
description: option.description,
|
|
104
|
+
recommended: question.recommended === i,
|
|
105
|
+
}));
|
|
106
|
+
rows.push({ kind: 'other', label: OTHER_LABEL });
|
|
107
|
+
return rows;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function rowChecked(state: PerQuestionState, row: DialogRow): boolean {
|
|
111
|
+
if (row.kind === 'other') return state.customInput !== undefined;
|
|
112
|
+
if (row.kind === 'option') return state.selected.has(row.value);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function advance(): void {
|
|
117
|
+
if (questionIndex < lastNavigableIndex()) {
|
|
118
|
+
questionIndex += 1;
|
|
119
|
+
tui.requestRender();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
finish();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function finishCancel(): void {
|
|
126
|
+
signal?.removeEventListener('abort', onAbort);
|
|
127
|
+
done(null);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function finish(): void {
|
|
131
|
+
signal?.removeEventListener('abort', onAbort);
|
|
132
|
+
done(buildResult());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const onAbort = () => finishCancel();
|
|
136
|
+
|
|
137
|
+
const isReviewPage = (): boolean => questions.length > 1 && questionIndex === questions.length;
|
|
138
|
+
|
|
139
|
+
function lastNavigableIndex(): number {
|
|
140
|
+
return questions.length > 1 ? questions.length : questions.length - 1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function reviewSelectionText(index: number): string {
|
|
144
|
+
const question = questions[index];
|
|
145
|
+
const state = states[index];
|
|
146
|
+
if (!question || !state) return '(no answer)';
|
|
147
|
+
if (state.customInput !== undefined) {
|
|
148
|
+
const preview = state.customInput.replace(/\s+/g, ' ').trim();
|
|
149
|
+
return preview ? `✎ ${preview}` : '(no answer)';
|
|
150
|
+
}
|
|
151
|
+
if (state.selected.size > 0) {
|
|
152
|
+
const labels = question.options
|
|
153
|
+
.filter((option) => state.selected.has(option.value))
|
|
154
|
+
.map((option) => option.label);
|
|
155
|
+
return labels.length > 0 ? labels.join(', ') : '(no answer)';
|
|
156
|
+
}
|
|
157
|
+
return '(no answer)';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function reviewPageLines(inner: number): string[] {
|
|
161
|
+
const lines: string[] = [];
|
|
162
|
+
questions.forEach((question, index) => {
|
|
163
|
+
const answered = questionAnswered(index);
|
|
164
|
+
const marker = answered ? '\u25c9' : '\u25cb';
|
|
165
|
+
const label = question.header?.trim() || question.question;
|
|
166
|
+
const choice = reviewSelectionText(index);
|
|
167
|
+
const styledMarker = answered ? theme.fg('accent', marker) : theme.fg('dim', marker);
|
|
168
|
+
const styledLabel = theme.fg(answered ? 'text' : 'muted', label);
|
|
169
|
+
lines.push(` ${styledMarker} ${styledLabel}`);
|
|
170
|
+
lines.push(` ${theme.fg('accent', choice)}`);
|
|
171
|
+
});
|
|
172
|
+
lines.push('');
|
|
173
|
+
lines.push(` ${theme.fg('text', 'Enter')} ${theme.fg('dim', 'to submit · ← to change an answer')}`);
|
|
174
|
+
return lines.map((line) => truncateToWidth(line, inner + 4));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function questionAnswered(index: number): boolean {
|
|
178
|
+
const state = states[index];
|
|
179
|
+
if (!state) return false;
|
|
180
|
+
return state.selected.size > 0 || state.customInput !== undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function tabLabels(): string[] {
|
|
184
|
+
return questions.map((question, index) =>
|
|
185
|
+
truncateToWidth(sanitizeForDisplay(question.header ?? `Field ${index + 1}`), TAB_LABEL_MAX_WIDTH),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function styledTab(labels: string[], index: number, shown: string): string {
|
|
190
|
+
const isSubmit = index === labels.length;
|
|
191
|
+
const answered = !isSubmit && questionAnswered(index);
|
|
192
|
+
const text = answered ? `${shown}\u2713` : shown;
|
|
193
|
+
const active = isSubmit ? questionIndex === questions.length : index === questionIndex;
|
|
194
|
+
return active ? theme.fg('accent', theme.bold(text)) : theme.fg('dim', text);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function labeledStrip(labels: string[], shown: (label: string, index: number) => string): string {
|
|
198
|
+
return labels
|
|
199
|
+
.map((label, index) => styledTab(labels, index, shown(label, index)))
|
|
200
|
+
.concat(styledTab(labels, labels.length, SUBMIT_TAB_LABEL))
|
|
201
|
+
.join(' ');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function tabStrip(width: number): string {
|
|
205
|
+
const indent = ' ';
|
|
206
|
+
const budget = Math.max(1, width - 4);
|
|
207
|
+
const labels = tabLabels();
|
|
208
|
+
const full = labeledStrip(labels, (label) => label);
|
|
209
|
+
if (visibleWidth(full) <= budget) return `${indent}${full}`;
|
|
210
|
+
|
|
211
|
+
const isSubmit = (index: number) => index === labels.length;
|
|
212
|
+
const longest = Math.max(...labels.map((label) => label.length), SUBMIT_TAB_LABEL.length);
|
|
213
|
+
for (let allowance = longest; allowance >= 2; allowance--) {
|
|
214
|
+
const candidate = labeledStrip(labels, (label, index) =>
|
|
215
|
+
isSubmit(index) ? label : truncateToWidth(label, allowance),
|
|
216
|
+
);
|
|
217
|
+
if (visibleWidth(candidate) <= budget) {
|
|
218
|
+
return `${indent}${candidate}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return collapsedStrip(budget);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function collapsedStrip(budget: number): string {
|
|
225
|
+
const tabs: TabToken[] = questions.map((_question, index) => ({
|
|
226
|
+
text: `${index + 1}${questionAnswered(index) ? '\u2713' : ''}`,
|
|
227
|
+
current: index === questionIndex,
|
|
228
|
+
}));
|
|
229
|
+
const joinTabs = (list: TabToken[]): string =>
|
|
230
|
+
list
|
|
231
|
+
.map((tab) => (tab.current ? theme.fg('accent', theme.bold(tab.text)) : theme.fg('dim', tab.text)))
|
|
232
|
+
.join(' ');
|
|
233
|
+
const widthOf = (list: TabToken[]) =>
|
|
234
|
+
list.reduce((sum, tab, index) => sum + visibleWidth(tab.text) + (index === 0 ? 0 : 1), 0);
|
|
235
|
+
if (widthOf(tabs) <= budget) return ` ${joinTabs(tabs)}`;
|
|
236
|
+
|
|
237
|
+
const windowStart = Math.max(0, questionIndex - 1);
|
|
238
|
+
const windowEnd = Math.min(questions.length, questionIndex + 2);
|
|
239
|
+
const parts: TabToken[] = [];
|
|
240
|
+
const firstTab = tabs[0];
|
|
241
|
+
if (windowStart > 0 && firstTab) parts.push(firstTab);
|
|
242
|
+
if (windowStart > 1) parts.push(ELLIPSIS);
|
|
243
|
+
parts.push(...tabs.slice(windowStart, windowEnd));
|
|
244
|
+
if (windowEnd < questions.length - 1) parts.push(ELLIPSIS);
|
|
245
|
+
const lastTab = tabs[tabs.length - 1];
|
|
246
|
+
if (windowEnd < questions.length && lastTab) parts.push(lastTab);
|
|
247
|
+
if (widthOf(parts) <= budget) return ` ${joinTabs(parts)}`;
|
|
248
|
+
|
|
249
|
+
const current = tabs[questionIndex];
|
|
250
|
+
return ` ${theme.fg('accent', theme.bold(current?.text ?? ''))} / ${questions.length}`;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function buildResult(): DialogResult {
|
|
254
|
+
const results: ResultItem[] = questions.map((question, index) => {
|
|
255
|
+
const state = states[index];
|
|
256
|
+
const selected: string[] = [];
|
|
257
|
+
const selectedLabels: string[] = [];
|
|
258
|
+
for (const value of question.options.map((option) => option.value)) {
|
|
259
|
+
if (state?.selected.has(value)) {
|
|
260
|
+
const option = question.options.find((candidate) => candidate.value === value);
|
|
261
|
+
selected.push(value);
|
|
262
|
+
if (option) selectedLabels.push(option.label);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const answered = questionAnswered(index);
|
|
266
|
+
const item: ResultItem = {
|
|
267
|
+
id: question.id,
|
|
268
|
+
question: question.question,
|
|
269
|
+
options: question.options.map((option) => ({
|
|
270
|
+
label: option.label,
|
|
271
|
+
value: option.value,
|
|
272
|
+
...(option.description ? { description: option.description } : {}),
|
|
273
|
+
})),
|
|
274
|
+
multi: question.multi,
|
|
275
|
+
selected,
|
|
276
|
+
selectedLabels,
|
|
277
|
+
customInput: state?.customInput,
|
|
278
|
+
...(!answered ? { unanswered: true } : {}),
|
|
279
|
+
};
|
|
280
|
+
if (question.header) item.header = question.header;
|
|
281
|
+
if (question.recommended !== undefined) item.recommended = question.recommended;
|
|
282
|
+
return item;
|
|
283
|
+
});
|
|
284
|
+
return { results };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// The Input eats ctrl+c as tui.select.cancel before our handler sees it, so
|
|
288
|
+
// ctrl+c is intercepted by raw byte here; Esc stays distinguishable.
|
|
289
|
+
const isCtrlCData = isCtrlCPress;
|
|
290
|
+
|
|
291
|
+
// Keyboard sends this for the End key; pi-tui maps it to cursorLineEnd.
|
|
292
|
+
const END_KEY = '\u001b[F';
|
|
293
|
+
|
|
294
|
+
function openCustomInput(): void {
|
|
295
|
+
const state = states[questionIndex];
|
|
296
|
+
if (!state) return;
|
|
297
|
+
mode = 'custom';
|
|
298
|
+
customInput.focused = true;
|
|
299
|
+
customInput.setValue(state.customInput ?? '');
|
|
300
|
+
customInput.handleInput(END_KEY);
|
|
301
|
+
refresh();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function commitCustomAnswer(): void {
|
|
305
|
+
const state = states[questionIndex];
|
|
306
|
+
const value = customInput.getValue().trim();
|
|
307
|
+
if (state && value) {
|
|
308
|
+
state.customInput = value;
|
|
309
|
+
state.selected.clear();
|
|
310
|
+
leaveInput();
|
|
311
|
+
advance();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
leaveInput();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function clearCustomAnswer(): void {
|
|
318
|
+
customInput.setValue('');
|
|
319
|
+
const state = states[questionIndex];
|
|
320
|
+
if (state) state.customInput = undefined;
|
|
321
|
+
refresh();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function deactivateInput(): void {
|
|
325
|
+
const state = states[questionIndex];
|
|
326
|
+
const draft = customInput.getValue().trim();
|
|
327
|
+
if (state && draft) state.customInput = draft;
|
|
328
|
+
customInput.focused = false;
|
|
329
|
+
customInput.setValue(state?.customInput ?? '');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function leaveInput(): void {
|
|
333
|
+
deactivateInput();
|
|
334
|
+
mode = 'options';
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function navigate(direction: 1 | -1): void {
|
|
338
|
+
const faceCount = lastNavigableIndex() + 1;
|
|
339
|
+
questionIndex = (questionIndex + direction + faceCount) % faceCount;
|
|
340
|
+
refresh();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function moveCursor(delta: 1 | -1): void {
|
|
344
|
+
const state = states[questionIndex];
|
|
345
|
+
if (!state) return;
|
|
346
|
+
const rowCount = rowsFor(questionIndex).length;
|
|
347
|
+
state.cursor = (state.cursor + delta + rowCount) % rowCount;
|
|
348
|
+
refresh();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function handleInput(data: string): void {
|
|
352
|
+
if (mode === 'custom') {
|
|
353
|
+
if (isCtrlCData(data)) {
|
|
354
|
+
clearCustomAnswer();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (matchesKey(data, Key.escape)) {
|
|
358
|
+
leaveInput();
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const isEnter = matchesKey(data, Key.enter) || matchesKey(data, Key.return) || data === '\n';
|
|
362
|
+
if (isEnter) {
|
|
363
|
+
commitCustomAnswer();
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
// Navigation keys always leave the input; the draft is kept, never committed.
|
|
367
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
368
|
+
leaveInput();
|
|
369
|
+
navigate(1);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (matchesKey(data, Key.left)) {
|
|
373
|
+
leaveInput();
|
|
374
|
+
navigate(-1);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (matchesKey(data, Key.up)) {
|
|
378
|
+
leaveInput();
|
|
379
|
+
moveCursor(-1);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (matchesKey(data, Key.down)) {
|
|
383
|
+
leaveInput();
|
|
384
|
+
moveCursor(1);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
customInput.handleInput(data);
|
|
388
|
+
refresh();
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (isReviewPage()) {
|
|
393
|
+
if (matchesKey(data, Key.escape)) {
|
|
394
|
+
finishCancel();
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
const isEnter = matchesKey(data, Key.enter) || matchesKey(data, Key.return) || data === '\n';
|
|
398
|
+
if (isEnter) {
|
|
399
|
+
finish();
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
403
|
+
navigate(1);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (matchesKey(data, Key.left)) {
|
|
407
|
+
navigate(-1);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const question = questions[questionIndex];
|
|
414
|
+
const state = states[questionIndex];
|
|
415
|
+
if (!question || !state) return;
|
|
416
|
+
const rows = rowsFor(questionIndex);
|
|
417
|
+
|
|
418
|
+
if (matchesKey(data, Key.escape)) {
|
|
419
|
+
finishCancel();
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
423
|
+
navigate(1);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (matchesKey(data, Key.left)) {
|
|
427
|
+
navigate(-1);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (matchesKey(data, Key.pageUp)) {
|
|
431
|
+
state.scrollOffset = Math.max(0, state.scrollOffset - 8);
|
|
432
|
+
refresh();
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (matchesKey(data, Key.pageDown)) {
|
|
436
|
+
state.scrollOffset += 8;
|
|
437
|
+
refresh();
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (matchesKey(data, Key.up)) {
|
|
441
|
+
moveCursor(-1);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (matchesKey(data, Key.down)) {
|
|
445
|
+
moveCursor(1);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (data === ' ' && question.multi) {
|
|
450
|
+
const toggleRow = rows[state.cursor];
|
|
451
|
+
const option = toggleRow?.kind === 'option' ? question.options[toggleRow.index] : undefined;
|
|
452
|
+
if (option) {
|
|
453
|
+
if (state.selected.has(option.value)) state.selected.delete(option.value);
|
|
454
|
+
else state.selected.add(option.value);
|
|
455
|
+
state.customInput = undefined;
|
|
456
|
+
refresh();
|
|
457
|
+
}
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const row = rows[state.cursor];
|
|
462
|
+
if (!row) return;
|
|
463
|
+
|
|
464
|
+
// Any printable character starts the custom input.
|
|
465
|
+
const isPaste = data.includes('\u001b[200~');
|
|
466
|
+
const isTypingKey =
|
|
467
|
+
isPaste || (data.length === 1 && data >= ' ' && data !== '\u001b' && !(data === ' ' && question.multi));
|
|
468
|
+
if (row.kind === 'other' && isTypingKey) {
|
|
469
|
+
openCustomInput();
|
|
470
|
+
customInput.handleInput(data);
|
|
471
|
+
refresh();
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const isEnter = matchesKey(data, Key.enter) || matchesKey(data, Key.return) || data === '\n';
|
|
476
|
+
if (!isEnter) return;
|
|
477
|
+
|
|
478
|
+
if (row.kind === 'other') {
|
|
479
|
+
openCustomInput();
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const option = question.options[row.index];
|
|
483
|
+
if (!option) return;
|
|
484
|
+
if (question.multi) {
|
|
485
|
+
state.selected.add(option.value);
|
|
486
|
+
} else {
|
|
487
|
+
state.selected = new Set([option.value]);
|
|
488
|
+
}
|
|
489
|
+
state.customInput = undefined;
|
|
490
|
+
advance();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function refresh(): void {
|
|
494
|
+
tui.requestRender();
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function render(width: number): string[] {
|
|
498
|
+
const terminalRows = process.stdout.rows ?? 40;
|
|
499
|
+
const inner = Math.max(1, width - 4);
|
|
500
|
+
const lines: string[] = [];
|
|
501
|
+
const add = (text: string) => lines.push(truncateToWidth(text, width));
|
|
502
|
+
|
|
503
|
+
add(theme.fg('accent', theme.bold('\u2500'.repeat(width))));
|
|
504
|
+
|
|
505
|
+
if (questions.length > 1) {
|
|
506
|
+
add(` ${theme.fg('dim', QUESTIONS_LABEL)}`);
|
|
507
|
+
lines.push('');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const question = isReviewPage() ? undefined : questions[questionIndex];
|
|
511
|
+
const state = isReviewPage() ? undefined : states[questionIndex];
|
|
512
|
+
|
|
513
|
+
// Without model-provided headers the strip cannot name tabs, so it degrades
|
|
514
|
+
// to a single dim progress line instead.
|
|
515
|
+
let stripNamesAllQuestions = false;
|
|
516
|
+
if (questions.length > 1) {
|
|
517
|
+
stripNamesAllQuestions = questions.every((q) => q.header?.trim() !== undefined);
|
|
518
|
+
if (stripNamesAllQuestions) {
|
|
519
|
+
add(tabStrip(width));
|
|
520
|
+
} else {
|
|
521
|
+
const completed = questions.filter((_q, index) => questionAnswered(index)).length;
|
|
522
|
+
add(
|
|
523
|
+
theme.fg(
|
|
524
|
+
'dim',
|
|
525
|
+
` Field ${Math.min(questionIndex + 1, questions.length)} of ${questions.length} · ${completed}/${questions.length} completed`,
|
|
526
|
+
),
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
lines.push('');
|
|
530
|
+
}
|
|
531
|
+
if (isReviewPage()) {
|
|
532
|
+
lines.push('');
|
|
533
|
+
for (const line of reviewPageLines(inner)) add(line);
|
|
534
|
+
add(theme.fg('accent', theme.bold('\u2500'.repeat(width))));
|
|
535
|
+
return lines;
|
|
536
|
+
}
|
|
537
|
+
if (question?.header && !stripNamesAllQuestions) {
|
|
538
|
+
add(theme.fg('muted', ` ${question.header}`));
|
|
539
|
+
}
|
|
540
|
+
if (question) {
|
|
541
|
+
const wrapped = wordWrap(sanitizeForDisplay(question.question), inner).slice(0, MAX_QUESTION_ROWS);
|
|
542
|
+
for (const line of wrapped) add(` ${theme.fg('text', line)}`);
|
|
543
|
+
lines.push('');
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (question && state) {
|
|
547
|
+
const block = optionBlock(state, inner);
|
|
548
|
+
const cursorStart = block.starts[state.cursor] ?? 0;
|
|
549
|
+
const cursorEnd =
|
|
550
|
+
state.cursor + 1 < block.starts.length
|
|
551
|
+
? (block.starts[state.cursor + 1] ?? block.lines.length)
|
|
552
|
+
: block.lines.length;
|
|
553
|
+
const trailing = 4;
|
|
554
|
+
const available = Math.max(4, Math.min(20, terminalRows - lines.length - trailing));
|
|
555
|
+
if (cursorStart < state.scrollOffset || cursorEnd > state.scrollOffset + available) {
|
|
556
|
+
state.scrollOffset = Math.max(0, cursorEnd - available);
|
|
557
|
+
}
|
|
558
|
+
state.scrollOffset = Math.min(Math.max(0, block.lines.length - available), state.scrollOffset);
|
|
559
|
+
for (const line of block.lines.slice(state.scrollOffset, state.scrollOffset + available)) {
|
|
560
|
+
add(line);
|
|
561
|
+
}
|
|
562
|
+
if (state.scrollOffset > 0 || state.scrollOffset + available < block.lines.length) {
|
|
563
|
+
const arrows = `${state.scrollOffset > 0 ? '\u2191' : ''}${state.scrollOffset + available < block.lines.length ? '\u2193' : ''}`;
|
|
564
|
+
add(theme.fg('dim', ` ${arrows} scroll`));
|
|
565
|
+
}
|
|
566
|
+
lines.push('');
|
|
567
|
+
add(footerHints(width));
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
add(theme.fg('accent', theme.bold('\u2500'.repeat(width))));
|
|
571
|
+
return lines;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function optionMarker(selected: boolean): string {
|
|
575
|
+
if (questionIndex >= 0) {
|
|
576
|
+
const q = questions[questionIndex];
|
|
577
|
+
if (q?.multi) return selected ? '\u2611' : '\u2610';
|
|
578
|
+
}
|
|
579
|
+
return selected ? '\u25c9' : '\u25cb';
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function footerHints(width: number): string {
|
|
583
|
+
const segments: { key: string; action: string; order: number; rank: number }[] = [];
|
|
584
|
+
const add = (key: string, action: string, order: number, rank: number) => {
|
|
585
|
+
segments.push({ key, action, order, rank });
|
|
586
|
+
};
|
|
587
|
+
add('\u21c6', 'tab', 0, 6);
|
|
588
|
+
add('enter', 'confirm', 1, 2);
|
|
589
|
+
if (mode !== 'custom') {
|
|
590
|
+
add('\u2191\u2193', 'select', 2, 7);
|
|
591
|
+
if (questions[questionIndex]?.multi) add('space', 'toggle', 3, 3);
|
|
592
|
+
}
|
|
593
|
+
add('esc', mode === 'custom' ? 'close' : 'dismiss', 5, 1);
|
|
594
|
+
|
|
595
|
+
const separator = ' · ';
|
|
596
|
+
const budget = Math.max(0, width - 2);
|
|
597
|
+
const kept: typeof segments = [];
|
|
598
|
+
let used = 0;
|
|
599
|
+
for (const segment of segments.toSorted((a, b) => a.rank - b.rank)) {
|
|
600
|
+
const cost = segment.key.length + segment.action.length + 1 + (kept.length === 0 ? 0 : separator.length);
|
|
601
|
+
if (used + cost > budget) continue;
|
|
602
|
+
kept.push(segment);
|
|
603
|
+
used += cost;
|
|
604
|
+
}
|
|
605
|
+
const text = kept
|
|
606
|
+
.toSorted((a, b) => a.order - b.order)
|
|
607
|
+
.map((segment) => `${theme.fg('text', segment.key)} ${theme.fg('dim', segment.action)}`)
|
|
608
|
+
.join(theme.fg('dim', separator));
|
|
609
|
+
return ` ${text}`;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function optionBlock(state: PerQuestionState, inner: number): { lines: string[]; starts: number[] } {
|
|
613
|
+
const width = inner + 4;
|
|
614
|
+
const rows = rowsFor(questionIndex);
|
|
615
|
+
const lines: string[] = [];
|
|
616
|
+
const starts: number[] = [];
|
|
617
|
+
const addRow = (row: DialogRow) => {
|
|
618
|
+
starts.push(lines.length);
|
|
619
|
+
const selected = rowChecked(state, row);
|
|
620
|
+
const isCursor = rows[state.cursor] === row;
|
|
621
|
+
const isOther = row.kind === 'other';
|
|
622
|
+
const cursor = isCursor ? theme.fg('accent', ' \u276f ') : ' ';
|
|
623
|
+
const glyph = isOther ? '\u270e' : optionMarker(selected);
|
|
624
|
+
const styledGlyph =
|
|
625
|
+
isOther && state.customInput !== undefined ? theme.fg('success', glyph) : theme.fg('dim', glyph);
|
|
626
|
+
const prefix = `${cursor}${styledGlyph} `;
|
|
627
|
+
const prefixWidth = visibleWidth(prefix);
|
|
628
|
+
const committedCustom =
|
|
629
|
+
isOther && state.customInput !== undefined ? state.customInput.replace(/\s+/g, ' ').trim() : '';
|
|
630
|
+
if (isOther && mode === 'custom' && isCursor) {
|
|
631
|
+
// Input already scrolled its window to the cursor; truncating from the
|
|
632
|
+
// left would destroy that window, so only the right edge is clipped.
|
|
633
|
+
for (const line of customInput.render(Math.max(1, width - prefixWidth))) {
|
|
634
|
+
lines.push(`${prefix}${line}`);
|
|
635
|
+
}
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const labelColor = isCursor ? 'accent' : selected ? 'toolOutput' : 'text';
|
|
639
|
+
let labelText = committedCustom || row.label;
|
|
640
|
+
if (row.kind === 'option' && row.recommended && !row.label.endsWith(RECOMMENDED_SUFFIX)) {
|
|
641
|
+
labelText += ' (Recommended)';
|
|
642
|
+
}
|
|
643
|
+
const available = Math.max(1, width - prefixWidth);
|
|
644
|
+
const wrapped = wordWrap(labelText, available);
|
|
645
|
+
lines.push(truncateToWidth(`${prefix}${theme.fg(labelColor, wrapped[0] ?? '')}`, width));
|
|
646
|
+
const indent = ' '.repeat(prefixWidth);
|
|
647
|
+
for (const extra of wrapped.slice(1)) {
|
|
648
|
+
lines.push(truncateToWidth(`${indent}${theme.fg(labelColor, extra)}`, width));
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (row.kind === 'option') {
|
|
652
|
+
if (row.description?.trim()) {
|
|
653
|
+
const wrappedDescription = wordWrap(sanitizeForDisplay(row.description.trim()), descriptionBudget(width));
|
|
654
|
+
const shown = wrappedDescription.slice(0, MAX_DESC_ROWS);
|
|
655
|
+
for (const line of shown) {
|
|
656
|
+
lines.push(`${DESCRIPTION_GUTTER}${isCursor ? theme.fg('muted', line) : theme.fg('dim', line)}`);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
rows.forEach(addRow);
|
|
663
|
+
return { lines, starts };
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
667
|
+
|
|
668
|
+
return {
|
|
669
|
+
render,
|
|
670
|
+
handleInput,
|
|
671
|
+
invalidate: () => {},
|
|
672
|
+
dispose: () => {
|
|
673
|
+
signal?.removeEventListener('abort', onAbort);
|
|
674
|
+
},
|
|
675
|
+
};
|
|
676
|
+
}
|