@mrclrchtr/supi-ask-user 1.12.1 → 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.
@@ -1,327 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import type { SelectItem } from "@earendil-works/pi-tui";
3
- import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
4
- import type { AskUserController } from "../session/controller.ts";
5
- import type { NormalizedChoiceQuestion } from "../types.ts";
6
-
7
- export type OverlayAction = "other" | "skip" | "discuss" | "partial";
8
- export type FocusTarget = "choices" | "editor" | "actions";
9
- export type OverlayMode = "choice" | "text" | "custom-input" | "discuss-input" | "note-input";
10
-
11
- export type ChoiceRow =
12
- | { kind: "option"; optionIndex: number }
13
- | { kind: "action"; action: OverlayAction };
14
-
15
- export function buildChoiceRows(
16
- controller: AskUserController,
17
- question: NormalizedChoiceQuestion,
18
- ): ChoiceRow[] {
19
- const rows: ChoiceRow[] = question.options.map((_option, optionIndex) => ({
20
- kind: "option",
21
- optionIndex,
22
- }));
23
- if (question.allowOther) rows.push({ kind: "action", action: "other" });
24
- rows.push(
25
- ...buildExceptionalActions(controller, question.required).map((action) => ({
26
- kind: "action" as const,
27
- action,
28
- })),
29
- );
30
- return rows;
31
- }
32
-
33
- export function buildChoiceItems(
34
- controller: AskUserController,
35
- question: NormalizedChoiceQuestion,
36
- rows: ChoiceRow[],
37
- ): SelectItem[] {
38
- const selectedIndexes = new Set(controller.getSelectedIndexes(question));
39
- return rows.flatMap((row) => {
40
- if (row.kind === "option") {
41
- const option = question.options[row.optionIndex];
42
- return option
43
- ? [
44
- buildOptionItem({
45
- question,
46
- optionIndex: row.optionIndex,
47
- label: option.label,
48
- selectedIndexes,
49
- hasNote: !!controller.getChoiceOptionNote(question.id, option.value),
50
- }),
51
- ]
52
- : [];
53
- }
54
- return [buildActionItem(controller, question.id, row.action)];
55
- });
56
- }
57
-
58
- export function buildTextActionItems(
59
- controller: AskUserController,
60
- ): Array<{ action: OverlayAction; item: SelectItem }> {
61
- return buildExceptionalActions(controller, controller.currentQuestion.required).map((action) => ({
62
- action,
63
- item: {
64
- value: action,
65
- label: actionLabel(action),
66
- },
67
- }));
68
- }
69
-
70
- export function defaultChoiceRowIndex(
71
- controller: AskUserController,
72
- question: NormalizedChoiceQuestion,
73
- rows: ChoiceRow[],
74
- ): number {
75
- const current = controller.getAnswer(question.id);
76
- if (current?.kind === "custom") {
77
- return rows.findIndex((row) => row.kind === "action" && row.action === "other");
78
- }
79
- if (current?.kind === "choice") {
80
- const first = current.selections[0];
81
- if (!first) return 0;
82
- const optionIndex = question.options.findIndex((option) => option.value === first.value);
83
- return optionIndex >= 0 ? optionIndex : 0;
84
- }
85
- return question.initialIndexes[0] ?? question.recommendedIndexes[0] ?? 0;
86
- }
87
-
88
- export function previewOptionIndexForRows(
89
- rows: ChoiceRow[],
90
- rowIndex: number,
91
- fallbackOptionIndex: number,
92
- ): number | undefined {
93
- const row = rows[rowIndex];
94
- if (row?.kind === "option") return row.optionIndex;
95
- return fallbackOptionIndex >= 0 ? fallbackOptionIndex : undefined;
96
- }
97
-
98
- export function footerText(args: {
99
- controller: AskUserController;
100
- mode: OverlayMode;
101
- focus: FocusTarget;
102
- hasTextActions: boolean;
103
- }): string {
104
- const { controller, mode, focus, hasTextActions } = args;
105
- const question = controller.currentQuestion;
106
-
107
- if (question.type === "text") {
108
- if (mode === "discuss-input") return "Enter submit • Esc cancel";
109
- if (focus === "editor") {
110
- return hasTextActions
111
- ? "Enter submit • ↓ actions • ← back • Esc cancel"
112
- : "Enter submit • ← back • Esc cancel";
113
- }
114
- return "↑↓ move • Enter select • ↑ editor • ← back • Esc cancel";
115
- }
116
-
117
- if (mode === "custom-input" || mode === "discuss-input") {
118
- return "Enter submit • Esc cancel";
119
- }
120
- if (mode === "note-input") {
121
- return "Enter save • Esc close";
122
- }
123
- return question.multi
124
- ? "↑↓ move • Space toggle • Enter submit • n note • ← back • Esc cancel"
125
- : "↑↓ move • Space select • Enter submit • n note • ← back • Esc cancel";
126
- }
127
-
128
- export function splitColumns(args: {
129
- width: number;
130
- theme: Theme;
131
- leftLines: string[];
132
- rightLines: string[];
133
- leftRatio?: number;
134
- }): string[] {
135
- const { width, theme, leftLines, rightLines, leftRatio = 0.55 } = args;
136
- const leftWidth = Math.max(36, Math.floor(width * leftRatio));
137
- const rightWidth = Math.max(24, width - leftWidth - 3);
138
- const total = Math.max(leftLines.length, rightLines.length);
139
- const lines: string[] = [];
140
-
141
- for (let index = 0; index < total; index += 1) {
142
- const left = padRight(leftLines[index] ?? "", leftWidth);
143
- const right = padRight(rightLines[index] ?? "", rightWidth);
144
- lines.push(`${left} ${theme.fg("accent", "│")} ${right}`);
145
- }
146
- return lines;
147
- }
148
-
149
- export function choiceRowValue(row: ChoiceRow): string {
150
- return row.kind === "option" ? `option:${row.optionIndex}` : `action:${row.action}`;
151
- }
152
-
153
- export function noteTargetLabel(
154
- controller: AskUserController,
155
- choiceRows: ChoiceRow[],
156
- choiceRowIndex: number,
157
- ): string | undefined {
158
- const question = controller.currentQuestion;
159
- if (question.type !== "choice") return undefined;
160
- const row = choiceRows[choiceRowIndex];
161
- if (row?.kind !== "option") return undefined;
162
- return question.options[row.optionIndex]?.label;
163
- }
164
-
165
- function renderOptionRow(args: {
166
- option: { label: string; description?: string };
167
- labelText: string;
168
- hasNote: boolean;
169
- isSelected: boolean;
170
- theme: Theme;
171
- width: number;
172
- }): string[] {
173
- const { theme, isSelected, labelText, hasNote, width, option } = args;
174
- const prefix = isSelected ? "\u2192 " : " ";
175
- const baseText = isSelected
176
- ? theme.fg("accent", `${prefix}${labelText}`)
177
- : `${prefix}${labelText}`;
178
- const noteSuffix = hasNote ? ` ${theme.fg("accent", "[note]")}` : "";
179
-
180
- const lines: string[] = wrapTextWithAnsi(`${baseText}${noteSuffix}`, width);
181
-
182
- if (option.description) {
183
- const descWidth = Math.max(10, width - 2);
184
- const wrapped = wrapTextWithAnsi(option.description, descWidth);
185
- for (const descLine of wrapped) {
186
- lines.push(theme.fg("muted", ` ${descLine}`));
187
- }
188
- }
189
-
190
- return lines;
191
- }
192
-
193
- function renderActionRow(args: {
194
- actionLabel: string;
195
- isSelected: boolean;
196
- theme: Theme;
197
- }): string[] {
198
- const { theme, isSelected, actionLabel } = args;
199
- const prefix = isSelected ? "\u2192 " : " ";
200
- return [isSelected ? theme.fg("accent", `${prefix}${actionLabel}`) : `${prefix}${actionLabel}`];
201
- }
202
-
203
- function prepareOptionMarker(
204
- question: NormalizedChoiceQuestion,
205
- optionIndex: number,
206
- selectedIndexes: Set<number>,
207
- ): string {
208
- if (question.multi) {
209
- return selectedIndexes.has(optionIndex) ? "[x]" : "[ ]";
210
- }
211
- return selectedIndexes.has(optionIndex) ? "(*)" : "( )";
212
- }
213
-
214
- function prepareOptionLabel(
215
- option: { label: string },
216
- marker: string,
217
- recommended: boolean,
218
- ): string {
219
- return `${marker} ${option.label}${recommended ? " (recommended)" : ""}`;
220
- }
221
-
222
- export function renderChoiceList(args: {
223
- controller: AskUserController;
224
- question: NormalizedChoiceQuestion;
225
- rows: ChoiceRow[];
226
- selectedIndex: number;
227
- theme: Theme;
228
- width: number;
229
- }): string[] {
230
- const { controller, question, rows, selectedIndex, theme, width } = args;
231
- const lines: string[] = [];
232
- const selectedIndexes = new Set(controller.getSelectedIndexes(question));
233
-
234
- for (let i = 0; i < rows.length; i++) {
235
- const row = rows[i];
236
- const isSelected = i === selectedIndex;
237
-
238
- if (row.kind === "option") {
239
- const option = question.options[row.optionIndex];
240
- if (!option) continue;
241
-
242
- const marker = prepareOptionMarker(question, row.optionIndex, selectedIndexes);
243
- const recommended = question.recommendedIndexes.includes(row.optionIndex);
244
- const hasNote = !!controller.getChoiceOptionNote(question.id, option.value);
245
- const labelText = prepareOptionLabel(option, marker, recommended);
246
-
247
- lines.push(...renderOptionRow({ option, labelText, hasNote, isSelected, theme, width }));
248
- } else {
249
- const answer = controller.getAnswer(question.id);
250
- const actionLabelText =
251
- row.action === "other" && answer?.kind === "custom"
252
- ? `Other \u2014 ${answer.value}`
253
- : actionLabel(row.action);
254
-
255
- lines.push(...renderActionRow({ actionLabel: actionLabelText, isSelected, theme }));
256
- }
257
- }
258
-
259
- return lines;
260
- }
261
-
262
- function buildOptionItem(args: {
263
- question: NormalizedChoiceQuestion;
264
- optionIndex: number;
265
- label: string;
266
- selectedIndexes: Set<number>;
267
- hasNote: boolean;
268
- }): SelectItem {
269
- const { question, optionIndex, label, selectedIndexes, hasNote } = args;
270
- const recommended = question.recommendedIndexes.includes(optionIndex) ? " (recommended)" : "";
271
- const marker = question.multi
272
- ? selectedIndexes.has(optionIndex)
273
- ? "[x]"
274
- : "[ ]"
275
- : selectedIndexes.has(optionIndex)
276
- ? "(*)"
277
- : "( )";
278
- return {
279
- value: choiceRowValue({ kind: "option", optionIndex }),
280
- label: `${marker} ${label}${recommended}${hasNote ? " [note]" : ""}`,
281
- };
282
- }
283
-
284
- function buildActionItem(
285
- controller: AskUserController,
286
- questionId: string,
287
- action: OverlayAction,
288
- ): SelectItem {
289
- const answer = controller.getAnswer(questionId);
290
- return {
291
- value: choiceRowValue({ kind: "action", action }),
292
- label:
293
- action === "other" && answer?.kind === "custom"
294
- ? `Other — ${answer.value}`
295
- : actionLabel(action),
296
- };
297
- }
298
-
299
- function buildExceptionalActions(
300
- controller: AskUserController,
301
- required: boolean,
302
- ): OverlayAction[] {
303
- const actions: OverlayAction[] = [];
304
- if (!required) actions.push("skip");
305
- actions.push("discuss");
306
- if (controller.canPartialSubmit()) actions.push("partial");
307
- return actions;
308
- }
309
-
310
- function actionLabel(action: OverlayAction): string {
311
- switch (action) {
312
- case "other":
313
- return "Other…";
314
- case "skip":
315
- return "Skip question";
316
- case "discuss":
317
- return "Discuss instead…";
318
- case "partial":
319
- return "Submit partial answers";
320
- }
321
- }
322
-
323
- function padRight(text: string, width: number): string {
324
- const visible = visibleWidth(text);
325
- if (visible >= width) return truncateToWidth(text, width);
326
- return `${text}${" ".repeat(width - visible)}`;
327
- }