@lune_99/pi-ask-question 0.1.0 → 0.2.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 +38 -2
- package/README.md +31 -22
- package/package.json +6 -6
- package/src/commands/question.ts +17 -7
- package/src/core/question-delivery.ts +81 -29
- package/src/core/question-format.ts +36 -11
- package/src/core/questionManager.ts +302 -78
- package/src/index.ts +64 -35
- package/src/tools/ask-user-questions.ts +142 -56
- package/src/tools/withdraw-user-questions.ts +169 -0
- package/src/tui/dock-line.ts +42 -0
- package/src/tui/panel/clipboard-paste.ts +124 -0
- package/src/tui/panel/question-panel-controller.ts +13 -12
- package/src/tui/panel/question-panel-renderer.ts +180 -126
- package/src/tui/panel/question-panel.ts +259 -97
- package/src/tui/preview/preview-box.ts +22 -15
- package/src/tui/preview/preview-layout.ts +7 -4
- package/src/tui/preview/preview-renderer.ts +6 -1
- package/src/tui/question-answer-message.ts +6 -4
- package/src/tui/question-dock-contribution.ts +63 -23
- package/src/tui/question-dock.ts +58 -21
- package/docs/question-answer-row.png +0 -0
- package/docs/question-answer-row.svg +0 -16
- package/docs/question-dock.png +0 -0
- package/docs/question-dock.svg +0 -8
- package/docs/question-panel.svg +0 -58
- package/src/core/question-notification.ts +0 -34
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ExtensionContext,
|
|
3
|
-
Theme,
|
|
4
|
-
} from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionContext, ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
5
2
|
|
|
6
3
|
import {
|
|
7
4
|
Editor,
|
|
@@ -11,16 +8,19 @@ import {
|
|
|
11
8
|
type EditorTheme,
|
|
12
9
|
type Focusable,
|
|
13
10
|
type KeybindingsManager,
|
|
11
|
+
type SizeValue,
|
|
14
12
|
type TUI,
|
|
15
13
|
type TuiMouseEvent,
|
|
16
14
|
type TuiMouseEventResult,
|
|
17
15
|
} from "@earendil-works/pi-tui";
|
|
18
16
|
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
import { requestHeaders } from "../../core/question-format.ts";
|
|
18
|
+
import type {
|
|
19
|
+
AskQuestionDraft,
|
|
20
|
+
AskQuestionRequest,
|
|
21
|
+
QuestionManager,
|
|
23
22
|
} from "../../core/questionManager.ts";
|
|
23
|
+
import { readClipboardPaste, type ClipboardPaste } from "./clipboard-paste.ts";
|
|
24
24
|
import { QuestionPanelController } from "./question-panel-controller.ts";
|
|
25
25
|
import {
|
|
26
26
|
QuestionPanelRenderer,
|
|
@@ -35,13 +35,16 @@ const OVERLAY_MIN_WIDTH = 70;
|
|
|
35
35
|
* The overlay is clamped to the terminal minus its margins, so a taller share than this buys
|
|
36
36
|
* nothing; 90% is what a short terminal needs for the preview block to fit at all.
|
|
37
37
|
*/
|
|
38
|
-
const OVERLAY_MAX_HEIGHT = "90%";
|
|
39
38
|
const OVERLAY_HEIGHT_RATIO = 0.9;
|
|
39
|
+
const OVERLAY_MAX_HEIGHT: SizeValue = `${OVERLAY_HEIGHT_RATIO * 100}%`;
|
|
40
40
|
const OVERLAY_MARGIN = 2;
|
|
41
41
|
|
|
42
42
|
/** Frame rows: top border, header, two separators, footer, bottom border. */
|
|
43
43
|
const FRAME_HEIGHT = 6;
|
|
44
44
|
|
|
45
|
+
/** pi-tui delivers a terminal paste as one input that starts with this marker. */
|
|
46
|
+
const BRACKETED_PASTE_START = "\x1b[200~";
|
|
47
|
+
|
|
45
48
|
export interface OpenQuestionPanelOptions {
|
|
46
49
|
/**
|
|
47
50
|
* Persist the pending questions after a deferred (Esc) close.
|
|
@@ -53,83 +56,90 @@ export interface OpenQuestionPanelOptions {
|
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
/**
|
|
56
|
-
* Why a panel closed: a settled one hands over to the next pending request, while a
|
|
57
|
-
* one (Esc) leaves its request pending and only closes the overlay.
|
|
59
|
+
* Why a panel closed: a settled or withdrawn one hands over to the next pending request, while a
|
|
60
|
+
* deferred one (Esc) leaves its request pending and only closes the overlay.
|
|
58
61
|
*/
|
|
59
|
-
export type PanelCloseReason = "settled" | "deferred";
|
|
62
|
+
export type PanelCloseReason = "settled" | "deferred" | "withdrawn";
|
|
60
63
|
|
|
61
64
|
/**
|
|
62
|
-
* Ownership of the single overlay.
|
|
65
|
+
* Ownership of the single overlay for the requests of one question manager.
|
|
63
66
|
*
|
|
64
|
-
* A panel is a long-lived async operation and a session boundary resets
|
|
67
|
+
* A panel is a long-lived async operation and a session boundary resets the ownership, so a
|
|
65
68
|
* flag is not enough: a panel that resolves after the reset would clear the ownership of the
|
|
66
69
|
* panel that replaced it. The generation is bumped on every reset, and the owner symbol
|
|
67
70
|
* identifies one invocation, so a stale continuation can neither release nor continue the
|
|
68
71
|
* new panel.
|
|
69
72
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
}
|
|
73
|
+
export class QuestionPanels {
|
|
74
|
+
private readonly manager: QuestionManager;
|
|
75
|
+
private generation = 0;
|
|
76
|
+
private owner: symbol | undefined;
|
|
81
77
|
|
|
82
|
-
|
|
83
|
-
|
|
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;
|
|
78
|
+
constructor(manager: QuestionManager) {
|
|
79
|
+
this.manager = manager;
|
|
101
80
|
}
|
|
102
81
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
do {
|
|
112
|
-
const request = questionManager.nextPendingRequest();
|
|
82
|
+
/**
|
|
83
|
+
* A session boundary disposes overlays without resolving their custom() promise in some
|
|
84
|
+
* cases, so the ownership must not survive the boundary and block `/question`.
|
|
85
|
+
*/
|
|
86
|
+
reset(): void {
|
|
87
|
+
this.generation++;
|
|
88
|
+
this.owner = undefined;
|
|
89
|
+
}
|
|
113
90
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Show pending requests in `nextPendingRequest` order until there is nothing left to answer.
|
|
93
|
+
*
|
|
94
|
+
* Called immediately after a request is created, and again from `/question`. A request that
|
|
95
|
+
* arrives while a panel is open does not have to wait for `/question`: it is already pending,
|
|
96
|
+
* so the loop picks it up as soon as the current panel settles or is withdrawn - and because
|
|
97
|
+
* it was never shown, it comes before a request the user only deferred. Deferring (Esc) ends
|
|
98
|
+
* the loop on purpose - the user asked for quiet, not for the next question - and the request
|
|
99
|
+
* stays pending for `/question`.
|
|
100
|
+
*
|
|
101
|
+
* The panel never settles the request when Esc closes it, so the promise completing only
|
|
102
|
+
* means the overlay is gone, not that the questions were answered.
|
|
103
|
+
*/
|
|
104
|
+
async open(ctx: ExtensionContext, options: OpenQuestionPanelOptions = {}): Promise<void> {
|
|
105
|
+
if (ctx.mode !== "tui" || this.owner !== undefined) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
117
108
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
questionManager.markShown(request.id);
|
|
109
|
+
const generation = this.generation;
|
|
110
|
+
const owner = Symbol("question-panel");
|
|
121
111
|
|
|
122
|
-
|
|
112
|
+
this.owner = owner;
|
|
123
113
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
114
|
+
try {
|
|
115
|
+
let reason: PanelCloseReason | undefined;
|
|
116
|
+
|
|
117
|
+
do {
|
|
118
|
+
const request = this.manager.nextPendingRequest();
|
|
119
|
+
|
|
120
|
+
if (!request) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Claim it before showing it, so a request the user defers does not outrank one
|
|
125
|
+
// that has never been shown.
|
|
126
|
+
this.manager.markShown(request.id);
|
|
127
|
+
|
|
128
|
+
reason = await showQuestionPanel(ctx, this.manager, request, options);
|
|
129
|
+
|
|
130
|
+
// The session ended or changed branch while the overlay was up; this panel must
|
|
131
|
+
// not continue into the queue that replaced it.
|
|
132
|
+
if (this.generation !== generation || this.owner !== owner) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
} while (
|
|
136
|
+
(reason === "settled" || reason === "withdrawn") &&
|
|
137
|
+
this.manager.getPendingRequests().length > 0
|
|
138
|
+
);
|
|
139
|
+
} finally {
|
|
140
|
+
if (this.generation === generation && this.owner === owner) {
|
|
141
|
+
this.owner = undefined;
|
|
128
142
|
}
|
|
129
|
-
} while (reason === "settled" && questionManager.getPendingRequests().length > 0);
|
|
130
|
-
} finally {
|
|
131
|
-
if (panelGeneration === generation && panelOwner === owner) {
|
|
132
|
-
panelOwner = undefined;
|
|
133
143
|
}
|
|
134
144
|
}
|
|
135
145
|
}
|
|
@@ -137,20 +147,39 @@ export async function openQuestionPanel(
|
|
|
137
147
|
/** One overlay for one request; `undefined` when it could not be opened at all. */
|
|
138
148
|
async function showQuestionPanel(
|
|
139
149
|
ctx: ExtensionContext,
|
|
150
|
+
manager: QuestionManager,
|
|
140
151
|
request: AskQuestionRequest,
|
|
141
152
|
options: OpenQuestionPanelOptions,
|
|
142
153
|
): Promise<PanelCloseReason | undefined> {
|
|
154
|
+
let unsubscribe: (() => void) | undefined;
|
|
155
|
+
|
|
143
156
|
try {
|
|
144
157
|
return await ctx.ui.custom<PanelCloseReason>(
|
|
145
|
-
(tui, theme, keybindings, done) =>
|
|
146
|
-
new QuestionPanel({
|
|
158
|
+
(tui, theme, keybindings, done) => {
|
|
159
|
+
const panel = new QuestionPanel({
|
|
160
|
+
manager,
|
|
147
161
|
request,
|
|
148
162
|
tui,
|
|
149
163
|
theme,
|
|
150
164
|
keybindings,
|
|
151
165
|
close: (reason) => done(reason),
|
|
152
166
|
onDeferred: options.onDeferred,
|
|
153
|
-
|
|
167
|
+
notify: (message, type) => ctx.ui.notify(message, type),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// The withdrawal comes from a tool call, not from this panel's input.
|
|
171
|
+
unsubscribe = manager.subscribe((event) => {
|
|
172
|
+
if (event.type === "request-withdrawn" && event.id === request.id) {
|
|
173
|
+
panel.closeWithdrawn();
|
|
174
|
+
ctx.ui.notify(
|
|
175
|
+
`The agent withdrew: ${requestHeaders(request)} (${event.reason})`,
|
|
176
|
+
"info",
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return panel;
|
|
182
|
+
},
|
|
154
183
|
{
|
|
155
184
|
overlay: true,
|
|
156
185
|
overlayOptions: {
|
|
@@ -170,16 +199,21 @@ async function showQuestionPanel(
|
|
|
170
199
|
);
|
|
171
200
|
|
|
172
201
|
return undefined;
|
|
202
|
+
} finally {
|
|
203
|
+
unsubscribe?.();
|
|
173
204
|
}
|
|
174
205
|
}
|
|
175
206
|
|
|
176
207
|
export interface QuestionPanelOptions {
|
|
208
|
+
manager: QuestionManager;
|
|
177
209
|
request: AskQuestionRequest;
|
|
178
210
|
tui: TUI;
|
|
179
211
|
theme: Theme;
|
|
180
212
|
keybindings: KeybindingsManager;
|
|
181
213
|
close: (reason: PanelCloseReason) => void;
|
|
182
214
|
onDeferred?: () => void;
|
|
215
|
+
/** Reports a failed clipboard paste; only the extension holds `ctx.ui`. */
|
|
216
|
+
notify?: ExtensionUIContext["notify"];
|
|
183
217
|
}
|
|
184
218
|
|
|
185
219
|
/**
|
|
@@ -191,18 +225,22 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
191
225
|
/** Set by the TUI when this overlay owns input. */
|
|
192
226
|
focused = false;
|
|
193
227
|
|
|
228
|
+
private readonly manager: QuestionManager;
|
|
194
229
|
private readonly request: AskQuestionRequest;
|
|
195
230
|
private readonly keybindings: KeybindingsManager;
|
|
196
231
|
private readonly requestRender: () => void;
|
|
197
232
|
private readonly terminalRows: () => number;
|
|
198
233
|
private readonly close: (reason: PanelCloseReason) => void;
|
|
199
234
|
private readonly onDeferred: (() => void) | undefined;
|
|
235
|
+
private readonly notify: ExtensionUIContext["notify"] | undefined;
|
|
200
236
|
|
|
201
237
|
private readonly controller: QuestionPanelController;
|
|
202
238
|
private readonly renderer: QuestionPanelRenderer;
|
|
203
239
|
|
|
204
240
|
private readonly editor: Editor;
|
|
205
241
|
private inputMode = false;
|
|
242
|
+
/** Set once the panel has closed itself, so a clipboard read that returns late is dropped. */
|
|
243
|
+
private closed = false;
|
|
206
244
|
|
|
207
245
|
/** Geometry of the last frame, for mouse hit-testing. */
|
|
208
246
|
private layout: QuestionPanelLayout | undefined;
|
|
@@ -211,16 +249,21 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
211
249
|
private previewScroll = 0;
|
|
212
250
|
|
|
213
251
|
constructor(options: QuestionPanelOptions) {
|
|
252
|
+
this.manager = options.manager;
|
|
214
253
|
this.request = options.request;
|
|
215
254
|
this.keybindings = options.keybindings;
|
|
216
|
-
this.close =
|
|
255
|
+
this.close = (reason) => {
|
|
256
|
+
this.closed = true;
|
|
257
|
+
options.close(reason);
|
|
258
|
+
};
|
|
217
259
|
this.onDeferred = options.onDeferred;
|
|
260
|
+
this.notify = options.notify;
|
|
218
261
|
|
|
219
262
|
this.requestRender = () => options.tui.requestRender();
|
|
220
263
|
this.terminalRows = () => options.tui.terminal.rows;
|
|
221
264
|
this.controller = new QuestionPanelController(options.request);
|
|
222
265
|
this.renderer = new QuestionPanelRenderer({
|
|
223
|
-
|
|
266
|
+
view: this.controller,
|
|
224
267
|
theme: options.theme,
|
|
225
268
|
});
|
|
226
269
|
|
|
@@ -259,6 +302,20 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
259
302
|
return;
|
|
260
303
|
}
|
|
261
304
|
|
|
305
|
+
// pi's editor handles this key itself; this editor is a plain one that drops it.
|
|
306
|
+
if (this.isClipboardPasteKey(data)) {
|
|
307
|
+
void this.pasteFromClipboard();
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const step = this.editorTabStep(data);
|
|
312
|
+
|
|
313
|
+
if (step !== 0) {
|
|
314
|
+
this.leaveEditorForTab();
|
|
315
|
+
this.moveTab(step);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
262
319
|
this.editor.handleInput(data);
|
|
263
320
|
this.requestRender();
|
|
264
321
|
return;
|
|
@@ -279,12 +336,20 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
279
336
|
return;
|
|
280
337
|
}
|
|
281
338
|
|
|
282
|
-
// The custom row is where answers are written, so printable keys start the
|
|
283
|
-
// there instead of being eaten by the single-key shortcuts below (digits, space,
|
|
339
|
+
// The custom row is where answers are written, so printable keys and pastes start the
|
|
340
|
+
// editor there instead of being eaten by the single-key shortcuts below (digits, space,
|
|
284
341
|
// j/k), which keep working on the option rows and on the submit tab.
|
|
285
|
-
if (
|
|
286
|
-
this.
|
|
287
|
-
|
|
342
|
+
if (this.controller.isCustomRowFocused()) {
|
|
343
|
+
if (this.isClipboardPasteKey(data)) {
|
|
344
|
+
this.enterInputMode();
|
|
345
|
+
void this.pasteFromClipboard();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (isTypedCharacter(data) || data.startsWith(BRACKETED_PASTE_START)) {
|
|
350
|
+
this.enterInputMode(data);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
288
353
|
}
|
|
289
354
|
|
|
290
355
|
if (this.controller.isMultiQuestion) {
|
|
@@ -354,7 +419,12 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
354
419
|
return undefined;
|
|
355
420
|
}
|
|
356
421
|
|
|
357
|
-
if (
|
|
422
|
+
if (
|
|
423
|
+
this.inputMode &&
|
|
424
|
+
layout.editor &&
|
|
425
|
+
insideRect(layout.editor, event) &&
|
|
426
|
+
insideRect(layout.body, event)
|
|
427
|
+
) {
|
|
358
428
|
return this.editor.handleMouse({
|
|
359
429
|
...event,
|
|
360
430
|
x: event.x - layout.editor.x,
|
|
@@ -412,6 +482,13 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
412
482
|
this.renderer.invalidate();
|
|
413
483
|
}
|
|
414
484
|
|
|
485
|
+
/** The request was withdrawn under the panel; its draft goes with it. */
|
|
486
|
+
closeWithdrawn(): void {
|
|
487
|
+
if (!this.closed) {
|
|
488
|
+
this.close("withdrawn");
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
415
492
|
private get draft(): AskQuestionDraft {
|
|
416
493
|
return this.request.draft;
|
|
417
494
|
}
|
|
@@ -443,7 +520,10 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
443
520
|
}
|
|
444
521
|
|
|
445
522
|
/** The tab strip is the only row the mouse can act on; option rows stay keyboard-only. */
|
|
446
|
-
private pressTabAt(
|
|
523
|
+
private pressTabAt(
|
|
524
|
+
layout: QuestionPanelLayout,
|
|
525
|
+
event: TuiMouseEvent,
|
|
526
|
+
): TuiMouseEventResult | undefined {
|
|
447
527
|
const tab = layout.tabs.find((span) => insideSpan(span, event));
|
|
448
528
|
|
|
449
529
|
if (!tab) {
|
|
@@ -469,7 +549,36 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
469
549
|
return { handled: true, focus: true };
|
|
470
550
|
}
|
|
471
551
|
|
|
472
|
-
/**
|
|
552
|
+
/**
|
|
553
|
+
* Tab step while the editor owns the keys. A free-form question opens its editor by itself,
|
|
554
|
+
* so without this the tabs could not be left by key. Tab has no other use in this editor,
|
|
555
|
+
* while the arrows move its cursor and only switch while there is no text to move through.
|
|
556
|
+
*/
|
|
557
|
+
private editorTabStep(data: string): number {
|
|
558
|
+
if (!this.controller.isMultiQuestion) {
|
|
559
|
+
return 0;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (matchesKey(data, Key.tab)) {
|
|
563
|
+
return 1;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (matchesKey(data, Key.shift("tab"))) {
|
|
567
|
+
return -1;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (this.editor.getText().length > 0) {
|
|
571
|
+
return 0;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (matchesKey(data, Key.right)) {
|
|
575
|
+
return 1;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return matchesKey(data, Key.left) ? -1 : 0;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Moving away from the editor must keep what was typed, as `Esc` does. */
|
|
473
582
|
private leaveEditorForTab(): void {
|
|
474
583
|
if (this.inputMode) {
|
|
475
584
|
this.captureEditorDraft();
|
|
@@ -555,9 +664,10 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
555
664
|
|
|
556
665
|
private enterInputMode(seed?: string): void {
|
|
557
666
|
const questionIndex = this.draft.currentIndex;
|
|
558
|
-
const draftText =
|
|
559
|
-
|
|
560
|
-
??
|
|
667
|
+
const draftText =
|
|
668
|
+
this.draft.customDrafts[questionIndex] ??
|
|
669
|
+
this.draft.answers[questionIndex]?.customText ??
|
|
670
|
+
"";
|
|
561
671
|
|
|
562
672
|
this.inputMode = true;
|
|
563
673
|
this.editor.setText(draftText);
|
|
@@ -585,6 +695,50 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
585
695
|
this.draft.customDrafts[this.draft.currentIndex] = text.length > 0 ? text : undefined;
|
|
586
696
|
}
|
|
587
697
|
|
|
698
|
+
/**
|
|
699
|
+
* The read is async, so by the time it returns the editor may be closed or show another
|
|
700
|
+
* question; the paste is then dropped instead of landing in an answer it was not meant for.
|
|
701
|
+
*/
|
|
702
|
+
private async pasteFromClipboard(): Promise<void> {
|
|
703
|
+
const question = this.draft.currentIndex;
|
|
704
|
+
let paste: ClipboardPaste | undefined;
|
|
705
|
+
|
|
706
|
+
try {
|
|
707
|
+
paste = await readClipboardPaste();
|
|
708
|
+
} catch (error) {
|
|
709
|
+
this.notify?.(
|
|
710
|
+
`Failed to paste from clipboard: ${error instanceof Error ? error.message : String(error)}`,
|
|
711
|
+
"error",
|
|
712
|
+
);
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (!paste || this.closed || !this.inputMode || this.draft.currentIndex !== question) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
let text = paste.text;
|
|
721
|
+
|
|
722
|
+
if (paste.isPath) {
|
|
723
|
+
// A path glued to a word would read as one token to the model.
|
|
724
|
+
const cursor = this.editor.getCursor();
|
|
725
|
+
const line = this.editor.getLines()[cursor.line] ?? "";
|
|
726
|
+
const before = line[cursor.col - 1];
|
|
727
|
+
const after = line[cursor.col];
|
|
728
|
+
|
|
729
|
+
if (before !== undefined && !/\s/.test(before)) {
|
|
730
|
+
text = ` ${text}`;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if (after !== undefined && !/\s/.test(after)) {
|
|
734
|
+
text = `${text} `;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
this.editor.insertTextAtCursor(text);
|
|
739
|
+
this.requestRender();
|
|
740
|
+
}
|
|
741
|
+
|
|
588
742
|
private commitCustomAnswer(text: string): void {
|
|
589
743
|
const trimmed = text.trim();
|
|
590
744
|
|
|
@@ -604,9 +758,8 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
604
758
|
const question = this.controller.currentQuestion();
|
|
605
759
|
const unanswered = this.draft.answers[this.draft.currentIndex] === undefined;
|
|
606
760
|
|
|
607
|
-
this.inputMode =
|
|
608
|
-
&& (question.options?.length ?? 0) === 0
|
|
609
|
-
&& unanswered;
|
|
761
|
+
this.inputMode =
|
|
762
|
+
question !== undefined && (question.options?.length ?? 0) === 0 && unanswered;
|
|
610
763
|
|
|
611
764
|
if (this.inputMode) {
|
|
612
765
|
this.editor.setText(this.draft.customDrafts[this.draft.currentIndex] ?? "");
|
|
@@ -621,13 +774,13 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
621
774
|
return;
|
|
622
775
|
}
|
|
623
776
|
|
|
624
|
-
if (
|
|
777
|
+
if (this.manager.submit(this.request.id, this.controller.collectAnswers())) {
|
|
625
778
|
this.close("settled");
|
|
626
779
|
}
|
|
627
780
|
}
|
|
628
781
|
|
|
629
782
|
private skipRequest(): void {
|
|
630
|
-
if (
|
|
783
|
+
if (this.manager.skip(this.request.id)) {
|
|
631
784
|
this.close("settled");
|
|
632
785
|
}
|
|
633
786
|
}
|
|
@@ -649,14 +802,19 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
649
802
|
}
|
|
650
803
|
|
|
651
804
|
private isConfirmKey(data: string): boolean {
|
|
652
|
-
return
|
|
653
|
-
|
|
654
|
-
|
|
805
|
+
return (
|
|
806
|
+
matchesKey(data, Key.enter) ||
|
|
807
|
+
this.keybindings.matches(data, "tui.select.confirm") ||
|
|
808
|
+
this.keybindings.matches(data, "tui.input.submit")
|
|
809
|
+
);
|
|
655
810
|
}
|
|
656
811
|
|
|
657
812
|
private isCancelKey(data: string): boolean {
|
|
658
|
-
return matchesKey(data, Key.escape)
|
|
659
|
-
|
|
813
|
+
return matchesKey(data, Key.escape) || this.keybindings.matches(data, "tui.select.cancel");
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
private isClipboardPasteKey(data: string): boolean {
|
|
817
|
+
return this.keybindings.matches(data, "app.clipboard.pasteImage");
|
|
660
818
|
}
|
|
661
819
|
|
|
662
820
|
private isUpKey(data: string): boolean {
|
|
@@ -684,8 +842,12 @@ export class QuestionPanel implements Component, Focusable {
|
|
|
684
842
|
}
|
|
685
843
|
|
|
686
844
|
function insideRect(rect: PanelRect, event: TuiMouseEvent): boolean {
|
|
687
|
-
return
|
|
688
|
-
|
|
845
|
+
return (
|
|
846
|
+
event.x >= rect.x &&
|
|
847
|
+
event.x < rect.x + rect.width &&
|
|
848
|
+
event.y >= rect.y &&
|
|
849
|
+
event.y < rect.y + rect.height
|
|
850
|
+
);
|
|
689
851
|
}
|
|
690
852
|
|
|
691
853
|
function insideSpan(span: PanelSpan, event: TuiMouseEvent): boolean {
|
|
@@ -76,7 +76,11 @@ function isBlank(line: string): boolean {
|
|
|
76
76
|
* render width; measuring that padding would stretch the box to its whole column. A title
|
|
77
77
|
* needs its own room inside the top border: one dash, the title, a space and one more dash.
|
|
78
78
|
*/
|
|
79
|
-
export function computeBoxWidth(
|
|
79
|
+
export function computeBoxWidth(
|
|
80
|
+
lines: readonly string[],
|
|
81
|
+
maxInnerWidth: number,
|
|
82
|
+
title?: string,
|
|
83
|
+
): number {
|
|
80
84
|
const available = Math.max(1, maxInnerWidth);
|
|
81
85
|
let widest = Math.min(MIN_BOX_INNER_WIDTH, available);
|
|
82
86
|
|
|
@@ -92,7 +96,11 @@ export function computeBoxWidth(lines: readonly string[], maxInnerWidth: number,
|
|
|
92
96
|
widest = Math.max(widest, visibleWidth(title) + 4);
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
return
|
|
99
|
+
return (
|
|
100
|
+
Math.min(widest, available) +
|
|
101
|
+
BORDER_HORIZONTAL_OVERHEAD +
|
|
102
|
+
2 * BORDER_INNER_PADDING_HORIZONTAL
|
|
103
|
+
);
|
|
96
104
|
}
|
|
97
105
|
|
|
98
106
|
/**
|
|
@@ -122,11 +130,13 @@ export function renderBorderedBox(
|
|
|
122
130
|
const hidden = options.hidden ?? 0;
|
|
123
131
|
const hiddenAbove = options.hiddenAbove ?? 0;
|
|
124
132
|
|
|
125
|
-
boxed.push(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
boxed.push(
|
|
134
|
+
options.colorFn(
|
|
135
|
+
hidden > 0 || hiddenAbove > 0
|
|
136
|
+
? hiddenNotice(hiddenAbove, hidden, dashSpan)
|
|
137
|
+
: `└${"─".repeat(dashSpan)}┘`,
|
|
138
|
+
),
|
|
139
|
+
);
|
|
130
140
|
|
|
131
141
|
return boxed;
|
|
132
142
|
}
|
|
@@ -153,14 +163,11 @@ function topBorder(dashSpan: number, title: string | undefined): string {
|
|
|
153
163
|
|
|
154
164
|
/** Bottom border with the hidden-line notice, kept exactly `dashSpan` columns wide. */
|
|
155
165
|
function hiddenNotice(above: number, below: number, dashSpan: number): string {
|
|
156
|
-
const text =
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
dashSpan,
|
|
162
|
-
"…",
|
|
163
|
-
);
|
|
166
|
+
const text =
|
|
167
|
+
above > 0
|
|
168
|
+
? `${[`↑ ${above}`, below > 0 ? `↓ ${below}` : undefined].filter(Boolean).join(" · ")} hidden`
|
|
169
|
+
: `${below} line${below === 1 ? "" : "s"} hidden`;
|
|
170
|
+
const notice = truncateToWidth(` ✂ ── ${text} ── `, dashSpan, "…");
|
|
164
171
|
const fill = Math.max(0, dashSpan - visibleWidth(notice));
|
|
165
172
|
const left = Math.floor(fill / 2);
|
|
166
173
|
|
|
@@ -83,7 +83,9 @@ export function leftColumnWidth(questions: readonly AskQuestion[], contentWidth:
|
|
|
83
83
|
* a fixed threshold would either stack usable widths or squeeze the box into unusable ones.
|
|
84
84
|
*/
|
|
85
85
|
export function decidePreviewLayout(contentWidth: number, leftWidth: number): PreviewLayoutMode {
|
|
86
|
-
return contentWidth >= leftWidth + PREVIEW_COLUMN_GAP + MIN_PREVIEW_WIDTH
|
|
86
|
+
return contentWidth >= leftWidth + PREVIEW_COLUMN_GAP + MIN_PREVIEW_WIDTH
|
|
87
|
+
? "side-by-side"
|
|
88
|
+
: "stacked";
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
export function previewColumnWidths(
|
|
@@ -108,9 +110,10 @@ export function previewColumnWidths(
|
|
|
108
110
|
* taller than that would be clipped by the panel window and lose its bottom border.
|
|
109
111
|
*/
|
|
110
112
|
export function previewBlockRowBudget(mode: PreviewLayoutMode, availableRows: number): number {
|
|
111
|
-
const cap =
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
const cap =
|
|
114
|
+
mode === "side-by-side"
|
|
115
|
+
? PREVIEW_MAX_BLOCK_ROWS_SIDE_BY_SIDE
|
|
116
|
+
: PREVIEW_MAX_BLOCK_ROWS_STACKED;
|
|
114
117
|
|
|
115
118
|
return Math.max(MIN_BLOCK_ROWS, Math.min(cap, availableRows));
|
|
116
119
|
}
|
|
@@ -68,7 +68,12 @@ export class QuestionPreviewRenderer {
|
|
|
68
68
|
return this.hasPreview;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
render(
|
|
71
|
+
render(
|
|
72
|
+
optionIndex: number,
|
|
73
|
+
width: number,
|
|
74
|
+
maxBlockRows: number,
|
|
75
|
+
scrollOffset = 0,
|
|
76
|
+
): PreviewBlock {
|
|
72
77
|
const maxInnerWidth = Math.max(
|
|
73
78
|
1,
|
|
74
79
|
width - BORDER_HORIZONTAL_OVERHEAD - 2 * BORDER_INNER_PADDING_HORIZONTAL,
|