@lune_99/pi-ask-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/CHANGELOG.md +66 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/docs/question-answer-row.png +0 -0
- package/docs/question-answer-row.svg +16 -0
- package/docs/question-dock.png +0 -0
- package/docs/question-dock.svg +8 -0
- package/docs/question-panel.png +0 -0
- package/docs/question-panel.svg +58 -0
- package/package.json +62 -5
- package/src/commands/question.ts +25 -0
- package/src/core/question-delivery.ts +111 -0
- package/src/core/question-format.ts +109 -0
- package/src/core/question-notification.ts +34 -0
- package/src/core/question-types.ts +91 -0
- package/src/core/questionManager.ts +484 -0
- package/src/index.ts +142 -0
- package/src/tools/ask-user-questions.ts +184 -0
- package/src/tui/panel/question-panel-controller.ts +261 -0
- package/src/tui/panel/question-panel-renderer.ts +813 -0
- package/src/tui/panel/question-panel.ts +702 -0
- package/src/tui/preview/preview-box.ts +168 -0
- package/src/tui/preview/preview-layout.ts +116 -0
- package/src/tui/preview/preview-renderer.ts +129 -0
- package/src/tui/question-answer-message.ts +42 -0
- package/src/tui/question-dock-contribution.ts +41 -0
- package/src/tui/question-dock.ts +68 -0
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionContext,
|
|
3
|
+
Theme,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Editor,
|
|
8
|
+
Key,
|
|
9
|
+
matchesKey,
|
|
10
|
+
type Component,
|
|
11
|
+
type EditorTheme,
|
|
12
|
+
type Focusable,
|
|
13
|
+
type KeybindingsManager,
|
|
14
|
+
type TUI,
|
|
15
|
+
type TuiMouseEvent,
|
|
16
|
+
type TuiMouseEventResult,
|
|
17
|
+
} from "@earendil-works/pi-tui";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
questionManager,
|
|
21
|
+
type AskQuestionDraft,
|
|
22
|
+
type AskQuestionRequest,
|
|
23
|
+
} from "../../core/questionManager.ts";
|
|
24
|
+
import { QuestionPanelController } from "./question-panel-controller.ts";
|
|
25
|
+
import {
|
|
26
|
+
QuestionPanelRenderer,
|
|
27
|
+
type PanelRect,
|
|
28
|
+
type PanelSpan,
|
|
29
|
+
type QuestionPanelLayout,
|
|
30
|
+
} from "./question-panel-renderer.ts";
|
|
31
|
+
|
|
32
|
+
const OVERLAY_WIDTH = "80%";
|
|
33
|
+
const OVERLAY_MIN_WIDTH = 70;
|
|
34
|
+
/**
|
|
35
|
+
* The overlay is clamped to the terminal minus its margins, so a taller share than this buys
|
|
36
|
+
* nothing; 90% is what a short terminal needs for the preview block to fit at all.
|
|
37
|
+
*/
|
|
38
|
+
const OVERLAY_MAX_HEIGHT = "90%";
|
|
39
|
+
const OVERLAY_HEIGHT_RATIO = 0.9;
|
|
40
|
+
const OVERLAY_MARGIN = 2;
|
|
41
|
+
|
|
42
|
+
/** Frame rows: top border, header, two separators, footer, bottom border. */
|
|
43
|
+
const FRAME_HEIGHT = 6;
|
|
44
|
+
|
|
45
|
+
export interface OpenQuestionPanelOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Persist the pending questions after a deferred (Esc) close.
|
|
48
|
+
*
|
|
49
|
+
* Only the extension knows `pi`, and without this hook the answers written before
|
|
50
|
+
* the close would not survive a restart.
|
|
51
|
+
*/
|
|
52
|
+
onDeferred?: () => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Why a panel closed: a settled one hands over to the next pending request, while a deferred
|
|
57
|
+
* one (Esc) leaves its request pending and only closes the overlay.
|
|
58
|
+
*/
|
|
59
|
+
export type PanelCloseReason = "settled" | "deferred";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Ownership of the single overlay.
|
|
63
|
+
*
|
|
64
|
+
* A panel is a long-lived async operation and a session boundary resets module state, so a
|
|
65
|
+
* flag is not enough: a panel that resolves after the reset would clear the ownership of the
|
|
66
|
+
* panel that replaced it. The generation is bumped on every reset, and the owner symbol
|
|
67
|
+
* identifies one invocation, so a stale continuation can neither release nor continue the
|
|
68
|
+
* new panel.
|
|
69
|
+
*/
|
|
70
|
+
let panelGeneration = 0;
|
|
71
|
+
let panelOwner: symbol | undefined;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A session boundary disposes overlays without resolving their custom() promise in some
|
|
75
|
+
* cases, so the ownership must not survive into the next session and block `/question`.
|
|
76
|
+
*/
|
|
77
|
+
export function resetQuestionPanelState(): void {
|
|
78
|
+
panelGeneration++;
|
|
79
|
+
panelOwner = undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Show pending requests, least recently shown first, until there is nothing left to answer.
|
|
84
|
+
*
|
|
85
|
+
* Called immediately after a request is created, and again from `/question`. A request that
|
|
86
|
+
* arrives while a panel is open does not have to wait for `/question`: it is already pending,
|
|
87
|
+
* so the loop picks it up as soon as the current panel settles - and because it was never
|
|
88
|
+
* shown, it comes before a request the user only deferred. Deferring (Esc) ends the loop on
|
|
89
|
+
* purpose - the user asked for quiet, not for the next question - and the request stays pending
|
|
90
|
+
* for `/question`.
|
|
91
|
+
*
|
|
92
|
+
* The panel never settles the request when Esc closes it, so the promise completing only
|
|
93
|
+
* means the overlay is gone, not that the questions were answered.
|
|
94
|
+
*/
|
|
95
|
+
export async function openQuestionPanel(
|
|
96
|
+
ctx: ExtensionContext,
|
|
97
|
+
options: OpenQuestionPanelOptions = {},
|
|
98
|
+
): Promise<void> {
|
|
99
|
+
if (ctx.mode !== "tui" || panelOwner !== undefined) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const generation = panelGeneration;
|
|
104
|
+
const owner = Symbol("question-panel");
|
|
105
|
+
|
|
106
|
+
panelOwner = owner;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
let reason: PanelCloseReason | undefined;
|
|
110
|
+
|
|
111
|
+
do {
|
|
112
|
+
const request = questionManager.nextPendingRequest();
|
|
113
|
+
|
|
114
|
+
if (!request) {
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Claim it before showing it, so a request the user defers does not outrank one
|
|
119
|
+
// that has never been shown.
|
|
120
|
+
questionManager.markShown(request.id);
|
|
121
|
+
|
|
122
|
+
reason = await showQuestionPanel(ctx, request, options);
|
|
123
|
+
|
|
124
|
+
// The session may have been replaced while the overlay was up; that session owns
|
|
125
|
+
// its own panel now, and this one must not continue into its queue.
|
|
126
|
+
if (panelGeneration !== generation || panelOwner !== owner) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
} while (reason === "settled" && questionManager.getPendingRequests().length > 0);
|
|
130
|
+
} finally {
|
|
131
|
+
if (panelGeneration === generation && panelOwner === owner) {
|
|
132
|
+
panelOwner = undefined;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** One overlay for one request; `undefined` when it could not be opened at all. */
|
|
138
|
+
async function showQuestionPanel(
|
|
139
|
+
ctx: ExtensionContext,
|
|
140
|
+
request: AskQuestionRequest,
|
|
141
|
+
options: OpenQuestionPanelOptions,
|
|
142
|
+
): Promise<PanelCloseReason | undefined> {
|
|
143
|
+
try {
|
|
144
|
+
return await ctx.ui.custom<PanelCloseReason>(
|
|
145
|
+
(tui, theme, keybindings, done) =>
|
|
146
|
+
new QuestionPanel({
|
|
147
|
+
request,
|
|
148
|
+
tui,
|
|
149
|
+
theme,
|
|
150
|
+
keybindings,
|
|
151
|
+
close: (reason) => done(reason),
|
|
152
|
+
onDeferred: options.onDeferred,
|
|
153
|
+
}),
|
|
154
|
+
{
|
|
155
|
+
overlay: true,
|
|
156
|
+
overlayOptions: {
|
|
157
|
+
anchor: "center",
|
|
158
|
+
width: OVERLAY_WIDTH,
|
|
159
|
+
minWidth: OVERLAY_MIN_WIDTH,
|
|
160
|
+
maxHeight: OVERLAY_MAX_HEIGHT,
|
|
161
|
+
margin: OVERLAY_MARGIN,
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
// Opening failed: stop instead of retrying into the same error.
|
|
167
|
+
ctx.ui.notify(
|
|
168
|
+
`Question panel failed to open: ${error instanceof Error ? error.message : String(error)}`,
|
|
169
|
+
"error",
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface QuestionPanelOptions {
|
|
177
|
+
request: AskQuestionRequest;
|
|
178
|
+
tui: TUI;
|
|
179
|
+
theme: Theme;
|
|
180
|
+
keybindings: KeybindingsManager;
|
|
181
|
+
close: (reason: PanelCloseReason) => void;
|
|
182
|
+
onDeferred?: () => void;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Overlay adapter of one question request: it routes terminal input to the controller, owns
|
|
187
|
+
* the editor, and turns a controller transition into a render request, an editor mode change
|
|
188
|
+
* or a settle. Drawing is the renderer's job.
|
|
189
|
+
*/
|
|
190
|
+
export class QuestionPanel implements Component, Focusable {
|
|
191
|
+
/** Set by the TUI when this overlay owns input. */
|
|
192
|
+
focused = false;
|
|
193
|
+
|
|
194
|
+
private readonly request: AskQuestionRequest;
|
|
195
|
+
private readonly keybindings: KeybindingsManager;
|
|
196
|
+
private readonly requestRender: () => void;
|
|
197
|
+
private readonly terminalRows: () => number;
|
|
198
|
+
private readonly close: (reason: PanelCloseReason) => void;
|
|
199
|
+
private readonly onDeferred: (() => void) | undefined;
|
|
200
|
+
|
|
201
|
+
private readonly controller: QuestionPanelController;
|
|
202
|
+
private readonly renderer: QuestionPanelRenderer;
|
|
203
|
+
|
|
204
|
+
private readonly editor: Editor;
|
|
205
|
+
private inputMode = false;
|
|
206
|
+
|
|
207
|
+
/** Geometry of the last frame, for mouse hit-testing. */
|
|
208
|
+
private layout: QuestionPanelLayout | undefined;
|
|
209
|
+
/** Wheel position of the body; `undefined` lets the window follow the focused row. */
|
|
210
|
+
private bodyScroll: number | undefined;
|
|
211
|
+
private previewScroll = 0;
|
|
212
|
+
|
|
213
|
+
constructor(options: QuestionPanelOptions) {
|
|
214
|
+
this.request = options.request;
|
|
215
|
+
this.keybindings = options.keybindings;
|
|
216
|
+
this.close = options.close;
|
|
217
|
+
this.onDeferred = options.onDeferred;
|
|
218
|
+
|
|
219
|
+
this.requestRender = () => options.tui.requestRender();
|
|
220
|
+
this.terminalRows = () => options.tui.terminal.rows;
|
|
221
|
+
this.controller = new QuestionPanelController(options.request);
|
|
222
|
+
this.renderer = new QuestionPanelRenderer({
|
|
223
|
+
request: options.request,
|
|
224
|
+
theme: options.theme,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const editorTheme: EditorTheme = {
|
|
228
|
+
borderColor: (text) => options.theme.fg("accent", text),
|
|
229
|
+
selectList: {
|
|
230
|
+
selectedPrefix: (text) => options.theme.fg("accent", text),
|
|
231
|
+
selectedText: (text) => options.theme.fg("accent", text),
|
|
232
|
+
description: (text) => options.theme.fg("muted", text),
|
|
233
|
+
scrollInfo: (text) => options.theme.fg("dim", text),
|
|
234
|
+
noMatch: (text) => options.theme.fg("warning", text),
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
this.editor = new Editor(options.tui, editorTheme, { paddingX: 1 });
|
|
239
|
+
this.editor.onSubmit = (text) => this.commitCustomAnswer(text);
|
|
240
|
+
// The draft is kept in sync while typing, so a persistence pass from any direction
|
|
241
|
+
// (Esc, shutdown, restart) sees the text even if the editor never lost focus.
|
|
242
|
+
this.editor.onChange = () => this.captureEditorDraft();
|
|
243
|
+
|
|
244
|
+
this.syncAutoInputMode();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
handleInput(data: string): void {
|
|
248
|
+
// A key moves the focus, so the window goes back to following it.
|
|
249
|
+
this.bodyScroll = undefined;
|
|
250
|
+
this.withPreviewReset(() => this.handleKey(data));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private handleKey(data: string): void {
|
|
254
|
+
if (this.inputMode) {
|
|
255
|
+
// Esc belongs to the editor first: it drops back to the options while keeping
|
|
256
|
+
// what was written, and only a second Esc defers the whole panel.
|
|
257
|
+
if (this.isCancelKey(data)) {
|
|
258
|
+
this.exitInputMode();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.editor.handleInput(data);
|
|
263
|
+
this.requestRender();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (this.isCancelKey(data)) {
|
|
268
|
+
this.defer();
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Skip is Shift+S, and it is checked before the custom-row shortcut: a free-form
|
|
273
|
+
// question opens its editor by itself, so Esc has to leave a key that still skips,
|
|
274
|
+
// while a plain `s` stays the letter it is everywhere else - the start of an answer.
|
|
275
|
+
// A Kitty-protocol terminal reports a modified key as a CSI sequence instead of the
|
|
276
|
+
// uppercase byte, so matching `"S"` alone would leave skip unreachable there.
|
|
277
|
+
if (data === "S" || matchesKey(data, Key.shift("s"))) {
|
|
278
|
+
this.skipRequest();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// The custom row is where answers are written, so printable keys start the editor
|
|
283
|
+
// there instead of being eaten by the single-key shortcuts below (digits, space,
|
|
284
|
+
// j/k), which keep working on the option rows and on the submit tab.
|
|
285
|
+
if (isTypedCharacter(data) && this.controller.isCustomRowFocused()) {
|
|
286
|
+
this.enterInputMode(data);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (this.controller.isMultiQuestion) {
|
|
291
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
292
|
+
this.moveTab(1);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (matchesKey(data, Key.shift("tab")) || matchesKey(data, Key.left)) {
|
|
297
|
+
this.moveTab(-1);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (this.controller.isSubmitTab) {
|
|
303
|
+
if (this.isConfirmKey(data)) {
|
|
304
|
+
this.submit();
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (this.isUpKey(data) || data === "k") {
|
|
310
|
+
this.moveRow(-1);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (this.isDownKey(data) || data === "j") {
|
|
315
|
+
this.moveRow(1);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (this.isConfirmKey(data)) {
|
|
320
|
+
this.confirmRow();
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (matchesKey(data, Key.space)) {
|
|
325
|
+
this.toggleFocusedRow();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const digit = /^[1-9]$/.exec(data);
|
|
330
|
+
|
|
331
|
+
if (digit) {
|
|
332
|
+
this.pickOption(Number(digit[0]) - 1);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Fullscreen mode routes the mouse here; regular mode leaves it to the terminal, so every
|
|
338
|
+
* action below also has a key. Only the tab strip and the open editor are targets: a press
|
|
339
|
+
* on an option row stays unhandled, so the panel's text can still be selected and copied.
|
|
340
|
+
*/
|
|
341
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
342
|
+
const layout = this.layout;
|
|
343
|
+
|
|
344
|
+
if (!layout) {
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (event.type === "wheel") {
|
|
349
|
+
// Claimed even when nothing moves: an unhandled wheel scrolls the transcript behind.
|
|
350
|
+
return { handled: true, render: this.scrollBy(layout, event) };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (event.button !== "left") {
|
|
354
|
+
return undefined;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (this.inputMode && layout.editor && insideRect(layout.editor, event) && insideRect(layout.body, event)) {
|
|
358
|
+
return this.editor.handleMouse({
|
|
359
|
+
...event,
|
|
360
|
+
x: event.x - layout.editor.x,
|
|
361
|
+
y: event.y - layout.editor.y,
|
|
362
|
+
width: layout.editor.width,
|
|
363
|
+
height: layout.editor.height,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (event.type === "press") {
|
|
368
|
+
return this.withPreviewReset(() => this.pressTabAt(layout, event));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* A preview belongs to one option, so moving the focus away and back starts it at its top;
|
|
376
|
+
* another question starts with the window on its focused row.
|
|
377
|
+
*/
|
|
378
|
+
private withPreviewReset<T>(action: () => T): T {
|
|
379
|
+
const question = this.draft.currentIndex;
|
|
380
|
+
const option = this.draft.optionIndex;
|
|
381
|
+
const result = action();
|
|
382
|
+
|
|
383
|
+
if (this.draft.currentIndex !== question) {
|
|
384
|
+
this.bodyScroll = undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (this.draft.currentIndex !== question || this.draft.optionIndex !== option) {
|
|
388
|
+
this.previewScroll = 0;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
render(width: number): string[] {
|
|
395
|
+
// The hardware cursor belongs to the editor only while it owns the keys.
|
|
396
|
+
this.editor.focused = this.focused && this.inputMode;
|
|
397
|
+
|
|
398
|
+
const frame = this.renderer.render(width, {
|
|
399
|
+
inputMode: this.inputMode,
|
|
400
|
+
maxBodyHeight: this.maxBodyHeight(),
|
|
401
|
+
editor: this.inputMode ? this.editor : undefined,
|
|
402
|
+
bodyScroll: this.bodyScroll,
|
|
403
|
+
previewScroll: this.previewScroll,
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
this.layout = frame.layout;
|
|
407
|
+
|
|
408
|
+
return frame.lines;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
invalidate(): void {
|
|
412
|
+
this.renderer.invalidate();
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private get draft(): AskQuestionDraft {
|
|
416
|
+
return this.request.draft;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* The preview under the pointer scrolls first; at its limits the wheel stays with it.
|
|
421
|
+
*
|
|
422
|
+
* Renders are batched, so several wheel events can arrive against one frame: each step
|
|
423
|
+
* builds on the scroll already requested, not on the offset that frame drew.
|
|
424
|
+
*/
|
|
425
|
+
private scrollBy(layout: QuestionPanelLayout, event: TuiMouseEvent): boolean {
|
|
426
|
+
const delta = event.wheelDelta ?? 0;
|
|
427
|
+
const preview = layout.preview;
|
|
428
|
+
|
|
429
|
+
if (preview && preview.maxOffset > 0 && insideRect(preview, event)) {
|
|
430
|
+
const current = clamp(this.previewScroll, 0, preview.maxOffset);
|
|
431
|
+
const next = clamp(current + delta, 0, preview.maxOffset);
|
|
432
|
+
|
|
433
|
+
this.previewScroll = next;
|
|
434
|
+
return next !== current;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const body = layout.body;
|
|
438
|
+
const current = clamp(this.bodyScroll ?? body.offset, 0, body.maxOffset);
|
|
439
|
+
const next = clamp(current + delta, 0, body.maxOffset);
|
|
440
|
+
|
|
441
|
+
this.bodyScroll = next;
|
|
442
|
+
return next !== current;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** The tab strip is the only row the mouse can act on; option rows stay keyboard-only. */
|
|
446
|
+
private pressTabAt(layout: QuestionPanelLayout, event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
447
|
+
const tab = layout.tabs.find((span) => insideSpan(span, event));
|
|
448
|
+
|
|
449
|
+
if (!tab) {
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const target = tab.target;
|
|
454
|
+
|
|
455
|
+
if (target.kind === "step" || target.index !== this.draft.currentIndex) {
|
|
456
|
+
// Before the switch: the draft is stored under the question being left.
|
|
457
|
+
this.leaveEditorForTab();
|
|
458
|
+
|
|
459
|
+
if (target.kind === "step") {
|
|
460
|
+
this.moveTab(target.step);
|
|
461
|
+
} else {
|
|
462
|
+
this.controller.selectTab(target.index);
|
|
463
|
+
this.inputMode = false;
|
|
464
|
+
this.syncAutoInputMode();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
this.requestRender();
|
|
469
|
+
return { handled: true, focus: true };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Moving away by mouse must keep what was typed, as `Esc` does. */
|
|
473
|
+
private leaveEditorForTab(): void {
|
|
474
|
+
if (this.inputMode) {
|
|
475
|
+
this.captureEditorDraft();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private moveRow(step: number): void {
|
|
480
|
+
this.controller.moveRow(step);
|
|
481
|
+
this.requestRender();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
private moveTab(step: number): void {
|
|
485
|
+
this.controller.moveTab(step);
|
|
486
|
+
this.inputMode = false;
|
|
487
|
+
this.syncAutoInputMode();
|
|
488
|
+
this.requestRender();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
private confirmRow(): void {
|
|
492
|
+
const question = this.controller.currentQuestion();
|
|
493
|
+
const row = this.controller.focusedRow();
|
|
494
|
+
|
|
495
|
+
if (!question || !row) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (row.kind === "custom") {
|
|
500
|
+
this.enterInputMode();
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (row.kind === "next") {
|
|
505
|
+
this.advanceAfterAnswer();
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (question.multiSelect === true) {
|
|
510
|
+
this.toggleOption(row.index);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
this.answerWithOption(row.index);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
private toggleFocusedRow(): void {
|
|
518
|
+
this.controller.toggleFocusedRow();
|
|
519
|
+
this.requestRender();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private toggleOption(index: number): void {
|
|
523
|
+
this.controller.toggleOption(index);
|
|
524
|
+
this.requestRender();
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private pickOption(index: number): void {
|
|
528
|
+
const action = this.controller.pickOption(index);
|
|
529
|
+
|
|
530
|
+
if (action === "answer") {
|
|
531
|
+
this.advanceAfterAnswer();
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (action === "toggle") {
|
|
536
|
+
this.requestRender();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private answerWithOption(index: number): void {
|
|
541
|
+
this.controller.answerWithOption(index);
|
|
542
|
+
this.advanceAfterAnswer();
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
private advanceAfterAnswer(): void {
|
|
546
|
+
if (this.controller.advanceAfterAnswer() === "submit") {
|
|
547
|
+
this.submit();
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
this.inputMode = false;
|
|
552
|
+
this.syncAutoInputMode();
|
|
553
|
+
this.requestRender();
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
private enterInputMode(seed?: string): void {
|
|
557
|
+
const questionIndex = this.draft.currentIndex;
|
|
558
|
+
const draftText = this.draft.customDrafts[questionIndex]
|
|
559
|
+
?? this.draft.answers[questionIndex]?.customText
|
|
560
|
+
?? "";
|
|
561
|
+
|
|
562
|
+
this.inputMode = true;
|
|
563
|
+
this.editor.setText(draftText);
|
|
564
|
+
this.editor.focused = this.focused;
|
|
565
|
+
|
|
566
|
+
if (seed !== undefined) {
|
|
567
|
+
// Let the editor place the key that opened it, so cursor and wrapping stay its job.
|
|
568
|
+
this.editor.handleInput(seed);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
this.requestRender();
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
private exitInputMode(): void {
|
|
575
|
+
this.captureEditorDraft();
|
|
576
|
+
this.inputMode = false;
|
|
577
|
+
this.requestRender();
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
private captureEditorDraft(): void {
|
|
581
|
+
// A large paste lives behind a `[paste #1 +12 lines]` marker in the editor; only
|
|
582
|
+
// getExpandedText() returns the content that has to survive Esc and a restart.
|
|
583
|
+
const text = this.editor.getExpandedText();
|
|
584
|
+
|
|
585
|
+
this.draft.customDrafts[this.draft.currentIndex] = text.length > 0 ? text : undefined;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
private commitCustomAnswer(text: string): void {
|
|
589
|
+
const trimmed = text.trim();
|
|
590
|
+
|
|
591
|
+
// An empty answer would be indistinguishable from unanswered; keep the editor
|
|
592
|
+
// open so the user can type something or leave with Esc.
|
|
593
|
+
if (trimmed.length === 0) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
this.controller.setCustomAnswer(trimmed);
|
|
598
|
+
this.inputMode = false;
|
|
599
|
+
this.advanceAfterAnswer();
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Free-form questions have nothing to pick, so their editor opens immediately. */
|
|
603
|
+
private syncAutoInputMode(): void {
|
|
604
|
+
const question = this.controller.currentQuestion();
|
|
605
|
+
const unanswered = this.draft.answers[this.draft.currentIndex] === undefined;
|
|
606
|
+
|
|
607
|
+
this.inputMode = question !== undefined
|
|
608
|
+
&& (question.options?.length ?? 0) === 0
|
|
609
|
+
&& unanswered;
|
|
610
|
+
|
|
611
|
+
if (this.inputMode) {
|
|
612
|
+
this.editor.setText(this.draft.customDrafts[this.draft.currentIndex] ?? "");
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
private submit(): void {
|
|
617
|
+
if (!this.controller.prepareSubmit()) {
|
|
618
|
+
this.inputMode = false;
|
|
619
|
+
this.syncAutoInputMode();
|
|
620
|
+
this.requestRender();
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if (questionManager.submit(this.request.id, this.controller.collectAnswers())) {
|
|
625
|
+
this.close("settled");
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
private skipRequest(): void {
|
|
630
|
+
if (questionManager.skip(this.request.id)) {
|
|
631
|
+
this.close("settled");
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
private defer(): void {
|
|
636
|
+
if (this.inputMode) {
|
|
637
|
+
this.captureEditorDraft();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
try {
|
|
641
|
+
// The draft is already on the request, so only the snapshot has to be written.
|
|
642
|
+
this.onDeferred?.();
|
|
643
|
+
} catch {
|
|
644
|
+
// A hook failure must not leave the overlay stuck, and it must not reach the
|
|
645
|
+
// terminal input loop either - the hook reports its own errors.
|
|
646
|
+
} finally {
|
|
647
|
+
this.close("deferred");
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
private isConfirmKey(data: string): boolean {
|
|
652
|
+
return matchesKey(data, Key.enter)
|
|
653
|
+
|| this.keybindings.matches(data, "tui.select.confirm")
|
|
654
|
+
|| this.keybindings.matches(data, "tui.input.submit");
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
private isCancelKey(data: string): boolean {
|
|
658
|
+
return matchesKey(data, Key.escape)
|
|
659
|
+
|| this.keybindings.matches(data, "tui.select.cancel");
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
private isUpKey(data: string): boolean {
|
|
663
|
+
return matchesKey(data, Key.up) || this.keybindings.matches(data, "tui.select.up");
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
private isDownKey(data: string): boolean {
|
|
667
|
+
return matchesKey(data, Key.down) || this.keybindings.matches(data, "tui.select.down");
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Upper bound for the body, not a target: the panel is only as tall as its content, and
|
|
672
|
+
* the bound is whatever room the terminal gives the overlay - a percentage of its height,
|
|
673
|
+
* itself limited by the overlay margins. There is no fixed row cap: a request with several
|
|
674
|
+
* questions is meant to be read in one piece, and clipping it to a constant would hide
|
|
675
|
+
* content the user has to answer. Scrolling only steps in when the content genuinely does
|
|
676
|
+
* not fit the screen.
|
|
677
|
+
*/
|
|
678
|
+
private maxBodyHeight(): number {
|
|
679
|
+
const rows = this.terminalRows();
|
|
680
|
+
const usable = Math.min(Math.floor(rows * OVERLAY_HEIGHT_RATIO), rows - OVERLAY_MARGIN * 2);
|
|
681
|
+
|
|
682
|
+
return Math.max(1, usable - FRAME_HEIGHT);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function insideRect(rect: PanelRect, event: TuiMouseEvent): boolean {
|
|
687
|
+
return event.x >= rect.x && event.x < rect.x + rect.width
|
|
688
|
+
&& event.y >= rect.y && event.y < rect.y + rect.height;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function insideSpan(span: PanelSpan, event: TuiMouseEvent): boolean {
|
|
692
|
+
return event.y === span.y && event.x >= span.start && event.x < span.end;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function clamp(value: number, min: number, max: number): number {
|
|
696
|
+
return Math.min(max, Math.max(min, value));
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/** A single printable character, as opposed to an escape sequence or a control key. */
|
|
700
|
+
function isTypedCharacter(data: string): boolean {
|
|
701
|
+
return data.length > 0 && !data.startsWith("\x1b") && !/[\u0000-\u001f\u007f]/.test(data);
|
|
702
|
+
}
|