@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,11 +1,27 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
AskUserInteractionResult,
|
|
3
|
+
AskUserOutcome,
|
|
4
|
+
NormalizedQuestionnaire,
|
|
5
|
+
} from "../types.ts";
|
|
6
|
+
import { runFormQuestionnaire } from "./form.ts";
|
|
3
7
|
import type { RunQuestionnaireOptions } from "./types.ts";
|
|
4
8
|
|
|
5
9
|
export async function runQuestionnaire(
|
|
6
10
|
questionnaire: NormalizedQuestionnaire,
|
|
7
11
|
opts: RunQuestionnaireOptions,
|
|
8
|
-
): Promise<AskUserOutcome | "unsupported"> {
|
|
12
|
+
): Promise<AskUserOutcome | AskUserInteractionResult | "unsupported"> {
|
|
9
13
|
if (typeof opts.ui.custom !== "function") return "unsupported";
|
|
10
|
-
|
|
14
|
+
|
|
15
|
+
const result = await runFormQuestionnaire(questionnaire, opts);
|
|
16
|
+
return isQuestionnaireResult(result) ? result : "unsupported";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isQuestionnaireResult(
|
|
20
|
+
result: AskUserOutcome | AskUserInteractionResult | undefined,
|
|
21
|
+
): result is AskUserOutcome | AskUserInteractionResult {
|
|
22
|
+
if (!result || typeof result !== "object") return false;
|
|
23
|
+
if ("outcome" in result) {
|
|
24
|
+
return result.outcome === "submitted" || result.outcome === "needs_discussion";
|
|
25
|
+
}
|
|
26
|
+
return "kind" in result && (result.kind === "cancel" || result.kind === "abort");
|
|
11
27
|
}
|
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
// biome-ignore lint/style/noExcessiveLinesPerFile: complex keyboard orchestration
|
|
2
|
+
import {
|
|
3
|
+
type Component,
|
|
4
|
+
Editor,
|
|
5
|
+
type EditorTheme,
|
|
6
|
+
type Focusable,
|
|
7
|
+
Key,
|
|
8
|
+
matchesKey,
|
|
9
|
+
} from "@earendil-works/pi-tui";
|
|
10
|
+
import type { NormalizedChoiceQuestion } from "../types.ts";
|
|
11
|
+
import { renderFormFrame } from "./form-render.ts";
|
|
12
|
+
import { defaultChoiceRowIndex, type FocusTarget, type FormMode } from "./form-view.ts";
|
|
13
|
+
import type { FormArgs } from "./types.ts";
|
|
14
|
+
|
|
15
|
+
export class AskUserForm implements Component, Focusable {
|
|
16
|
+
focused: boolean = false;
|
|
17
|
+
private mode: FormMode = "choice";
|
|
18
|
+
private focus: FocusTarget = "choices";
|
|
19
|
+
private readonly editor: Editor;
|
|
20
|
+
private settingEditorText: boolean = false;
|
|
21
|
+
private choiceFocusIndex = 0;
|
|
22
|
+
private readonly choiceFocusByQuestionId = new Map<string, number>();
|
|
23
|
+
private reviewFocusIndex = 0;
|
|
24
|
+
private closed: boolean = false;
|
|
25
|
+
private cachedWidth: number | undefined;
|
|
26
|
+
private cachedEditorFocused: boolean | undefined;
|
|
27
|
+
private cachedLines: string[] | undefined;
|
|
28
|
+
private commentQuestionId: string | undefined;
|
|
29
|
+
private commentOptionValue: string | undefined;
|
|
30
|
+
private returnChoiceFocusIndex: number | undefined;
|
|
31
|
+
private editorContext: string | undefined;
|
|
32
|
+
private returnToReviewAfterEdit: boolean = false;
|
|
33
|
+
private pendingEsc: boolean = false;
|
|
34
|
+
private readonly onAbort: () => void;
|
|
35
|
+
|
|
36
|
+
constructor(private readonly args: FormArgs) {
|
|
37
|
+
this.editor = new Editor(args.tui, makeEditorTheme(args));
|
|
38
|
+
this.editor.onChange = () => {
|
|
39
|
+
if (this.settingEditorText) return;
|
|
40
|
+
this.syncTextAnswerFromEditor();
|
|
41
|
+
this.refresh();
|
|
42
|
+
};
|
|
43
|
+
this.editor.onSubmit = (value) => this.handleEditorSubmit(value);
|
|
44
|
+
this.syncCurrentQuestion();
|
|
45
|
+
this.onAbort = () => {
|
|
46
|
+
this.args.controller.abort();
|
|
47
|
+
this.finish();
|
|
48
|
+
};
|
|
49
|
+
args.signal?.addEventListener("abort", this.onAbort);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
render(width: number): string[] {
|
|
53
|
+
const editorFocused = this.focused && this.focus === "editor";
|
|
54
|
+
this.editor.focused = editorFocused;
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
this.cachedWidth === width &&
|
|
58
|
+
this.cachedEditorFocused === editorFocused &&
|
|
59
|
+
this.cachedLines
|
|
60
|
+
) {
|
|
61
|
+
return this.cachedLines;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.cachedWidth = width;
|
|
65
|
+
this.cachedEditorFocused = editorFocused;
|
|
66
|
+
this.cachedLines = renderFormFrame({
|
|
67
|
+
width,
|
|
68
|
+
theme: this.args.theme,
|
|
69
|
+
controller: this.args.controller,
|
|
70
|
+
mode: this.mode,
|
|
71
|
+
focus: this.focus,
|
|
72
|
+
editor: this.editor,
|
|
73
|
+
choiceFocusIndex: this.choiceFocusIndex,
|
|
74
|
+
reviewFocusIndex: this.reviewFocusIndex,
|
|
75
|
+
previewText: this.currentPreviewText(),
|
|
76
|
+
editorLabel: this.currentEditorLabel(),
|
|
77
|
+
editorContext: this.editorContext,
|
|
78
|
+
});
|
|
79
|
+
return this.cachedLines;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
handleInput(data: string): void {
|
|
83
|
+
if (this.closed || this.args.controller.isTerminal) return;
|
|
84
|
+
|
|
85
|
+
if (this.args.keybindings.matches(data, "app.tools.expand")) {
|
|
86
|
+
this.args.onToggleToolsExpanded?.();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (this.handleEscapeKey(data)) return;
|
|
91
|
+
if (this.handleNavigationKey(data)) return;
|
|
92
|
+
|
|
93
|
+
if (this.mode === "review") {
|
|
94
|
+
this.handleReviewInput(data);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (this.isCommentEditorMode()) {
|
|
99
|
+
this.handleCommentEditorKey(data);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const question = this.args.controller.currentQuestion;
|
|
104
|
+
if (question.type === "text") {
|
|
105
|
+
this.handleTextKey(data);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.handleChoiceKey(data);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private handleEscapeKey(data: string): boolean {
|
|
113
|
+
if (!matchesKey(data, Key.escape)) return false;
|
|
114
|
+
|
|
115
|
+
if (this.isCommentEditorMode()) {
|
|
116
|
+
this.returnFromCommentEditor();
|
|
117
|
+
this.refresh();
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (this.mode === "text") {
|
|
122
|
+
this.pendingEsc = true;
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
if (this.pendingEsc) {
|
|
125
|
+
this.pendingEsc = false;
|
|
126
|
+
this.args.controller.cancel();
|
|
127
|
+
this.finish();
|
|
128
|
+
}
|
|
129
|
+
}, 80);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.args.controller.cancel();
|
|
134
|
+
this.finish();
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private handleNavigationKey(data: string): boolean {
|
|
139
|
+
if (this.isCommentEditorMode()) return false;
|
|
140
|
+
|
|
141
|
+
const direction = this.navigationDirectionFor(data);
|
|
142
|
+
if (!direction) return false;
|
|
143
|
+
|
|
144
|
+
if (direction === "forward" && this.mode !== "review") {
|
|
145
|
+
this.navigateForward();
|
|
146
|
+
} else if (direction === "backward") {
|
|
147
|
+
if (this.mode === "review") {
|
|
148
|
+
this.saveCurrentChoiceFocus();
|
|
149
|
+
this.goToLastQuestion();
|
|
150
|
+
this.syncCurrentQuestion();
|
|
151
|
+
this.refresh();
|
|
152
|
+
} else {
|
|
153
|
+
this.navigateBackward();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private navigationDirectionFor(data: string): "forward" | "backward" | undefined {
|
|
160
|
+
if (matchesKey(data, Key.tab)) return "forward";
|
|
161
|
+
if (matchesKey(data, Key.shift("tab"))) return "backward";
|
|
162
|
+
if (this.mode === "text") return undefined;
|
|
163
|
+
if (matchesKey(data, Key.left)) return "backward";
|
|
164
|
+
if (matchesKey(data, Key.right)) return "forward";
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private navigateForward(): void {
|
|
169
|
+
this.syncTextAnswerFromEditor();
|
|
170
|
+
this.goNext();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private navigateBackward(): void {
|
|
174
|
+
this.syncTextAnswerFromEditor();
|
|
175
|
+
this.saveCurrentChoiceFocus();
|
|
176
|
+
this.returnToReviewAfterEdit = false;
|
|
177
|
+
this.args.controller.goBack();
|
|
178
|
+
this.syncCurrentQuestion();
|
|
179
|
+
this.refresh();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private isCommentEditorMode(): boolean {
|
|
183
|
+
return (
|
|
184
|
+
this.mode === "question-comment" ||
|
|
185
|
+
this.mode === "option-comment" ||
|
|
186
|
+
this.mode === "form-comment"
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
invalidate(): void {
|
|
191
|
+
this.cachedLines = undefined;
|
|
192
|
+
this.editor.invalidate();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
dispose(): void {
|
|
196
|
+
this.closed = true;
|
|
197
|
+
this.args.signal?.removeEventListener("abort", this.onAbort);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ── Review screen ───────────────────────────────────────────────
|
|
201
|
+
|
|
202
|
+
private handleReviewInput(data: string): void {
|
|
203
|
+
const questionCount = this.args.controller.questionnaire.questions.length;
|
|
204
|
+
const submitIndex = questionCount;
|
|
205
|
+
|
|
206
|
+
if (matchesKey(data, Key.enter)) {
|
|
207
|
+
if (this.reviewFocusIndex === submitIndex) {
|
|
208
|
+
this.finish();
|
|
209
|
+
} else {
|
|
210
|
+
this.goToQuestion(this.reviewFocusIndex, { returnToReviewAfterEdit: true });
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (matchesKey(data, Key.up)) {
|
|
216
|
+
this.reviewFocusIndex = Math.max(0, this.reviewFocusIndex - 1);
|
|
217
|
+
this.refresh();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (matchesKey(data, Key.down)) {
|
|
222
|
+
this.reviewFocusIndex = Math.min(submitIndex, this.reviewFocusIndex + 1);
|
|
223
|
+
this.refresh();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (data === "c") {
|
|
228
|
+
this.openFormCommentEditor();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// ── Choice screen ───────────────────────────────────────────────
|
|
233
|
+
|
|
234
|
+
private handleChoiceKey(data: string): void {
|
|
235
|
+
const question = this.args.controller.currentQuestion;
|
|
236
|
+
if (question.type !== "choice") return;
|
|
237
|
+
|
|
238
|
+
if (matchesKey(data, Key.up)) {
|
|
239
|
+
this.choiceFocusIndex = Math.max(0, this.choiceFocusIndex - 1);
|
|
240
|
+
this.refresh();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (matchesKey(data, Key.down)) {
|
|
245
|
+
this.choiceFocusIndex = Math.min(question.options.length - 1, this.choiceFocusIndex + 1);
|
|
246
|
+
this.refresh();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (matchesKey(data, Key.space)) {
|
|
251
|
+
if (question.multi) {
|
|
252
|
+
this.args.controller.toggleChoiceOption(question, this.choiceFocusIndex);
|
|
253
|
+
} else {
|
|
254
|
+
this.args.controller.selectChoiceOption(question, this.choiceFocusIndex);
|
|
255
|
+
}
|
|
256
|
+
this.refresh();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (matchesKey(data, Key.enter)) {
|
|
261
|
+
if (!question.multi && !this.args.controller.isQuestionMarkedUnanswered(question.id)) {
|
|
262
|
+
this.args.controller.selectChoiceOption(question, this.choiceFocusIndex);
|
|
263
|
+
}
|
|
264
|
+
this.goNext();
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (data === "u") {
|
|
269
|
+
this.args.controller.markCurrentQuestionUnanswered();
|
|
270
|
+
this.refresh();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (data === "c") {
|
|
275
|
+
this.openQuestionCommentEditor(question.id);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (data === "n") {
|
|
280
|
+
this.openOptionCommentEditor(question, this.choiceFocusIndex);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ── Text screen ─────────────────────────────────────────────────
|
|
285
|
+
|
|
286
|
+
private handleTextKey(data: string): void {
|
|
287
|
+
if (this.pendingEsc) {
|
|
288
|
+
this.pendingEsc = false;
|
|
289
|
+
if (data === "u") {
|
|
290
|
+
this.args.controller.markCurrentQuestionUnanswered();
|
|
291
|
+
this.setEditorText("");
|
|
292
|
+
this.refresh();
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (data === "c") {
|
|
296
|
+
this.syncTextAnswerFromEditor();
|
|
297
|
+
this.openQuestionCommentEditor(this.args.controller.currentQuestion.id);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (matchesKey(data, Key.alt("c"))) {
|
|
303
|
+
this.syncTextAnswerFromEditor();
|
|
304
|
+
this.openQuestionCommentEditor(this.args.controller.currentQuestion.id);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (matchesKey(data, Key.alt("u"))) {
|
|
309
|
+
this.args.controller.markCurrentQuestionUnanswered();
|
|
310
|
+
this.setEditorText("");
|
|
311
|
+
this.refresh();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
this.editor.handleInput(data);
|
|
316
|
+
this.refresh();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ── Comment editors ─────────────────────────────────────────────
|
|
320
|
+
|
|
321
|
+
private handleCommentEditorKey(data: string): void {
|
|
322
|
+
this.editor.handleInput(data);
|
|
323
|
+
this.refresh();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private handleEditorSubmit(value: string): void {
|
|
327
|
+
if (this.mode === "text") {
|
|
328
|
+
this.args.controller.setTextAnswer(this.args.controller.currentQuestion.id, value);
|
|
329
|
+
this.goNext();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (this.mode === "form-comment") {
|
|
334
|
+
this.args.controller.setComment(value);
|
|
335
|
+
this.returnFromCommentEditor();
|
|
336
|
+
this.refresh();
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (this.mode === "question-comment" && this.commentQuestionId) {
|
|
341
|
+
this.args.controller.setQuestionComment(this.commentQuestionId, value);
|
|
342
|
+
this.returnFromCommentEditor();
|
|
343
|
+
this.refresh();
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (
|
|
348
|
+
this.mode === "option-comment" &&
|
|
349
|
+
this.commentQuestionId &&
|
|
350
|
+
this.commentOptionValue !== undefined
|
|
351
|
+
) {
|
|
352
|
+
const question = this.args.controller.questionnaire.questions.find(
|
|
353
|
+
(q) => q.id === this.commentQuestionId,
|
|
354
|
+
);
|
|
355
|
+
if (question?.type === "choice") {
|
|
356
|
+
const optIndex = question.options.findIndex((o) => o.value === this.commentOptionValue);
|
|
357
|
+
if (optIndex >= 0) {
|
|
358
|
+
this.args.controller.setChoiceOptionComment(question, optIndex, value);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
this.returnFromCommentEditor();
|
|
362
|
+
this.refresh();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private openFormCommentEditor(): void {
|
|
367
|
+
this.commentQuestionId = undefined;
|
|
368
|
+
this.commentOptionValue = undefined;
|
|
369
|
+
this.returnChoiceFocusIndex = undefined;
|
|
370
|
+
this.editorContext = this.args.controller.questionnaire.title ?? "Form";
|
|
371
|
+
this.mode = "form-comment";
|
|
372
|
+
this.focus = "editor";
|
|
373
|
+
this.setEditorText(this.args.controller.comment ?? "");
|
|
374
|
+
this.refresh();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private openQuestionCommentEditor(questionId: string): void {
|
|
378
|
+
this.commentQuestionId = questionId;
|
|
379
|
+
this.commentOptionValue = undefined;
|
|
380
|
+
this.returnChoiceFocusIndex = this.choiceFocusIndex;
|
|
381
|
+
this.editorContext = this.args.controller.currentQuestion.header;
|
|
382
|
+
this.mode = "question-comment";
|
|
383
|
+
this.focus = "editor";
|
|
384
|
+
this.setEditorText(this.args.controller.getQuestionComment(questionId) ?? "");
|
|
385
|
+
this.refresh();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private openOptionCommentEditor(question: NormalizedChoiceQuestion, optionIndex: number): void {
|
|
389
|
+
const opt = question.options[optionIndex];
|
|
390
|
+
if (!opt) return;
|
|
391
|
+
this.commentQuestionId = question.id;
|
|
392
|
+
this.commentOptionValue = opt.value;
|
|
393
|
+
this.returnChoiceFocusIndex = optionIndex;
|
|
394
|
+
this.editorContext = opt.label;
|
|
395
|
+
this.mode = "option-comment";
|
|
396
|
+
this.focus = "editor";
|
|
397
|
+
this.setEditorText(this.args.controller.getOptionComment(question.id, opt.value) ?? "");
|
|
398
|
+
this.refresh();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private returnFromCommentEditor(): void {
|
|
402
|
+
const prevMode = this.mode;
|
|
403
|
+
const questionId = this.commentQuestionId;
|
|
404
|
+
const optionValue = this.commentOptionValue;
|
|
405
|
+
const returnChoiceFocusIndex = this.returnChoiceFocusIndex;
|
|
406
|
+
|
|
407
|
+
this.commentQuestionId = undefined;
|
|
408
|
+
this.commentOptionValue = undefined;
|
|
409
|
+
this.returnChoiceFocusIndex = undefined;
|
|
410
|
+
this.editorContext = undefined;
|
|
411
|
+
|
|
412
|
+
if (prevMode === "form-comment") {
|
|
413
|
+
this.mode = "review";
|
|
414
|
+
this.focus = "review";
|
|
415
|
+
this.setEditorText("");
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (prevMode === "question-comment" || prevMode === "option-comment") {
|
|
420
|
+
this.syncCurrentQuestion();
|
|
421
|
+
this.restoreChoiceFocus(questionId, optionValue, returnChoiceFocusIndex);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ── Navigation helpers ──────────────────────────────────────────
|
|
426
|
+
|
|
427
|
+
private goNext(): void {
|
|
428
|
+
this.saveCurrentChoiceFocus();
|
|
429
|
+
if (this.returnToReviewAfterEdit) {
|
|
430
|
+
this.returnToReviewAfterEdit = false;
|
|
431
|
+
this.mode = "review";
|
|
432
|
+
this.focus = "review";
|
|
433
|
+
this.reviewFocusIndex = this.args.controller.questionnaire.questions.length;
|
|
434
|
+
this.setEditorText("");
|
|
435
|
+
this.refresh();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (
|
|
439
|
+
this.args.controller.currentIndex >=
|
|
440
|
+
this.args.controller.questionnaire.questions.length - 1
|
|
441
|
+
) {
|
|
442
|
+
this.mode = "review";
|
|
443
|
+
this.focus = "review";
|
|
444
|
+
// Focus the Submit row by default so Enter submits immediately.
|
|
445
|
+
this.reviewFocusIndex = this.args.controller.questionnaire.questions.length;
|
|
446
|
+
this.refresh();
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
this.args.controller.goNext();
|
|
450
|
+
this.syncCurrentQuestion();
|
|
451
|
+
this.refresh();
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private goToLastQuestion(): void {
|
|
455
|
+
const lastIndex = this.args.controller.questionnaire.questions.length - 1;
|
|
456
|
+
this.args.controller.goTo(lastIndex);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private goToQuestion(index: number, opts: { returnToReviewAfterEdit?: boolean } = {}): void {
|
|
460
|
+
if (index < 0 || index >= this.args.controller.questionnaire.questions.length) return;
|
|
461
|
+
this.saveCurrentChoiceFocus();
|
|
462
|
+
this.returnToReviewAfterEdit = opts.returnToReviewAfterEdit ?? false;
|
|
463
|
+
this.args.controller.goTo(index);
|
|
464
|
+
const q = this.args.controller.currentQuestion;
|
|
465
|
+
this.mode = q.type === "text" ? "text" : "choice";
|
|
466
|
+
this.focus = q.type === "text" ? "editor" : "choices";
|
|
467
|
+
this.syncCurrentQuestion();
|
|
468
|
+
this.refresh();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
private syncCurrentQuestion(): void {
|
|
472
|
+
const question = this.args.controller.currentQuestion;
|
|
473
|
+
|
|
474
|
+
if (question.type === "text") {
|
|
475
|
+
this.mode = "text";
|
|
476
|
+
this.focus = "editor";
|
|
477
|
+
this.setEditorText(this.args.controller.getTextAnswer(question.id));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
this.mode = "choice";
|
|
482
|
+
this.focus = "choices";
|
|
483
|
+
this.setEditorText("");
|
|
484
|
+
this.choiceFocusIndex =
|
|
485
|
+
this.choiceFocusByQuestionId.get(question.id) ??
|
|
486
|
+
defaultChoiceRowIndex(this.args.controller, question);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private saveCurrentChoiceFocus(): void {
|
|
490
|
+
const question = this.args.controller.currentQuestion;
|
|
491
|
+
if (question.type === "choice") {
|
|
492
|
+
this.choiceFocusByQuestionId.set(question.id, this.choiceFocusIndex);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
private syncTextAnswerFromEditor(): void {
|
|
497
|
+
if (this.mode !== "text") return;
|
|
498
|
+
const question = this.args.controller.currentQuestion;
|
|
499
|
+
if (question.type !== "text") return;
|
|
500
|
+
this.args.controller.setTextAnswer(question.id, this.editor.getExpandedText());
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
private restoreChoiceFocus(
|
|
504
|
+
questionId: string | undefined,
|
|
505
|
+
optionValue: string | undefined,
|
|
506
|
+
fallbackIndex: number | undefined,
|
|
507
|
+
): void {
|
|
508
|
+
const question = this.args.controller.currentQuestion;
|
|
509
|
+
if (question.type !== "choice" || question.id !== questionId) return;
|
|
510
|
+
|
|
511
|
+
const optionIndex =
|
|
512
|
+
optionValue === undefined
|
|
513
|
+
? fallbackIndex
|
|
514
|
+
: question.options.findIndex((option) => option.value === optionValue);
|
|
515
|
+
if (optionIndex === undefined || optionIndex < 0) return;
|
|
516
|
+
|
|
517
|
+
this.choiceFocusIndex = Math.min(optionIndex, question.options.length - 1);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
private setEditorText(value: string): void {
|
|
521
|
+
this.settingEditorText = true;
|
|
522
|
+
try {
|
|
523
|
+
this.editor.setText(value);
|
|
524
|
+
} finally {
|
|
525
|
+
this.settingEditorText = false;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
private finish(): void {
|
|
530
|
+
if (this.closed) return;
|
|
531
|
+
this.closed = true;
|
|
532
|
+
this.args.signal?.removeEventListener("abort", this.onAbort);
|
|
533
|
+
const interactionResult = this.args.controller.getInteractionResult();
|
|
534
|
+
this.args.done(interactionResult ?? this.args.controller.outcome());
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
private refresh(): void {
|
|
538
|
+
this.cachedLines = undefined;
|
|
539
|
+
this.args.tui.requestRender();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
private currentPreviewText(): string | undefined {
|
|
543
|
+
if (this.mode !== "choice") return undefined;
|
|
544
|
+
const question = this.args.controller.currentQuestion;
|
|
545
|
+
if (question.type !== "choice") return undefined;
|
|
546
|
+
return question.options[this.choiceFocusIndex]?.preview;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
private currentEditorLabel(): string | undefined {
|
|
550
|
+
switch (this.mode) {
|
|
551
|
+
case "question-comment":
|
|
552
|
+
return "Question comment";
|
|
553
|
+
case "option-comment":
|
|
554
|
+
return "Option comment";
|
|
555
|
+
case "form-comment":
|
|
556
|
+
return "Form comment";
|
|
557
|
+
default:
|
|
558
|
+
return undefined;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function makeEditorTheme(args: FormArgs): EditorTheme {
|
|
564
|
+
return {
|
|
565
|
+
borderColor: (text) => args.theme.fg("accent", text),
|
|
566
|
+
selectList: {
|
|
567
|
+
selectedPrefix: (text) => args.theme.fg("accent", text),
|
|
568
|
+
selectedText: (text) => args.theme.fg("accent", text),
|
|
569
|
+
description: (text) => args.theme.fg("muted", text),
|
|
570
|
+
scrollInfo: (text) => args.theme.fg("dim", text),
|
|
571
|
+
noMatch: (text) => args.theme.fg("warning", text),
|
|
572
|
+
},
|
|
573
|
+
};
|
|
574
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { getMarkdownTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Markdown, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
export function renderMiniBox(
|
|
5
|
+
theme: Theme,
|
|
6
|
+
title: string,
|
|
7
|
+
bodyLines: string[],
|
|
8
|
+
width: number,
|
|
9
|
+
): string[] {
|
|
10
|
+
const safe = safeWidth(width);
|
|
11
|
+
if (safe < 8) {
|
|
12
|
+
return [theme.fg("accent", title), ...bodyLines].map((line) => truncateToWidth(line, safe));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const innerWidth = Math.max(1, safe - 4);
|
|
16
|
+
const border = (text: string) => theme.fg("borderMuted", text);
|
|
17
|
+
const lines = [
|
|
18
|
+
border(`╭${"─".repeat(safe - 2)}╮`),
|
|
19
|
+
`${border("│")} ${padRight(theme.fg("accent", theme.bold(title)), innerWidth)} ${border("│")}`,
|
|
20
|
+
...bodyLines.map((line) => `${border("│")} ${padRight(line, innerWidth)} ${border("│")}`),
|
|
21
|
+
border(`╰${"─".repeat(safe - 2)}╯`),
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
return lines.map((line) => truncateToWidth(line, safe));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function pushWrappedWithPrefix(args: {
|
|
28
|
+
lines: string[];
|
|
29
|
+
prefix: string;
|
|
30
|
+
text: string;
|
|
31
|
+
width: number;
|
|
32
|
+
}): void {
|
|
33
|
+
const width = safeWidth(args.width);
|
|
34
|
+
const prefixWidth = visibleWidth(args.prefix);
|
|
35
|
+
const textWidth = Math.max(1, width - prefixWidth);
|
|
36
|
+
const wrapped = wrapTextWithAnsi(args.text, textWidth);
|
|
37
|
+
const continuationPrefix = " ".repeat(prefixWidth);
|
|
38
|
+
|
|
39
|
+
for (let index = 0; index < wrapped.length; index += 1) {
|
|
40
|
+
args.lines.push(`${index === 0 ? args.prefix : continuationPrefix}${wrapped[index]}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function renderPrompt(text: string, width: number): string[] {
|
|
45
|
+
return new Markdown(text, 0, 0, getMarkdownTheme()).render(safeWidth(width));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function formatSplitLine(left: string, right: string, width: number): string {
|
|
49
|
+
const safe = safeWidth(width);
|
|
50
|
+
const leftWidth = visibleWidth(left);
|
|
51
|
+
const rightWidth = visibleWidth(right);
|
|
52
|
+
if (leftWidth + rightWidth + 2 <= safe) {
|
|
53
|
+
return `${left}${" ".repeat(safe - leftWidth - rightWidth)}${right}`;
|
|
54
|
+
}
|
|
55
|
+
return truncateToWidth(`${left} ${right}`, safe);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function padRight(text: string, width: number): string {
|
|
59
|
+
const safe = safeWidth(width);
|
|
60
|
+
const truncated = truncateToWidth(text, safe);
|
|
61
|
+
const padding = Math.max(0, safe - visibleWidth(truncated));
|
|
62
|
+
return `${truncated}${" ".repeat(padding)}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function wrapLines(lines: string[], width: number): string[] {
|
|
66
|
+
return lines.flatMap((line) => wrapTextWithAnsi(line, safeWidth(width)));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function safeWidth(width: number): number {
|
|
70
|
+
return Math.max(1, width);
|
|
71
|
+
}
|