@mrclrchtr/supi-ask-user 1.13.0 → 1.14.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/README.md +157 -65
- package/node_modules/@mrclrchtr/supi-core/README.md +10 -0
- package/node_modules/@mrclrchtr/supi-core/package.json +2 -1
- package/node_modules/@mrclrchtr/supi-core/src/api.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/index.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/progress-widget.ts +98 -17
- package/node_modules/@mrclrchtr/supi-core/src/report.ts +121 -0
- package/package.json +2 -2
- package/src/api.ts +7 -5
- package/src/ask-user.ts +23 -7
- package/src/index.ts +7 -5
- package/src/normalize.ts +47 -30
- package/src/render/result.ts +72 -49
- package/src/render/transcript.ts +90 -62
- package/src/schema.ts +8 -30
- package/src/session/controller.ts +226 -193
- package/src/session/lock.ts +1 -1
- package/src/tool/guidance.ts +4 -1
- package/src/types.ts +46 -35
- package/src/ui/choose-renderer.ts +20 -4
- package/src/ui/form-component.ts +574 -0
- package/src/ui/form-render-primitives.ts +71 -0
- package/src/ui/form-render.ts +283 -0
- package/src/ui/form-review-render.ts +144 -0
- package/src/ui/form-view.ts +23 -0
- package/src/ui/{overlay.ts → form.ts} +12 -9
- package/src/ui/types.ts +7 -3
- package/src/ui/overlay-actions.ts +0 -42
- package/src/ui/overlay-component.ts +0 -400
- package/src/ui/overlay-render.ts +0 -230
- package/src/ui/overlay-view.ts +0 -327
|
@@ -1,400 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Component,
|
|
3
|
-
Editor,
|
|
4
|
-
type Focusable,
|
|
5
|
-
Key,
|
|
6
|
-
matchesKey,
|
|
7
|
-
SelectList,
|
|
8
|
-
} from "@earendil-works/pi-tui";
|
|
9
|
-
import type { NormalizedChoiceQuestion } from "../types.ts";
|
|
10
|
-
import { createActionList } from "./overlay-actions.ts";
|
|
11
|
-
import {
|
|
12
|
-
clampIndex,
|
|
13
|
-
currentCustomValue,
|
|
14
|
-
currentPreviewText,
|
|
15
|
-
currentTextValue,
|
|
16
|
-
makeEditorTheme,
|
|
17
|
-
makeSelectListTheme,
|
|
18
|
-
renderOverlayFrame,
|
|
19
|
-
} from "./overlay-render.ts";
|
|
20
|
-
import {
|
|
21
|
-
buildChoiceItems,
|
|
22
|
-
buildChoiceRows,
|
|
23
|
-
type ChoiceRow,
|
|
24
|
-
choiceRowValue,
|
|
25
|
-
defaultChoiceRowIndex,
|
|
26
|
-
type FocusTarget,
|
|
27
|
-
noteTargetLabel,
|
|
28
|
-
type OverlayAction,
|
|
29
|
-
type OverlayMode,
|
|
30
|
-
previewOptionIndexForRows,
|
|
31
|
-
} from "./overlay-view.ts";
|
|
32
|
-
import type { OverlayArgs } from "./types.ts";
|
|
33
|
-
|
|
34
|
-
export class AskUserOverlay implements Component, Focusable {
|
|
35
|
-
focused = false;
|
|
36
|
-
private readonly editor: Editor;
|
|
37
|
-
private focus: FocusTarget = "choices";
|
|
38
|
-
private mode: OverlayMode = "choice";
|
|
39
|
-
private closed = false;
|
|
40
|
-
private cachedWidth: number | undefined;
|
|
41
|
-
private cachedLines: string[] | undefined;
|
|
42
|
-
private readonly onAbort: () => void;
|
|
43
|
-
private choiceRows: ChoiceRow[] = [];
|
|
44
|
-
private choiceRowIndex = 0;
|
|
45
|
-
private previewOptionIndex = 0;
|
|
46
|
-
private choiceList: SelectList | undefined;
|
|
47
|
-
private textActions: Array<{ action: OverlayAction; label: string }> = [];
|
|
48
|
-
private actionIndex = 0;
|
|
49
|
-
private actionList: SelectList | undefined;
|
|
50
|
-
constructor(private readonly args: OverlayArgs) {
|
|
51
|
-
this.editor = new Editor(args.tui, makeEditorTheme(args.theme));
|
|
52
|
-
this.editor.onSubmit = (value) => this.handleEditorSubmit(value);
|
|
53
|
-
this.syncCurrentQuestion();
|
|
54
|
-
this.onAbort = () => {
|
|
55
|
-
this.args.controller.abort();
|
|
56
|
-
this.finish();
|
|
57
|
-
};
|
|
58
|
-
args.signal?.addEventListener("abort", this.onAbort);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
render(width: number): string[] {
|
|
62
|
-
this.editor.focused = this.focus === "editor";
|
|
63
|
-
if (this.cachedWidth === width && this.cachedLines) return this.cachedLines;
|
|
64
|
-
this.cachedWidth = width;
|
|
65
|
-
this.cachedLines = renderOverlayFrame({
|
|
66
|
-
width,
|
|
67
|
-
theme: this.args.theme,
|
|
68
|
-
controller: this.args.controller,
|
|
69
|
-
mode: this.mode,
|
|
70
|
-
focus: this.focus,
|
|
71
|
-
editor: this.editor,
|
|
72
|
-
choiceRows: this.choiceRows,
|
|
73
|
-
choiceRowIndex: this.choiceRowIndex,
|
|
74
|
-
actionList: this.actionList,
|
|
75
|
-
textActionLabels: this.textActions.map(({ label }) => label),
|
|
76
|
-
previewText: currentPreviewText(
|
|
77
|
-
this.args.controller.currentQuestion,
|
|
78
|
-
this.previewOptionIndex,
|
|
79
|
-
),
|
|
80
|
-
noteTargetLabel:
|
|
81
|
-
this.mode === "note-input"
|
|
82
|
-
? noteTargetLabel(this.args.controller, this.choiceRows, this.choiceRowIndex)
|
|
83
|
-
: undefined,
|
|
84
|
-
});
|
|
85
|
-
return this.cachedLines;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
handleInput(data: string): void {
|
|
89
|
-
if (this.closed || this.args.controller.isTerminal) return;
|
|
90
|
-
|
|
91
|
-
if (this.args.keybindings.matches(data, "app.tools.expand")) {
|
|
92
|
-
this.args.onToggleToolsExpanded?.();
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (this.mode === "note-input" && matchesKey(data, Key.escape)) {
|
|
97
|
-
const question = this.args.controller.currentQuestion;
|
|
98
|
-
const row = this.choiceRows[this.choiceRowIndex];
|
|
99
|
-
if (question.type === "choice" && row?.kind === "option") {
|
|
100
|
-
this.restoreChoiceMode(question, row.optionIndex);
|
|
101
|
-
}
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (matchesKey(data, Key.escape)) {
|
|
105
|
-
this.args.controller.cancel();
|
|
106
|
-
this.finish();
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
if (this.mode !== "note-input" && matchesKey(data, Key.left)) {
|
|
110
|
-
if (this.args.controller.goBack()) {
|
|
111
|
-
this.syncCurrentQuestion();
|
|
112
|
-
this.refresh();
|
|
113
|
-
}
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
switch (this.focus) {
|
|
118
|
-
case "choices":
|
|
119
|
-
this.handleChoiceKey(data);
|
|
120
|
-
return;
|
|
121
|
-
case "actions":
|
|
122
|
-
this.handleActionKey(data);
|
|
123
|
-
return;
|
|
124
|
-
case "editor":
|
|
125
|
-
this.handleEditorKey(data);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
invalidate(): void {
|
|
131
|
-
this.cachedLines = undefined;
|
|
132
|
-
this.choiceList?.invalidate();
|
|
133
|
-
this.actionList?.invalidate();
|
|
134
|
-
this.editor.invalidate();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
dispose(): void {
|
|
138
|
-
this.closed = true;
|
|
139
|
-
this.args.signal?.removeEventListener("abort", this.onAbort);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private handleChoiceKey(data: string): void {
|
|
143
|
-
const question = this.args.controller.currentQuestion;
|
|
144
|
-
if (question.type !== "choice" || !this.choiceList) return;
|
|
145
|
-
|
|
146
|
-
if (data === "n") {
|
|
147
|
-
const row = this.choiceRows[this.choiceRowIndex];
|
|
148
|
-
if (row?.kind === "option") this.openNoteEditor(question, row.optionIndex);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (matchesKey(data, Key.space)) {
|
|
153
|
-
const row = this.choiceRows[this.choiceRowIndex];
|
|
154
|
-
if (row?.kind === "option") this.applyChoiceSelection(question, row.optionIndex, false);
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this.choiceList.handleInput(data);
|
|
159
|
-
}
|
|
160
|
-
private handleActionKey(data: string): void {
|
|
161
|
-
const question = this.args.controller.currentQuestion;
|
|
162
|
-
if (!this.actionList) return;
|
|
163
|
-
|
|
164
|
-
if (question.type === "text") {
|
|
165
|
-
if (matchesKey(data, Key.up) && this.actionIndex === 0) {
|
|
166
|
-
this.focus = "editor";
|
|
167
|
-
this.refresh();
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
} else if (matchesKey(data, Key.up) && this.actionIndex === 0) {
|
|
171
|
-
this.focus = "choices";
|
|
172
|
-
this.refresh();
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
this.actionList.handleInput(data);
|
|
177
|
-
this.refresh();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private handleEditorKey(data: string): void {
|
|
181
|
-
if (this.mode === "text" && matchesKey(data, Key.down) && this.textActions.length > 0) {
|
|
182
|
-
this.focus = "actions";
|
|
183
|
-
this.refresh();
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
this.editor.handleInput(data);
|
|
188
|
-
this.refresh();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private handleEditorSubmit(value: string): void {
|
|
192
|
-
const trimmed = value.trim();
|
|
193
|
-
const question = this.args.controller.currentQuestion;
|
|
194
|
-
|
|
195
|
-
if (this.mode === "discuss-input") {
|
|
196
|
-
this.args.controller.finishDiscuss(trimmed || undefined);
|
|
197
|
-
this.finish();
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
if (this.mode === "note-input") {
|
|
201
|
-
if (question.type !== "choice") return;
|
|
202
|
-
const row = this.choiceRows[this.choiceRowIndex];
|
|
203
|
-
if (row?.kind !== "option") return;
|
|
204
|
-
this.args.controller.setChoiceOptionNote(question, row.optionIndex, trimmed || undefined);
|
|
205
|
-
this.restoreChoiceMode(question, row.optionIndex);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
if (this.mode === "custom-input") {
|
|
209
|
-
if (trimmed.length === 0) return;
|
|
210
|
-
this.args.controller.setAnswer(question.id, { kind: "custom", value: trimmed });
|
|
211
|
-
this.advanceAfterQuestion();
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (trimmed.length === 0) {
|
|
215
|
-
if (question.required) return;
|
|
216
|
-
this.args.controller.clearAnswer(question.id);
|
|
217
|
-
this.advanceAfterQuestion();
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
this.args.controller.setAnswer(question.id, { kind: "text", value: trimmed });
|
|
222
|
-
this.advanceAfterQuestion();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private applyChoiceSelection(
|
|
226
|
-
question: NormalizedChoiceQuestion,
|
|
227
|
-
optionIndex: number,
|
|
228
|
-
submit: boolean,
|
|
229
|
-
): void {
|
|
230
|
-
if (question.multi) {
|
|
231
|
-
this.toggleMultiChoice(question, optionIndex);
|
|
232
|
-
if (submit && this.args.controller.hasAnswer(question.id)) this.advanceAfterQuestion();
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
this.args.controller.selectChoiceOption(question, optionIndex);
|
|
237
|
-
this.choiceRowIndex = optionIndex;
|
|
238
|
-
this.previewOptionIndex = optionIndex;
|
|
239
|
-
if (submit) {
|
|
240
|
-
this.advanceAfterQuestion();
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
this.buildChoiceList(question);
|
|
244
|
-
this.refresh();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
private toggleMultiChoice(question: NormalizedChoiceQuestion, optionIndex: number): void {
|
|
248
|
-
this.args.controller.toggleChoiceOption(question, optionIndex);
|
|
249
|
-
this.choiceRowIndex = optionIndex;
|
|
250
|
-
this.previewOptionIndex = optionIndex;
|
|
251
|
-
this.buildChoiceList(question);
|
|
252
|
-
this.refresh();
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
private openNoteEditor(question: NormalizedChoiceQuestion, optionIndex: number): void {
|
|
256
|
-
const option = question.options[optionIndex];
|
|
257
|
-
if (!option) return;
|
|
258
|
-
this.mode = "note-input";
|
|
259
|
-
this.focus = "editor";
|
|
260
|
-
this.choiceRowIndex = optionIndex;
|
|
261
|
-
this.previewOptionIndex = optionIndex;
|
|
262
|
-
this.editor.setText(this.args.controller.getChoiceOptionNote(question.id, option.value) ?? "");
|
|
263
|
-
this.refresh();
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
private restoreChoiceMode(question: NormalizedChoiceQuestion, optionIndex: number): void {
|
|
267
|
-
this.mode = "choice";
|
|
268
|
-
this.focus = "choices";
|
|
269
|
-
this.editor.setText("");
|
|
270
|
-
this.choiceRows = buildChoiceRows(this.args.controller, question);
|
|
271
|
-
const nextIndex = this.choiceRows.findIndex(
|
|
272
|
-
(row) => row.kind === "option" && row.optionIndex === optionIndex,
|
|
273
|
-
);
|
|
274
|
-
this.choiceRowIndex = clampIndex(
|
|
275
|
-
nextIndex >= 0 ? nextIndex : optionIndex,
|
|
276
|
-
this.choiceRows.length,
|
|
277
|
-
);
|
|
278
|
-
this.previewOptionIndex = optionIndex;
|
|
279
|
-
this.buildChoiceList(question);
|
|
280
|
-
this.refresh();
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
private handleAction(action: OverlayAction): void {
|
|
284
|
-
switch (action) {
|
|
285
|
-
case "other":
|
|
286
|
-
this.mode = "custom-input";
|
|
287
|
-
this.focus = "editor";
|
|
288
|
-
this.editor.setText(currentCustomValue(this.args.controller));
|
|
289
|
-
this.refresh();
|
|
290
|
-
return;
|
|
291
|
-
case "skip":
|
|
292
|
-
this.args.controller.clearAnswer(this.args.controller.currentQuestion.id);
|
|
293
|
-
this.advanceAfterQuestion();
|
|
294
|
-
return;
|
|
295
|
-
case "discuss":
|
|
296
|
-
this.mode = "discuss-input";
|
|
297
|
-
this.focus = "editor";
|
|
298
|
-
this.editor.setText("");
|
|
299
|
-
this.refresh();
|
|
300
|
-
return;
|
|
301
|
-
case "partial":
|
|
302
|
-
this.args.controller.finishPartial();
|
|
303
|
-
this.finish();
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private advanceAfterQuestion(): void {
|
|
309
|
-
if (!this.args.controller.goNext()) {
|
|
310
|
-
this.args.controller.finishSubmitted();
|
|
311
|
-
this.finish();
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
this.syncCurrentQuestion();
|
|
315
|
-
this.refresh();
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
private syncCurrentQuestion(): void {
|
|
319
|
-
const question = this.args.controller.currentQuestion;
|
|
320
|
-
if (question.type === "text") {
|
|
321
|
-
this.mode = "text";
|
|
322
|
-
this.focus = "editor";
|
|
323
|
-
this.editor.setText(currentTextValue(this.args.controller, question.initial));
|
|
324
|
-
this.buildTextActions();
|
|
325
|
-
this.choiceRows = [];
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
this.mode = "choice";
|
|
330
|
-
this.focus = "choices";
|
|
331
|
-
this.textActions = [];
|
|
332
|
-
this.actionList = undefined;
|
|
333
|
-
this.editor.setText("");
|
|
334
|
-
this.choiceRows = buildChoiceRows(this.args.controller, question);
|
|
335
|
-
this.choiceRowIndex = clampIndex(
|
|
336
|
-
defaultChoiceRowIndex(this.args.controller, question, this.choiceRows),
|
|
337
|
-
this.choiceRows.length,
|
|
338
|
-
);
|
|
339
|
-
this.previewOptionIndex =
|
|
340
|
-
previewOptionIndexForRows(this.choiceRows, this.choiceRowIndex, this.previewOptionIndex) ?? 0;
|
|
341
|
-
this.buildChoiceList(question);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
private buildChoiceList(question: NormalizedChoiceQuestion): void {
|
|
345
|
-
const items = buildChoiceItems(this.args.controller, question, this.choiceRows);
|
|
346
|
-
|
|
347
|
-
const list = new SelectList(
|
|
348
|
-
items,
|
|
349
|
-
Math.min(this.choiceRows.length, 10),
|
|
350
|
-
makeSelectListTheme(this.args.theme),
|
|
351
|
-
);
|
|
352
|
-
list.onSelectionChange = (item) => {
|
|
353
|
-
const nextIndex = this.choiceRows.findIndex((row) => choiceRowValue(row) === item.value);
|
|
354
|
-
if (nextIndex < 0) return;
|
|
355
|
-
this.choiceRowIndex = nextIndex;
|
|
356
|
-
this.previewOptionIndex =
|
|
357
|
-
previewOptionIndexForRows(this.choiceRows, nextIndex, this.previewOptionIndex) ??
|
|
358
|
-
this.previewOptionIndex;
|
|
359
|
-
this.refresh();
|
|
360
|
-
};
|
|
361
|
-
list.onSelect = (item) => {
|
|
362
|
-
const row = this.choiceRows.find((candidate) => choiceRowValue(candidate) === item.value);
|
|
363
|
-
if (!row) return;
|
|
364
|
-
if (row.kind === "option") {
|
|
365
|
-
this.applyChoiceSelection(question, row.optionIndex, true);
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
this.handleAction(row.action);
|
|
369
|
-
};
|
|
370
|
-
list.setSelectedIndex(this.choiceRowIndex);
|
|
371
|
-
this.choiceList = list;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
private buildTextActions(): void {
|
|
375
|
-
const state = createActionList({
|
|
376
|
-
controller: this.args.controller,
|
|
377
|
-
theme: this.args.theme,
|
|
378
|
-
actionIndex: this.actionIndex,
|
|
379
|
-
onIndexChange: (index) => {
|
|
380
|
-
this.actionIndex = index;
|
|
381
|
-
this.refresh();
|
|
382
|
-
},
|
|
383
|
-
onAction: (action) => this.handleAction(action),
|
|
384
|
-
});
|
|
385
|
-
this.textActions = state.entries;
|
|
386
|
-
this.actionList = state.list;
|
|
387
|
-
this.actionIndex = state.index;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
private refresh(): void {
|
|
391
|
-
this.cachedLines = undefined;
|
|
392
|
-
this.args.tui.requestRender();
|
|
393
|
-
}
|
|
394
|
-
private finish(): void {
|
|
395
|
-
if (this.closed) return;
|
|
396
|
-
this.closed = true;
|
|
397
|
-
this.args.signal?.removeEventListener("abort", this.onAbort);
|
|
398
|
-
this.args.done(this.args.controller.outcome());
|
|
399
|
-
}
|
|
400
|
-
}
|
package/src/ui/overlay-render.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { getMarkdownTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
type Editor,
|
|
4
|
-
type EditorTheme,
|
|
5
|
-
Markdown,
|
|
6
|
-
type SelectList,
|
|
7
|
-
type SelectListTheme,
|
|
8
|
-
wrapTextWithAnsi,
|
|
9
|
-
} from "@earendil-works/pi-tui";
|
|
10
|
-
import type { AskUserController } from "../session/controller.ts";
|
|
11
|
-
import type { NormalizedQuestionnaire } from "../types.ts";
|
|
12
|
-
import {
|
|
13
|
-
type ChoiceRow,
|
|
14
|
-
type FocusTarget,
|
|
15
|
-
footerText,
|
|
16
|
-
type OverlayMode,
|
|
17
|
-
renderChoiceList,
|
|
18
|
-
splitColumns,
|
|
19
|
-
} from "./overlay-view.ts";
|
|
20
|
-
|
|
21
|
-
export interface RenderOverlayFrameArgs {
|
|
22
|
-
width: number;
|
|
23
|
-
theme: Theme;
|
|
24
|
-
controller: AskUserController;
|
|
25
|
-
mode: OverlayMode;
|
|
26
|
-
focus: FocusTarget;
|
|
27
|
-
editor: Editor;
|
|
28
|
-
choiceRows: ChoiceRow[];
|
|
29
|
-
choiceRowIndex: number;
|
|
30
|
-
actionList: SelectList | undefined;
|
|
31
|
-
textActionLabels: string[];
|
|
32
|
-
previewText?: string;
|
|
33
|
-
noteTargetLabel?: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function renderOverlayFrame(args: RenderOverlayFrameArgs): string[] {
|
|
37
|
-
const lines: string[] = [];
|
|
38
|
-
lines.push(args.theme.fg("accent", "─".repeat(args.width)));
|
|
39
|
-
lines.push(...renderHeader(args));
|
|
40
|
-
lines.push(...renderPrompt(args.controller.currentQuestion.prompt, args.width));
|
|
41
|
-
lines.push("");
|
|
42
|
-
lines.push(...renderBody(args));
|
|
43
|
-
lines.push("");
|
|
44
|
-
lines.push(
|
|
45
|
-
args.theme.fg(
|
|
46
|
-
"dim",
|
|
47
|
-
footerText({
|
|
48
|
-
controller: args.controller,
|
|
49
|
-
mode: args.mode,
|
|
50
|
-
focus: args.focus,
|
|
51
|
-
hasTextActions: args.textActionLabels.length > 0,
|
|
52
|
-
}),
|
|
53
|
-
),
|
|
54
|
-
);
|
|
55
|
-
lines.push(args.theme.fg("accent", "─".repeat(args.width)));
|
|
56
|
-
return lines;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function makeEditorTheme(theme: Theme): EditorTheme {
|
|
60
|
-
return {
|
|
61
|
-
borderColor: (text) => theme.fg("accent", text),
|
|
62
|
-
selectList: makeSelectListTheme(theme),
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function makeSelectListTheme(theme: Theme): SelectListTheme {
|
|
67
|
-
return {
|
|
68
|
-
selectedPrefix: (text) => theme.fg("accent", text),
|
|
69
|
-
selectedText: (text) => theme.fg("accent", text),
|
|
70
|
-
description: (text) => theme.fg("muted", text),
|
|
71
|
-
scrollInfo: (text) => theme.fg("dim", text),
|
|
72
|
-
noMatch: (text) => theme.fg("warning", text),
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function currentPreviewText(
|
|
77
|
-
question: NormalizedQuestionnaire["questions"][number],
|
|
78
|
-
optionIndex: number,
|
|
79
|
-
): string | undefined {
|
|
80
|
-
return question.type === "choice" ? question.options[optionIndex]?.preview : undefined;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function currentTextValue(
|
|
84
|
-
controller: AskUserController,
|
|
85
|
-
initial: string | undefined,
|
|
86
|
-
): string {
|
|
87
|
-
const answer = controller.getAnswer(controller.currentQuestion.id);
|
|
88
|
-
return answer?.kind === "text" ? answer.value : (initial ?? "");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function currentCustomValue(controller: AskUserController): string {
|
|
92
|
-
const answer = controller.getAnswer(controller.currentQuestion.id);
|
|
93
|
-
return answer?.kind === "custom" ? answer.value : "";
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function clampIndex(index: number, length: number): number {
|
|
97
|
-
if (length === 0) return 0;
|
|
98
|
-
return Math.max(0, Math.min(index, length - 1));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function renderHeader(args: RenderOverlayFrameArgs): string[] {
|
|
102
|
-
const lines: string[] = [];
|
|
103
|
-
const { title, intro } = args.controller.questionnaire;
|
|
104
|
-
if (title)
|
|
105
|
-
lines.push(...wrapTextWithAnsi(args.theme.fg("accent", args.theme.bold(title)), args.width));
|
|
106
|
-
lines.push(
|
|
107
|
-
...wrapTextWithAnsi(
|
|
108
|
-
args.theme.fg(
|
|
109
|
-
"muted",
|
|
110
|
-
`${args.controller.currentIndex + 1}/${args.controller.questions.length} · ${args.controller.currentQuestion.header}`,
|
|
111
|
-
),
|
|
112
|
-
args.width,
|
|
113
|
-
),
|
|
114
|
-
);
|
|
115
|
-
if (intro) {
|
|
116
|
-
lines.push("");
|
|
117
|
-
lines.push(...renderPrompt(intro, args.width));
|
|
118
|
-
}
|
|
119
|
-
return lines;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function renderBody(args: RenderOverlayFrameArgs): string[] {
|
|
123
|
-
const question = args.controller.currentQuestion;
|
|
124
|
-
if (question.type === "text") {
|
|
125
|
-
return renderTextBody(args);
|
|
126
|
-
}
|
|
127
|
-
return renderChoiceBody(args);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function renderChoiceBody(args: RenderOverlayFrameArgs): string[] {
|
|
131
|
-
const question = args.controller.currentQuestion;
|
|
132
|
-
const listWidth = splitLeftWidth(args.width);
|
|
133
|
-
|
|
134
|
-
const leftLines =
|
|
135
|
-
question.type === "choice"
|
|
136
|
-
? renderChoiceList({
|
|
137
|
-
controller: args.controller,
|
|
138
|
-
question,
|
|
139
|
-
rows: args.choiceRows,
|
|
140
|
-
selectedIndex: args.choiceRowIndex,
|
|
141
|
-
theme: args.theme,
|
|
142
|
-
width: listWidth,
|
|
143
|
-
})
|
|
144
|
-
: [];
|
|
145
|
-
|
|
146
|
-
if (args.mode === "custom-input" || args.mode === "discuss-input" || args.mode === "note-input") {
|
|
147
|
-
const rightLines = renderEditorLines(args, splitRightWidth(args.width));
|
|
148
|
-
if (args.width >= 100) {
|
|
149
|
-
return splitColumns({
|
|
150
|
-
width: args.width,
|
|
151
|
-
theme: args.theme,
|
|
152
|
-
leftLines,
|
|
153
|
-
rightLines,
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
return [...leftLines, "", ...rightLines];
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (args.previewText && args.width >= 100) {
|
|
160
|
-
const rightLines = [
|
|
161
|
-
args.theme.fg("accent", "Preview"),
|
|
162
|
-
"",
|
|
163
|
-
...renderPrompt(args.previewText, splitRightWidth(args.width)),
|
|
164
|
-
];
|
|
165
|
-
return splitColumns({
|
|
166
|
-
width: args.width,
|
|
167
|
-
theme: args.theme,
|
|
168
|
-
leftLines,
|
|
169
|
-
rightLines,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (!args.previewText) return leftLines;
|
|
174
|
-
return [
|
|
175
|
-
...leftLines,
|
|
176
|
-
"",
|
|
177
|
-
args.theme.fg("accent", "Preview"),
|
|
178
|
-
...renderPrompt(args.previewText, args.width),
|
|
179
|
-
];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function renderTextBody(args: RenderOverlayFrameArgs): string[] {
|
|
183
|
-
const lines = renderEditorLines(args, args.width);
|
|
184
|
-
if (args.textActionLabels.length === 0) return lines;
|
|
185
|
-
|
|
186
|
-
if (args.focus === "actions") {
|
|
187
|
-
return [...lines, "", ...(args.actionList?.render(args.width) ?? [])];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return [...lines, "", ...args.textActionLabels.map((label) => ` ${label}`)];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function renderEditorLines(args: RenderOverlayFrameArgs, width: number): string[] {
|
|
194
|
-
const label =
|
|
195
|
-
args.mode === "discuss-input"
|
|
196
|
-
? "Discuss instead"
|
|
197
|
-
: args.mode === "custom-input"
|
|
198
|
-
? "Other answer"
|
|
199
|
-
: args.mode === "note-input"
|
|
200
|
-
? args.noteTargetLabel
|
|
201
|
-
? `Note for: ${args.noteTargetLabel}`
|
|
202
|
-
: "Option note"
|
|
203
|
-
: "Your answer";
|
|
204
|
-
|
|
205
|
-
const labelLines = wrapTextWithAnsi(args.theme.fg("accent", label), width);
|
|
206
|
-
const lines = [...labelLines, ...args.editor.render(Math.max(20, width - 1))];
|
|
207
|
-
const question = args.controller.currentQuestion;
|
|
208
|
-
if (question.type !== "text" || args.editor.getText()) return lines;
|
|
209
|
-
|
|
210
|
-
if (question.initial) {
|
|
211
|
-
lines.push(...wrapTextWithAnsi(args.theme.fg("dim", `Initial: ${question.initial}`), width));
|
|
212
|
-
} else if (question.placeholder) {
|
|
213
|
-
lines.push(
|
|
214
|
-
...wrapTextWithAnsi(args.theme.fg("dim", `Placeholder: ${question.placeholder}`), width),
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return lines;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function renderPrompt(text: string, width: number): string[] {
|
|
221
|
-
return new Markdown(text, 0, 0, getMarkdownTheme()).render(Math.max(1, width));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function splitLeftWidth(width: number): number {
|
|
225
|
-
return Math.max(36, Math.floor(width * 0.55));
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function splitRightWidth(width: number): number {
|
|
229
|
-
return Math.max(24, width - splitLeftWidth(width) - 3);
|
|
230
|
-
}
|