pi-keeper 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 +63 -0
- package/LICENSE +201 -0
- package/NOTICE +36 -0
- package/README.md +71 -2
- package/extensions/ask-heartbeat.ts +55 -0
- package/extensions/ask-mailbox.ts +497 -0
- package/extensions/ask-server.ts +452 -0
- package/extensions/attention.ts +24 -0
- package/extensions/cc-extensions-compat.ts +103 -0
- package/extensions/child-auto.ts +32 -0
- package/extensions/child-cwd.ts +76 -0
- package/extensions/child-default.ts +32 -0
- package/extensions/child-lane.ts +134 -0
- package/extensions/child-plan.ts +12 -0
- package/extensions/config.ts +347 -0
- package/extensions/dialog-lock.ts +70 -0
- package/extensions/dialog.ts +420 -0
- package/extensions/index.ts +3453 -0
- package/extensions/modes.ts +1330 -0
- package/extensions/options.ts +109 -0
- package/extensions/perf.ts +54 -0
- package/extensions/plan-approval.ts +345 -0
- package/extensions/preview.ts +232 -0
- package/extensions/project-rules.ts +565 -0
- package/extensions/prompt-blocks.ts +163 -0
- package/extensions/remote-relay.ts +163 -0
- package/extensions/rtk-rewrite.ts +60 -0
- package/extensions/rulebook.ts +302 -0
- package/extensions/rules-line.ts +71 -0
- package/extensions/rules-view.ts +168 -0
- package/extensions/rules.ts +412 -0
- package/extensions/secure-file.ts +157 -0
- package/extensions/session-grant-store.ts +139 -0
- package/extensions/session-grants.ts +154 -0
- package/extensions/settings-panel.ts +194 -0
- package/extensions/shell-highlight-bridge.ts +50 -0
- package/extensions/subagents-bridge.ts +129 -0
- package/extensions/telemetry.ts +213 -0
- package/extensions/thinking.ts +62 -0
- package/extensions/title.ts +80 -0
- package/extensions/ui-relay.ts +149 -0
- package/extensions/utils.ts +3981 -0
- package/package.json +64 -5
- package/prompts/auto-mode.md +16 -0
- package/prompts/child-auto.md +26 -0
- package/prompts/child-default.md +21 -0
- package/prompts/child-plan.md +16 -0
- package/prompts/contract.md +29 -0
- package/prompts/default-mode.md +8 -0
- package/prompts/plan-mode.md +45 -0
- package/prompts/rule-warning.md +8 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Permission dialog with a preview (diff or full command) above the
|
|
3
|
+
* options, so the user sees what a call will do before answering. The
|
|
4
|
+
* title is styled per line (title.ts): headers and questions bold in the
|
|
5
|
+
* normal text color, commands in the theme's warning color (yellow, like
|
|
6
|
+
* the auto-mode badge), explanations dim — except a whole command when
|
|
7
|
+
* `commandColors` asks for the shell highlighter (see below). Without a
|
|
8
|
+
* preview the same dialog shows just the title and the options.
|
|
9
|
+
*
|
|
10
|
+
* The full command of a long bash call (preview.kind === "text") is painted in the
|
|
11
|
+
* slot `commandColors` names (config.ts), because a theme's dim is often only just
|
|
12
|
+
* readable for a block of body text. Unset, it is `"shell-highlight"` when
|
|
13
|
+
* `pi-shell-highlight` is loaded and `"dim"` when not; the choice is made each time a
|
|
14
|
+
* dialog opens, so a package that loads later counts. The two syntax values color it
|
|
15
|
+
* instead: `"builtin-syntax"` with pi's own highlighter (the one the transcript uses
|
|
16
|
+
* for fenced code), `"shell-highlight"` with `pi-shell-highlight` (command names and the
|
|
17
|
+
* inner language of a heredoc; without that package it means dim). `"shell-highlight"`
|
|
18
|
+
* reaches two more surfaces through
|
|
19
|
+
* `paintShellLines`: the one-line `bash: <command>` title of a short command (which has
|
|
20
|
+
* no preview block), and a shell fenced block in the plan view (plan-approval.ts).
|
|
21
|
+
* A diff preview keeps its own added/removed colors on purpose: there the colors carry
|
|
22
|
+
* meaning.
|
|
23
|
+
*
|
|
24
|
+
* A mode-switch shortcut (onModeShortcut) is consumed without closing the
|
|
25
|
+
* dialog: the answer still governs the pending call, and the next call
|
|
26
|
+
* follows the new mode.
|
|
27
|
+
*
|
|
28
|
+
* Returns the chosen option label (undefined on Esc or abort), like
|
|
29
|
+
* ctx.ui.select, so callers keep their branch logic.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import type { ExtensionContext, Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
33
|
+
import { highlightCode, renderDiff } from "@earendil-works/pi-coding-agent";
|
|
34
|
+
import { Key, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
35
|
+
import { effectiveCommandColors, type PreviewTextColor } from "./config.ts";
|
|
36
|
+
import { withDialogLock } from "./dialog-lock.ts";
|
|
37
|
+
import { ALLOW_ONCE } from "./options.ts";
|
|
38
|
+
import type { Preview } from "./preview.ts";
|
|
39
|
+
import { commandHighlighter, type CommandStyle } from "./shell-highlight-bridge.ts";
|
|
40
|
+
import type { DialogTitle, TitleLine, TitleSegment } from "./title.ts";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The configured full-command color: what config.ts holds, undefined when unset. Set once per
|
|
44
|
+
* session from the config (index.ts, right after `loadConfig`), so every dialog of this process
|
|
45
|
+
* agrees. It is resolved against the loaded highlighter at each use, not here.
|
|
46
|
+
*/
|
|
47
|
+
let commandColors: PreviewTextColor | undefined;
|
|
48
|
+
|
|
49
|
+
/** Use `color` (undefined: unset) for the full-command text of every later dialog (config `commandColors`). */
|
|
50
|
+
export function setPreviewTextColor(color: PreviewTextColor | undefined): void {
|
|
51
|
+
commandColors = color;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The color in effect now: the configured one, or the default that depends on whether pi-shell-highlight is loaded. */
|
|
55
|
+
function resolvedCommandColors(): PreviewTextColor {
|
|
56
|
+
return effectiveCommandColors(commandColors, commandHighlighter() !== undefined);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** True when the command is painted by the shell highlighter (`shell-highlight` in effect). */
|
|
60
|
+
export function wantsShellHighlight(): boolean {
|
|
61
|
+
return resolvedCommandColors() === "shell-highlight";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* How the command highlighter paints what it decides, in the theme that draws the text.
|
|
66
|
+
* One definition for every surface that shows a shell command (the dialog title, the
|
|
67
|
+
* full-command preview, the plan's fenced blocks), so they cannot drift apart: a command
|
|
68
|
+
* name is `syntaxFunction`, a runner (python, go, npm) `syntaxType`, a precommand
|
|
69
|
+
* (sudo, rtk) `syntaxVariable`, an option `syntaxNumber`.
|
|
70
|
+
*/
|
|
71
|
+
export function shellStyle(theme: Pick<Theme, "fg">): CommandStyle {
|
|
72
|
+
return {
|
|
73
|
+
command: (text) => theme.fg("syntaxFunction", text),
|
|
74
|
+
// a runner names the language or the task, so it reads louder than a plain tool
|
|
75
|
+
runner: (text) => theme.fg("syntaxType", text),
|
|
76
|
+
// a precommand only says *how* the command runs: it recedes
|
|
77
|
+
precommand: (text) => theme.fg("syntaxVariable", text),
|
|
78
|
+
option: (text) => theme.fg("syntaxNumber", text),
|
|
79
|
+
plain: (text) => theme.fg("mdCodeBlock", text),
|
|
80
|
+
variable: (text) => theme.fg("syntaxVariable", text),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Shell text as colored lines: `pi-shell-highlight` when it is loaded (command names and
|
|
86
|
+
* the inner language of a heredoc on top of the grammar), pi's own highlighter when it is
|
|
87
|
+
* not or when it throws. Never throws itself, so a caller can paint a title with it.
|
|
88
|
+
*/
|
|
89
|
+
export function paintShellLines(theme: Pick<Theme, "fg">, text: string, lang: string): string[] {
|
|
90
|
+
const highlighter = commandHighlighter();
|
|
91
|
+
if (highlighter !== undefined) {
|
|
92
|
+
try {
|
|
93
|
+
return highlighter(text, lang, shellStyle(theme));
|
|
94
|
+
} catch {
|
|
95
|
+
// a broken highlighter must not cost the caller its text
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return highlightCode(text, lang);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The full-command text: syntax-colored when configured, else one plain theme slot.
|
|
103
|
+
* pi's highlighter reads the process theme (as renderDiff does), not the `theme`
|
|
104
|
+
* argument, which is the same instance in the TUI; the richer renderer gets the color
|
|
105
|
+
* callbacks and stays a pure function of text and style. Either way a language nothing
|
|
106
|
+
* knows comes back uncolored rather than failing.
|
|
107
|
+
*/
|
|
108
|
+
function paintCommand(theme: Pick<Theme, "fg">, preview: { text: string; lang?: string }): string {
|
|
109
|
+
const lang = preview.lang ?? "bash";
|
|
110
|
+
const color = resolvedCommandColors();
|
|
111
|
+
if (color === "shell-highlight") return paintShellLines(theme, preview.text, lang).join("\n");
|
|
112
|
+
// builtin-syntax keeps pi's highlighter alone
|
|
113
|
+
if (color === "builtin-syntax") return highlightCode(preview.text, lang).join("\n");
|
|
114
|
+
return theme.fg(color, preview.text);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Preview lines kept in the plain-text fallback (RPC select title). */
|
|
118
|
+
const PLAIN_PREVIEW_LINES = 200;
|
|
119
|
+
|
|
120
|
+
/** Rows left free below the dialog (pi's footer/status lines). */
|
|
121
|
+
const RESERVED_ROWS = 4;
|
|
122
|
+
/** Keep enough command/diff content visible to make paging useful. */
|
|
123
|
+
const MIN_VIEWPORT = 10;
|
|
124
|
+
const MAX_VIEWPORT = 15;
|
|
125
|
+
|
|
126
|
+
/** One title segment in its dialog color: commands yellow (the warning slot, like the auto-mode badge), labels muted, headers bold, explanations dim. */
|
|
127
|
+
function styleSegment(theme: Pick<Theme, "fg" | "bold">, segment: TitleSegment): string {
|
|
128
|
+
if (segment.style === "command") return styleCommand(theme, segment);
|
|
129
|
+
if (segment.style === "muted") return theme.fg("muted", segment.text);
|
|
130
|
+
if (segment.style === "bold") return theme.bold(theme.fg("text", segment.text));
|
|
131
|
+
if (segment.style === "dim") return theme.fg("dim", segment.text);
|
|
132
|
+
return theme.fg("text", segment.text);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A command in a title. With a language and the shell highlighter configured, the words
|
|
137
|
+
* are colored as shell text, so the one-line `bash: <command>` form reads like the
|
|
138
|
+
* full-command preview below it; otherwise — another setting, a command pattern, a
|
|
139
|
+
* highlighter that throws — it keeps the flat yellow that draws the eye.
|
|
140
|
+
*/
|
|
141
|
+
function styleCommand(theme: Pick<Theme, "fg" | "bold">, segment: TitleSegment): string {
|
|
142
|
+
if (segment.lang === undefined || segment.text.trim() === "" || wantsShellHighlight() === false) return theme.fg("warning", segment.text);
|
|
143
|
+
const lines = paintShellLines(theme, segment.text, segment.lang);
|
|
144
|
+
return lines.length === 1 && lines[0] !== undefined ? lines[0] : theme.fg("warning", segment.text);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A title line in its dialog colors (shared with the plan-approval selector). */
|
|
148
|
+
export function styleTitleLine(theme: Pick<Theme, "fg" | "bold">, line: TitleLine): string {
|
|
149
|
+
return line.map((segment) => styleSegment(theme, segment)).join("");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function permissionDialog(
|
|
153
|
+
ctx: ExtensionContext,
|
|
154
|
+
title: DialogTitle,
|
|
155
|
+
preview: Preview | undefined,
|
|
156
|
+
options: string[],
|
|
157
|
+
escLabel = "reject",
|
|
158
|
+
signalOverride?: AbortSignal,
|
|
159
|
+
onModeShortcut?: (data: string) => boolean,
|
|
160
|
+
/** Run inside the lock, before the dialog opens: true when the call is now allowed (a grant a queued dialog just made), and the dialog is skipped. */
|
|
161
|
+
recheck?: () => boolean,
|
|
162
|
+
): Promise<string | undefined> {
|
|
163
|
+
// the one choke point of every permission select, the parent's own and a child's relayed ask
|
|
164
|
+
return withDialogLock(async () => {
|
|
165
|
+
if (recheck !== undefined) {
|
|
166
|
+
// Yield one macrotask first: the dialog that held the lock just before may still be on its way
|
|
167
|
+
// to recording its answer (a grant is written after its promise settles, a few microtasks later),
|
|
168
|
+
// and the recheck must see that grant.
|
|
169
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
170
|
+
if (recheck() === true) return ALLOW_ONCE;
|
|
171
|
+
}
|
|
172
|
+
return openPermissionDialog(ctx, title, preview, options, escLabel, signalOverride, onModeShortcut);
|
|
173
|
+
}, signalOverride ?? ctx.signal);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function openPermissionDialog(
|
|
177
|
+
ctx: ExtensionContext,
|
|
178
|
+
title: DialogTitle,
|
|
179
|
+
preview: Preview | undefined,
|
|
180
|
+
options: string[],
|
|
181
|
+
/** What Esc does, shown in the hints line ("reject" in the main dialog, "back" in a sub-list). */
|
|
182
|
+
escLabel = "reject",
|
|
183
|
+
/** Closes the dialog when aborted, overriding ctx.signal (the child relay passes the child's signal). */
|
|
184
|
+
signalOverride?: AbortSignal,
|
|
185
|
+
/**
|
|
186
|
+
* Handles a mode-switch shortcut pressed inside the dialog; returns true when it
|
|
187
|
+
* consumed the key. The dialog stays open: the answer still governs the pending
|
|
188
|
+
* call, and the next call follows the new mode.
|
|
189
|
+
*/
|
|
190
|
+
onModeShortcut?: (data: string) => boolean,
|
|
191
|
+
): Promise<string | undefined> {
|
|
192
|
+
const signal = signalOverride ?? ctx.signal;
|
|
193
|
+
if (ctx.mode !== "tui") {
|
|
194
|
+
return ctx.ui.select(`${title.plain}\n${preview === undefined ? "" : `${plainPreview(preview)}\n`}`, options, { signal });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (signal?.aborted) return undefined;
|
|
198
|
+
let onAbort: (() => void) | undefined;
|
|
199
|
+
try {
|
|
200
|
+
return await ctx.ui.custom<string | undefined>((tui, theme, _keybindings, finish) => {
|
|
201
|
+
// abort can race a keypress: settle once
|
|
202
|
+
let settled = false;
|
|
203
|
+
const done = (result: string | undefined): void => {
|
|
204
|
+
if (settled) return;
|
|
205
|
+
settled = true;
|
|
206
|
+
finish(result);
|
|
207
|
+
};
|
|
208
|
+
onAbort = () => done(undefined);
|
|
209
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
210
|
+
|
|
211
|
+
const hasPreview = preview !== undefined;
|
|
212
|
+
const body =
|
|
213
|
+
preview === undefined
|
|
214
|
+
? ""
|
|
215
|
+
: (preview.kind === "diff" ? renderDiff(preview.diff) : paintCommand(theme, preview)).replace(/\t/g, " ");
|
|
216
|
+
let bodyLines: string[] = body === "" ? [] : body.split("\n");
|
|
217
|
+
const summary = preview !== undefined && preview.kind === "diff" ? preview.summary : "full command";
|
|
218
|
+
|
|
219
|
+
let selected = 0;
|
|
220
|
+
let scroll = 0;
|
|
221
|
+
let viewport = MIN_VIEWPORT;
|
|
222
|
+
let cachedWidth: number | undefined;
|
|
223
|
+
let cachedLines: string[] | undefined;
|
|
224
|
+
|
|
225
|
+
const refresh = (): void => {
|
|
226
|
+
cachedLines = undefined;
|
|
227
|
+
tui.requestRender();
|
|
228
|
+
};
|
|
229
|
+
const scrollBy = (delta: number): void => {
|
|
230
|
+
scroll = Math.max(0, Math.min(scroll + delta, Math.max(0, bodyLines.length - viewport)));
|
|
231
|
+
refresh();
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
function handleInput(data: string): void {
|
|
235
|
+
if (onModeShortcut?.(data)) return;
|
|
236
|
+
if (matchesKey(data, Key.escape)) return done(undefined);
|
|
237
|
+
if (matchesKey(data, Key.enter)) return done(options[selected]);
|
|
238
|
+
if (matchesKey(data, Key.shift("up"))) return scrollBy(-1);
|
|
239
|
+
if (matchesKey(data, Key.shift("down"))) return scrollBy(1);
|
|
240
|
+
if (matchesKey(data, Key.pageUp)) return scrollBy(-Math.max(1, viewport - 2));
|
|
241
|
+
if (matchesKey(data, Key.pageDown)) return scrollBy(Math.max(1, viewport - 2));
|
|
242
|
+
if (matchesKey(data, Key.home)) return scrollBy(-bodyLines.length);
|
|
243
|
+
if (matchesKey(data, Key.end)) return scrollBy(bodyLines.length);
|
|
244
|
+
if (matchesKey(data, Key.up) || data === "k") {
|
|
245
|
+
selected = Math.max(0, selected - 1);
|
|
246
|
+
return refresh();
|
|
247
|
+
}
|
|
248
|
+
if (matchesKey(data, Key.down) || data === "j") {
|
|
249
|
+
selected = Math.min(options.length - 1, selected + 1);
|
|
250
|
+
return refresh();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function render(width: number): string[] {
|
|
255
|
+
if (cachedLines !== undefined && cachedWidth === width) return cachedLines;
|
|
256
|
+
const w = Math.max(1, width);
|
|
257
|
+
const border = theme.fg("accent", "─".repeat(w));
|
|
258
|
+
|
|
259
|
+
// trailing blank lines are dropped, as the string title's trailing newlines were
|
|
260
|
+
// Wrap each logical preview line instead of truncating it horizontally. This
|
|
261
|
+
// is especially important for long, single-line interpreter commands.
|
|
262
|
+
bodyLines = body === "" ? [] : body.split("\n").flatMap((line) => (line === "" ? [""] : wrapTextWithAnsi(` ${line}`, w)));
|
|
263
|
+
const headerTexts = title.lines.map((line) => styleTitleLine(theme, line));
|
|
264
|
+
while (headerTexts.length > 0 && headerTexts[headerTexts.length - 1] === "") headerTexts.pop();
|
|
265
|
+
const header = headerTexts.flatMap((line) => (line === "" ? [""] : wrapTextWithAnsi(` ${line}`, w)));
|
|
266
|
+
const optionLines = options.flatMap((option, i) => {
|
|
267
|
+
const prefix = i === selected ? theme.fg("accent", "> ") : " ";
|
|
268
|
+
const label = theme.fg(i === selected ? "accent" : "text", option);
|
|
269
|
+
return wrapTextWithAnsi(`${prefix}${label}`, w);
|
|
270
|
+
});
|
|
271
|
+
const hints = ` ↑↓ select · enter confirm · esc ${escLabel}`;
|
|
272
|
+
|
|
273
|
+
// border, header, blank, [summary, viewport, indicator, blank], options, blank, hints, border
|
|
274
|
+
const fixed = hasPreview
|
|
275
|
+
? 2 + header.length + 2 + 1 + 1 + optionLines.length + 2
|
|
276
|
+
: 2 + header.length + 1 + optionLines.length + 2;
|
|
277
|
+
const available = tui.terminal.rows - RESERVED_ROWS - fixed;
|
|
278
|
+
const target = Math.min(MAX_VIEWPORT, Math.max(MIN_VIEWPORT, bodyLines.length));
|
|
279
|
+
viewport = hasPreview ? Math.max(1, Math.min(target, available)) : 0;
|
|
280
|
+
scroll = Math.max(0, Math.min(scroll, bodyLines.length - viewport));
|
|
281
|
+
const scrollable = bodyLines.length > viewport;
|
|
282
|
+
|
|
283
|
+
const lines = [border, ...header];
|
|
284
|
+
if (hasPreview) {
|
|
285
|
+
lines.push("", ` ${theme.fg("muted", summary)}`);
|
|
286
|
+
for (const line of bodyLines.slice(scroll, scroll + viewport)) lines.push(line);
|
|
287
|
+
if (scrollable) {
|
|
288
|
+
const last = Math.min(scroll + viewport, bodyLines.length);
|
|
289
|
+
lines.push(theme.fg("dim", truncateToWidth(` lines ${scroll + 1}–${last} of ${bodyLines.length} · pgup/pgdn, shift+↑↓ scroll`, w)));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
lines.push("", ...optionLines, "", theme.fg("dim", truncateToWidth(hints, w)), border);
|
|
293
|
+
|
|
294
|
+
cachedWidth = width;
|
|
295
|
+
cachedLines = lines;
|
|
296
|
+
return lines;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
render,
|
|
301
|
+
handleInput,
|
|
302
|
+
invalidate: () => {
|
|
303
|
+
cachedLines = undefined;
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
});
|
|
307
|
+
} finally {
|
|
308
|
+
if (onAbort !== undefined) signal?.removeEventListener("abort", onAbort);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Uncolored preview for non-TUI dialogs, capped at PLAIN_PREVIEW_LINES. */
|
|
313
|
+
function plainPreview(preview: Preview): string {
|
|
314
|
+
const text = preview.kind === "diff" ? `${preview.summary}\n${preview.diff}` : preview.text;
|
|
315
|
+
const lines = text.split("\n");
|
|
316
|
+
if (lines.length <= PLAIN_PREVIEW_LINES) return text;
|
|
317
|
+
return `${lines.slice(0, PLAIN_PREVIEW_LINES).join("\n")}\n… ${lines.length - PLAIN_PREVIEW_LINES} more lines`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** One /mode selector row: icon and badge (padded into aligned columns, colored like the footer chip) plus a plain-text description. */
|
|
321
|
+
export interface ModeDialogItem {
|
|
322
|
+
icon: string;
|
|
323
|
+
badge: string;
|
|
324
|
+
color: ThemeColor;
|
|
325
|
+
description: string;
|
|
326
|
+
current: boolean;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Icon + badge padded into fixed columns, so the rows stack: `● default`, `⏵⏵ auto`, `⏸ plan`. */
|
|
330
|
+
function padModeLabel(item: ModeDialogItem, iconWidth: number, badgeWidth: number): string {
|
|
331
|
+
const icon = item.icon + " ".repeat(iconWidth - visibleWidth(item.icon));
|
|
332
|
+
const badge = item.badge + " ".repeat(badgeWidth - visibleWidth(item.badge));
|
|
333
|
+
return `${icon} ${badge}`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The /mode selector: rows stacked in aligned columns — bold icon and badge
|
|
338
|
+
* in the mode's footer-chip color, description in the text color. Returns the
|
|
339
|
+
* chosen index (undefined on Esc or abort). Non-TUI modes fall back to
|
|
340
|
+
* ctx.ui.select with plain strings.
|
|
341
|
+
*/
|
|
342
|
+
export async function selectModeDialog(
|
|
343
|
+
ctx: ExtensionContext,
|
|
344
|
+
items: ModeDialogItem[],
|
|
345
|
+
title: string,
|
|
346
|
+
signal?: AbortSignal,
|
|
347
|
+
onModeShortcut?: (data: string) => boolean,
|
|
348
|
+
): Promise<number | undefined> {
|
|
349
|
+
const iconWidth = Math.max(...items.map((item) => visibleWidth(item.icon)));
|
|
350
|
+
const badgeWidth = Math.max(...items.map((item) => visibleWidth(item.badge)));
|
|
351
|
+
const plain = items.map((item) => `${padModeLabel(item, iconWidth, badgeWidth)} — ${item.description}${item.current ? " (current)" : ""}`);
|
|
352
|
+
if (ctx.mode !== "tui") {
|
|
353
|
+
const picked = await ctx.ui.select(title, plain, signal === undefined ? undefined : { signal });
|
|
354
|
+
return picked === undefined ? undefined : plain.indexOf(picked);
|
|
355
|
+
}
|
|
356
|
+
if (signal?.aborted) return undefined;
|
|
357
|
+
let onAbort: (() => void) | undefined;
|
|
358
|
+
try {
|
|
359
|
+
return await ctx.ui.custom<number | undefined>((tui, theme, _keybindings, finish) => {
|
|
360
|
+
let settled = false;
|
|
361
|
+
const done = (result: number | undefined): void => {
|
|
362
|
+
if (settled) return;
|
|
363
|
+
settled = true;
|
|
364
|
+
finish(result);
|
|
365
|
+
};
|
|
366
|
+
onAbort = () => done(undefined);
|
|
367
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
368
|
+
|
|
369
|
+
let selected = Math.max(0, items.findIndex((item) => item.current));
|
|
370
|
+
let cachedWidth: number | undefined;
|
|
371
|
+
let cachedLines: string[] | undefined;
|
|
372
|
+
const refresh = (): void => {
|
|
373
|
+
cachedLines = undefined;
|
|
374
|
+
tui.requestRender();
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
function handleInput(data: string): void {
|
|
378
|
+
if (onModeShortcut?.(data)) return;
|
|
379
|
+
if (matchesKey(data, Key.escape)) return done(undefined);
|
|
380
|
+
if (matchesKey(data, Key.enter)) return done(selected);
|
|
381
|
+
if (matchesKey(data, Key.up) || data === "k") {
|
|
382
|
+
selected = Math.max(0, selected - 1);
|
|
383
|
+
return refresh();
|
|
384
|
+
}
|
|
385
|
+
if (matchesKey(data, Key.down) || data === "j") {
|
|
386
|
+
selected = Math.min(items.length - 1, selected + 1);
|
|
387
|
+
return refresh();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function render(width: number): string[] {
|
|
392
|
+
if (cachedLines !== undefined && cachedWidth === width) return cachedLines;
|
|
393
|
+
const w = Math.max(1, width);
|
|
394
|
+
const border = theme.fg("accent", "─".repeat(w));
|
|
395
|
+
const header = title.split("\n").flatMap((line) => wrapTextWithAnsi(` ${theme.bold(theme.fg("text", line))}`, w));
|
|
396
|
+
const rows = items.flatMap((item, i) => {
|
|
397
|
+
const prefix = i === selected ? theme.fg("accent", "> ") : " ";
|
|
398
|
+
const text =
|
|
399
|
+
`${prefix}${theme.bold(theme.fg(item.color, padModeLabel(item, iconWidth, badgeWidth)))}${theme.fg("text", ` — ${item.description}`)}` +
|
|
400
|
+
(item.current ? theme.fg("dim", " (current)") : "");
|
|
401
|
+
return wrapTextWithAnsi(text, w);
|
|
402
|
+
});
|
|
403
|
+
const hints = " ↑↓ select · enter confirm · esc keep";
|
|
404
|
+
cachedWidth = width;
|
|
405
|
+
cachedLines = [border, ...header, "", ...rows, "", theme.fg("dim", truncateToWidth(hints, w)), border];
|
|
406
|
+
return cachedLines;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return {
|
|
410
|
+
render,
|
|
411
|
+
handleInput,
|
|
412
|
+
invalidate: () => {
|
|
413
|
+
cachedLines = undefined;
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
});
|
|
417
|
+
} finally {
|
|
418
|
+
if (onAbort !== undefined) signal?.removeEventListener("abort", onAbort);
|
|
419
|
+
}
|
|
420
|
+
}
|