@juicesharp/rpiv-ask-user-question 0.1.2 → 0.1.4
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/ask-user-question.ts +197 -71
- package/docs/prompt.jpg +0 -0
- package/package.json +1 -1
- package/wrapping-select.ts +185 -0
package/ask-user-question.ts
CHANGED
|
@@ -1,12 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Registration function: call registerAskUserQuestionTool(pi) from index.ts.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { type ExtensionAPI, DynamicBorder } from "@mariozechner/pi-coding-agent";
|
|
8
|
-
import { Container, SelectList, Spacer, Text, type SelectItem } from "@mariozechner/pi-tui";
|
|
1
|
+
import { type ExtensionAPI, DynamicBorder, type Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Container, Spacer, Text, getKeybindings } from "@mariozechner/pi-tui";
|
|
9
3
|
import { Type } from "@sinclair/typebox";
|
|
4
|
+
import {
|
|
5
|
+
WrappingSelect,
|
|
6
|
+
type WrappingSelectItem,
|
|
7
|
+
type WrappingSelectTheme,
|
|
8
|
+
} from "./wrapping-select.js";
|
|
9
|
+
|
|
10
|
+
const MAX_VISIBLE_ROWS = 10;
|
|
11
|
+
|
|
12
|
+
const TYPE_SOMETHING_LABEL = "Type something.";
|
|
13
|
+
const CHAT_ABOUT_THIS_LABEL = "Chat about this";
|
|
14
|
+
const NAV_HINT = "Enter to select · ↑/↓ to navigate · Esc to cancel";
|
|
15
|
+
|
|
16
|
+
const DECLINE_MESSAGE = "User declined to answer questions";
|
|
17
|
+
const CHAT_CONTINUATION_MESSAGE = "User wants to chat about this. Continue the conversation to help them decide.";
|
|
18
|
+
const CHAT_ANSWER_TAG = "User wants to chat about this";
|
|
19
|
+
const NO_INPUT_PLACEHOLDER = "(no input)";
|
|
20
|
+
const ERROR_NO_UI = "Error: UI not available (running in non-interactive mode)";
|
|
21
|
+
const ERROR_NO_OPTIONS = "Error: No options provided";
|
|
22
|
+
|
|
23
|
+
const KEYBIND_UP = "tui.select.up";
|
|
24
|
+
const KEYBIND_DOWN = "tui.select.down";
|
|
25
|
+
const KEYBIND_CONFIRM = "tui.select.confirm";
|
|
26
|
+
const KEYBIND_CANCEL = "tui.select.cancel";
|
|
27
|
+
|
|
28
|
+
const BACKSPACE_CHARS = new Set(["\x7f", "\b"]);
|
|
29
|
+
const ESC_SEQUENCE_PREFIX = "\x1b";
|
|
30
|
+
|
|
31
|
+
interface QuestionOption {
|
|
32
|
+
label: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface QuestionParams {
|
|
37
|
+
question: string;
|
|
38
|
+
header?: string;
|
|
39
|
+
options: QuestionOption[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ToolDetails {
|
|
43
|
+
question: string;
|
|
44
|
+
answer: string | null;
|
|
45
|
+
wasCustom?: boolean;
|
|
46
|
+
wasChat?: boolean;
|
|
47
|
+
}
|
|
10
48
|
|
|
11
49
|
export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
|
|
12
50
|
const OptionSchema = Type.Object({
|
|
@@ -35,82 +73,170 @@ export function registerAskUserQuestionTool(pi: ExtensionAPI): void {
|
|
|
35
73
|
}),
|
|
36
74
|
|
|
37
75
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
38
|
-
if (!ctx.hasUI) {
|
|
39
|
-
|
|
40
|
-
content: [{ type: "text", text: "Error: UI not available (running in non-interactive mode)" }],
|
|
41
|
-
details: { question: params.question, answer: null },
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (params.options.length === 0) {
|
|
46
|
-
return {
|
|
47
|
-
content: [{ type: "text", text: "Error: No options provided" }],
|
|
48
|
-
details: { question: params.question, answer: null },
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const headerPrefix = params.header ? `[${params.header}] ` : "";
|
|
53
|
-
const items = params.options.map(
|
|
54
|
-
(o) => `${o.label}${o.description ? ` — ${o.description}` : ""}`,
|
|
55
|
-
);
|
|
76
|
+
if (!ctx.hasUI) return buildToolResult(ERROR_NO_UI, { question: params.question, answer: null });
|
|
77
|
+
if (params.options.length === 0) return buildToolResult(ERROR_NO_OPTIONS, { question: params.question, answer: null });
|
|
56
78
|
|
|
57
|
-
|
|
58
|
-
const
|
|
79
|
+
const mainItems = buildMainItems(params.options);
|
|
80
|
+
const chatItems: WrappingSelectItem[] = [{ label: CHAT_ABOUT_THIS_LABEL, isChat: true }];
|
|
81
|
+
const totalCount = mainItems.length + chatItems.length;
|
|
59
82
|
|
|
60
|
-
const choice = await ctx.ui.custom<
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s)));
|
|
64
|
-
container.addChild(new Spacer(1));
|
|
65
|
-
container.addChild(new Text(theme.fg("accent", theme.bold(`${headerPrefix}${params.question}`)), 1, 0));
|
|
66
|
-
container.addChild(new Spacer(1));
|
|
67
|
-
|
|
68
|
-
const selectItems: SelectItem[] = allItems.map((item) => ({ value: item, label: item }));
|
|
69
|
-
const selectList = new SelectList(selectItems, Math.min(allItems.length, 10), {
|
|
70
|
-
selectedPrefix: (t) => theme.bg("selectedBg", theme.fg("accent", t)),
|
|
71
|
-
selectedText: (t) => theme.bg("selectedBg", theme.bold(t)),
|
|
83
|
+
const choice = await ctx.ui.custom<WrappingSelectItem | null>((tui, theme, _kb, done) => {
|
|
84
|
+
const selectTheme: WrappingSelectTheme = {
|
|
85
|
+
selectedText: (t) => theme.fg("accent", theme.bold(t)),
|
|
72
86
|
description: (t) => theme.fg("muted", t),
|
|
73
87
|
scrollInfo: (t) => theme.fg("dim", t),
|
|
74
|
-
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const mainList = new WrappingSelect(
|
|
91
|
+
mainItems,
|
|
92
|
+
Math.min(mainItems.length, MAX_VISIBLE_ROWS),
|
|
93
|
+
selectTheme,
|
|
94
|
+
{ totalItemsForNumbering: totalCount },
|
|
95
|
+
);
|
|
96
|
+
const chatList = new WrappingSelect(chatItems, 1, selectTheme, {
|
|
97
|
+
numberStartOffset: mainItems.length,
|
|
98
|
+
totalItemsForNumbering: totalCount,
|
|
75
99
|
});
|
|
76
|
-
selectList.onSelect = (item) => done(item.value);
|
|
77
|
-
selectList.onCancel = () => done(null);
|
|
78
|
-
container.addChild(selectList);
|
|
79
100
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
101
|
+
let selectionIndex = 0;
|
|
102
|
+
const applySelection = () => {
|
|
103
|
+
const isInMainList = selectionIndex < mainItems.length;
|
|
104
|
+
mainList.setFocused(isInMainList);
|
|
105
|
+
chatList.setFocused(!isInMainList);
|
|
106
|
+
if (isInMainList) {
|
|
107
|
+
mainList.setSelectedIndex(selectionIndex);
|
|
108
|
+
} else {
|
|
109
|
+
chatList.setSelectedIndex(selectionIndex - mainItems.length);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
applySelection();
|
|
113
|
+
|
|
114
|
+
const container = buildDialogContainer(theme, params, mainList, chatList);
|
|
84
115
|
|
|
85
116
|
return {
|
|
86
117
|
render: (w) => container.render(w),
|
|
87
118
|
invalidate: () => container.invalidate(),
|
|
88
|
-
handleInput: (data) => {
|
|
89
|
-
|
|
90
|
-
|
|
119
|
+
handleInput: (data) => {
|
|
120
|
+
const currentItem = itemAt(selectionIndex, mainItems, chatItems);
|
|
121
|
+
const isInlineInputActive = !!currentItem?.isOther;
|
|
122
|
+
const kb = getKeybindings();
|
|
91
123
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
if (kb.matches(data, KEYBIND_UP)) {
|
|
125
|
+
if (isInlineInputActive) mainList.clearInputBuffer();
|
|
126
|
+
selectionIndex = wrapIndex(selectionIndex - 1, totalCount);
|
|
127
|
+
applySelection();
|
|
128
|
+
tui.requestRender();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (kb.matches(data, KEYBIND_DOWN)) {
|
|
132
|
+
if (isInlineInputActive) mainList.clearInputBuffer();
|
|
133
|
+
selectionIndex = wrapIndex(selectionIndex + 1, totalCount);
|
|
134
|
+
applySelection();
|
|
135
|
+
tui.requestRender();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (kb.matches(data, KEYBIND_CONFIRM)) {
|
|
139
|
+
if (isInlineInputActive) {
|
|
140
|
+
done({ label: mainList.getInputBuffer(), isOther: true });
|
|
141
|
+
} else if (currentItem) {
|
|
142
|
+
done(currentItem);
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (kb.matches(data, KEYBIND_CANCEL)) {
|
|
147
|
+
done(null);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (isInlineInputActive) {
|
|
151
|
+
if (BACKSPACE_CHARS.has(data)) {
|
|
152
|
+
mainList.backspaceInput();
|
|
153
|
+
} else if (data && !data.startsWith(ESC_SEQUENCE_PREFIX)) {
|
|
154
|
+
mainList.appendInput(data);
|
|
155
|
+
}
|
|
156
|
+
tui.requestRender();
|
|
157
|
+
}
|
|
158
|
+
},
|
|
96
159
|
};
|
|
97
|
-
}
|
|
160
|
+
});
|
|
98
161
|
|
|
99
|
-
|
|
100
|
-
if (choice === "Other (type your own answer)") {
|
|
101
|
-
const customAnswer = await ctx.ui.input(`${params.question}`, "Type your answer...");
|
|
102
|
-
return {
|
|
103
|
-
content: [{ type: "text", text: `User answered: ${customAnswer ?? "(no input)"}` }],
|
|
104
|
-
details: { question: params.question, answer: customAnswer ?? null, wasCustom: true },
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Extract just the label (before the " — " description separator)
|
|
109
|
-
const selectedLabel = choice.split(" — ")[0];
|
|
110
|
-
return {
|
|
111
|
-
content: [{ type: "text", text: `User selected: ${selectedLabel}` }],
|
|
112
|
-
details: { question: params.question, answer: selectedLabel, wasCustom: false },
|
|
113
|
-
};
|
|
162
|
+
return buildResponse(choice, params);
|
|
114
163
|
},
|
|
115
164
|
});
|
|
116
165
|
}
|
|
166
|
+
|
|
167
|
+
function buildMainItems(options: QuestionOption[]): WrappingSelectItem[] {
|
|
168
|
+
return [
|
|
169
|
+
...options.map((o) => ({ label: o.label, description: o.description })),
|
|
170
|
+
{ label: TYPE_SOMETHING_LABEL, isOther: true },
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function itemAt(
|
|
175
|
+
index: number,
|
|
176
|
+
mainItems: WrappingSelectItem[],
|
|
177
|
+
chatItems: WrappingSelectItem[],
|
|
178
|
+
): WrappingSelectItem | undefined {
|
|
179
|
+
return index < mainItems.length ? mainItems[index] : chatItems[index - mainItems.length];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function wrapIndex(index: number, total: number): number {
|
|
183
|
+
return ((index % total) + total) % total;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function buildDialogContainer(
|
|
187
|
+
theme: Theme,
|
|
188
|
+
params: QuestionParams,
|
|
189
|
+
mainList: WrappingSelect,
|
|
190
|
+
chatList: WrappingSelect,
|
|
191
|
+
): Container {
|
|
192
|
+
const container = new Container();
|
|
193
|
+
const border = () => new DynamicBorder((s: string) => theme.fg("accent", s));
|
|
194
|
+
|
|
195
|
+
container.addChild(border());
|
|
196
|
+
container.addChild(new Spacer(1));
|
|
197
|
+
if (params.header) {
|
|
198
|
+
container.addChild(new Text(theme.bg("selectedBg", ` ${params.header} `), 1, 0));
|
|
199
|
+
container.addChild(new Spacer(1));
|
|
200
|
+
}
|
|
201
|
+
container.addChild(new Text(theme.bold(params.question), 1, 0));
|
|
202
|
+
container.addChild(new Spacer(1));
|
|
203
|
+
container.addChild(mainList);
|
|
204
|
+
container.addChild(new Spacer(1));
|
|
205
|
+
container.addChild(border());
|
|
206
|
+
container.addChild(chatList);
|
|
207
|
+
container.addChild(new Spacer(1));
|
|
208
|
+
container.addChild(new Text(theme.fg("dim", NAV_HINT), 1, 0));
|
|
209
|
+
return container;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function buildResponse(choice: WrappingSelectItem | null, params: QuestionParams) {
|
|
213
|
+
if (!choice) {
|
|
214
|
+
return buildToolResult(DECLINE_MESSAGE, { question: params.question, answer: null });
|
|
215
|
+
}
|
|
216
|
+
if (choice.isOther) {
|
|
217
|
+
const customAnswer = choice.label.length > 0 ? choice.label : null;
|
|
218
|
+
return buildToolResult(
|
|
219
|
+
`User answered: ${customAnswer ?? NO_INPUT_PLACEHOLDER}`,
|
|
220
|
+
{ question: params.question, answer: customAnswer, wasCustom: true },
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
if (choice.isChat) {
|
|
224
|
+
return buildToolResult(CHAT_CONTINUATION_MESSAGE, {
|
|
225
|
+
question: params.question,
|
|
226
|
+
answer: CHAT_ANSWER_TAG,
|
|
227
|
+
wasChat: true,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return buildToolResult(`User selected: ${choice.label}`, {
|
|
231
|
+
question: params.question,
|
|
232
|
+
answer: choice.label,
|
|
233
|
+
wasCustom: false,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function buildToolResult(text: string, details: ToolDetails) {
|
|
238
|
+
return {
|
|
239
|
+
content: [{ type: "text" as const, text }],
|
|
240
|
+
details,
|
|
241
|
+
};
|
|
242
|
+
}
|
package/docs/prompt.jpg
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-ask-user-question",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Pi extension: lets the model ask you a clarifying question with structured options instead of guessing",
|
|
5
5
|
"keywords": ["pi-package", "pi-extension", "rpiv"],
|
|
6
6
|
"type": "module",
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { visibleWidth, wrapTextWithAnsi } from "@mariozechner/pi-tui";
|
|
2
|
+
import type { Component } from "@mariozechner/pi-tui";
|
|
3
|
+
|
|
4
|
+
export interface WrappingSelectItem {
|
|
5
|
+
label: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
/** Sentinel: the inline free-text input row. */
|
|
8
|
+
isOther?: boolean;
|
|
9
|
+
/** Sentinel: the "chat about this question" row. */
|
|
10
|
+
isChat?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface WrappingSelectTheme {
|
|
14
|
+
selectedText: (text: string) => string;
|
|
15
|
+
description: (text: string) => string;
|
|
16
|
+
scrollInfo: (text: string) => string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface WrappingSelectOptions {
|
|
20
|
+
/** Start numbering at this offset + 1 (default 0 → rows labeled 1, 2, 3 …). */
|
|
21
|
+
numberStartOffset?: number;
|
|
22
|
+
/** Override the total used to pad the number column (useful when items span multiple lists). */
|
|
23
|
+
totalItemsForNumbering?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class WrappingSelect implements Component {
|
|
27
|
+
private static readonly ACTIVE_POINTER = "❯ ";
|
|
28
|
+
private static readonly INACTIVE_POINTER = " ";
|
|
29
|
+
private static readonly NUMBER_SEPARATOR = ". ";
|
|
30
|
+
private static readonly INPUT_CURSOR = "▌";
|
|
31
|
+
private static readonly MIN_CONTENT_WIDTH = 1;
|
|
32
|
+
|
|
33
|
+
private readonly items: readonly WrappingSelectItem[];
|
|
34
|
+
private readonly maxVisible: number;
|
|
35
|
+
private readonly theme: WrappingSelectTheme;
|
|
36
|
+
private readonly numberStartOffset: number;
|
|
37
|
+
private readonly totalItemsForNumbering: number;
|
|
38
|
+
|
|
39
|
+
private selectedIndex = 0;
|
|
40
|
+
private focused = true;
|
|
41
|
+
private inputBuffer = "";
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
items: readonly WrappingSelectItem[],
|
|
45
|
+
maxVisible: number,
|
|
46
|
+
theme: WrappingSelectTheme,
|
|
47
|
+
options: WrappingSelectOptions = {},
|
|
48
|
+
) {
|
|
49
|
+
this.items = items;
|
|
50
|
+
this.maxVisible = Math.max(1, maxVisible);
|
|
51
|
+
this.theme = theme;
|
|
52
|
+
this.numberStartOffset = options.numberStartOffset ?? 0;
|
|
53
|
+
this.totalItemsForNumbering = options.totalItemsForNumbering ?? items.length;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setSelectedIndex(index: number): void {
|
|
57
|
+
this.selectedIndex = Math.max(0, Math.min(index, this.items.length - 1));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setFocused(focused: boolean): void {
|
|
61
|
+
this.focused = focused;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getInputBuffer(): string {
|
|
65
|
+
return this.inputBuffer;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
appendInput(text: string): void {
|
|
69
|
+
const printable = this.stripControlChars(text);
|
|
70
|
+
if (printable) this.inputBuffer += printable;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
backspaceInput(): void {
|
|
74
|
+
if (this.inputBuffer.length === 0) return;
|
|
75
|
+
this.inputBuffer = Array.from(this.inputBuffer).slice(0, -1).join("");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
clearInputBuffer(): void {
|
|
79
|
+
this.inputBuffer = "";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Intentionally empty — input is routed at the container level. */
|
|
83
|
+
handleInput(_data: string): void {}
|
|
84
|
+
|
|
85
|
+
invalidate(): void {}
|
|
86
|
+
|
|
87
|
+
render(width: number): string[] {
|
|
88
|
+
if (this.items.length === 0) return [];
|
|
89
|
+
|
|
90
|
+
const { startIndex, endIndex } = this.computeVisibleWindow();
|
|
91
|
+
const numberWidth = String(this.totalItemsForNumbering).length;
|
|
92
|
+
const lines: string[] = [];
|
|
93
|
+
|
|
94
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
95
|
+
const item = this.items[i];
|
|
96
|
+
if (!item) continue;
|
|
97
|
+
const isActive = i === this.selectedIndex && this.focused;
|
|
98
|
+
lines.push(...this.renderItem(item, i, isActive, width, numberWidth));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (this.hasItemsOutsideWindow(startIndex, endIndex)) {
|
|
102
|
+
lines.push(this.theme.scrollInfo(` (${this.selectedIndex + 1}/${this.items.length})`));
|
|
103
|
+
}
|
|
104
|
+
return lines;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private computeVisibleWindow(): { startIndex: number; endIndex: number } {
|
|
108
|
+
const half = Math.floor(this.maxVisible / 2);
|
|
109
|
+
const startIndex = Math.max(
|
|
110
|
+
0,
|
|
111
|
+
Math.min(this.selectedIndex - half, this.items.length - this.maxVisible),
|
|
112
|
+
);
|
|
113
|
+
const endIndex = Math.min(startIndex + this.maxVisible, this.items.length);
|
|
114
|
+
return { startIndex, endIndex };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private hasItemsOutsideWindow(startIndex: number, endIndex: number): boolean {
|
|
118
|
+
return startIndex > 0 || endIndex < this.items.length;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private renderItem(
|
|
122
|
+
item: WrappingSelectItem,
|
|
123
|
+
index: number,
|
|
124
|
+
isActive: boolean,
|
|
125
|
+
width: number,
|
|
126
|
+
numberWidth: number,
|
|
127
|
+
): string[] {
|
|
128
|
+
const rowPrefix = this.buildRowPrefix(index, isActive, numberWidth);
|
|
129
|
+
const continuationPrefix = " ".repeat(visibleWidth(rowPrefix));
|
|
130
|
+
const contentWidth = Math.max(WrappingSelect.MIN_CONTENT_WIDTH, width - visibleWidth(rowPrefix));
|
|
131
|
+
|
|
132
|
+
if (this.shouldRenderAsInlineInput(item, isActive)) {
|
|
133
|
+
return [this.renderInlineInputRow(rowPrefix)];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return [
|
|
137
|
+
...this.renderLabelBlock(item.label, rowPrefix, continuationPrefix, contentWidth, isActive),
|
|
138
|
+
...this.renderDescriptionBlock(item.description, continuationPrefix, contentWidth),
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private buildRowPrefix(index: number, isActive: boolean, numberWidth: number): string {
|
|
143
|
+
const pointer = isActive ? WrappingSelect.ACTIVE_POINTER : WrappingSelect.INACTIVE_POINTER;
|
|
144
|
+
const displayNumber = this.numberStartOffset + index + 1;
|
|
145
|
+
const paddedNumber = String(displayNumber).padStart(numberWidth, " ");
|
|
146
|
+
return `${pointer}${paddedNumber}${WrappingSelect.NUMBER_SEPARATOR}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private shouldRenderAsInlineInput(item: WrappingSelectItem, isActive: boolean): boolean {
|
|
150
|
+
return !!item.isOther && isActive;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private renderInlineInputRow(rowPrefix: string): string {
|
|
154
|
+
return this.theme.selectedText(`${rowPrefix}${this.inputBuffer}${WrappingSelect.INPUT_CURSOR}`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private renderLabelBlock(
|
|
158
|
+
label: string,
|
|
159
|
+
rowPrefix: string,
|
|
160
|
+
continuationPrefix: string,
|
|
161
|
+
contentWidth: number,
|
|
162
|
+
isActive: boolean,
|
|
163
|
+
): string[] {
|
|
164
|
+
const wrapped = wrapTextWithAnsi(label, contentWidth);
|
|
165
|
+
return wrapped.map((segment, index) => {
|
|
166
|
+
const prefix = index === 0 ? rowPrefix : continuationPrefix;
|
|
167
|
+
const line = `${prefix}${segment}`;
|
|
168
|
+
return isActive ? this.theme.selectedText(line) : line;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private renderDescriptionBlock(
|
|
173
|
+
description: string | undefined,
|
|
174
|
+
continuationPrefix: string,
|
|
175
|
+
contentWidth: number,
|
|
176
|
+
): string[] {
|
|
177
|
+
if (!description) return [];
|
|
178
|
+
const wrapped = wrapTextWithAnsi(description, contentWidth);
|
|
179
|
+
return wrapped.map((segment) => `${continuationPrefix}${this.theme.description(segment)}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private stripControlChars(text: string): string {
|
|
183
|
+
return Array.from(text).filter((c) => c >= " ").join("");
|
|
184
|
+
}
|
|
185
|
+
}
|