@0xkahi/pi-qol 0.1.1 → 1.0.1
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/README.md +13 -0
- package/assets/config.schema.json +12 -0
- package/package.json +4 -4
- package/src/codemap.md +26 -16
- package/src/config-loader.ts +4 -0
- package/src/constants.ts +1 -0
- package/src/extensions/auto-session-name/codemap.md +17 -12
- package/src/extensions/auto-session-name/title-generator.ts +1 -1
- package/src/extensions/codemap.md +173 -13
- package/src/extensions/context-view/capture.ts +394 -0
- package/src/extensions/context-view/codemap.md +213 -0
- package/src/extensions/context-view/constants.ts +4 -0
- package/src/extensions/context-view/context-view-controller.ts +67 -0
- package/src/extensions/context-view/index.ts +120 -0
- package/src/extensions/context-view/measure.ts +307 -0
- package/src/extensions/context-view/model.ts +207 -0
- package/src/extensions/context-view/ui/codemap.md +82 -0
- package/src/extensions/context-view/ui/context-view-dialog.ts +59 -0
- package/src/extensions/context-view/ui/injections-model.ts +118 -0
- package/src/extensions/context-view/ui/injections-view.ts +264 -0
- package/src/extensions/context-view/ui/skill-preview.ts +82 -0
- package/src/extensions/context-view/ui/usage-map.ts +133 -0
- package/src/extensions/context-view/ui/usage-view.ts +613 -0
- package/src/extensions/context-view/usage.ts +432 -0
- package/src/extensions/model-select/codemap.md +34 -17
- package/src/extensions/model-select/model-select-dialog.ts +85 -324
- package/src/index.ts +2 -0
- package/src/libs/codemap.md +8 -1
- package/src/libs/modal/README.md +212 -0
- package/src/libs/modal/codemap.md +34 -0
- package/src/libs/modal/index.ts +29 -0
- package/src/libs/modal/list-navigator.ts +167 -0
- package/src/libs/modal/modal-dialog.ts +297 -0
- package/src/libs/modal/navigation/pi-scheme.ts +33 -0
- package/src/libs/modal/navigation/vim-scheme.ts +56 -0
- package/src/libs/modal/preview-layer.ts +87 -0
- package/src/libs/modal/render-cache.ts +33 -0
- package/src/libs/modal/tab-strip.ts +87 -0
- package/src/libs/modal/tabs/list-tab.ts +159 -0
- package/src/libs/modal/text.ts +88 -0
- package/src/libs/modal/types.ts +70 -0
- package/src/schemas/codemap.md +100 -18
- package/src/schemas/config.schema.ts +3 -0
- package/src/schemas/context-view.config.schema.ts +13 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modal dialog shell. Owns everything about being a modal — framing, tab
|
|
3
|
+
* strip, tab cycling, per-tab input layers, optional shared filter input,
|
|
4
|
+
* notices, focus plumbing, and the help footer — so tab strategies only
|
|
5
|
+
* implement content. See `types.ts` for the tab/layer/scheme contracts.
|
|
6
|
+
*
|
|
7
|
+
* Input routing order:
|
|
8
|
+
* 1. Tab/Shift+Tab cycles tabs (wrapping, scheme reset).
|
|
9
|
+
* 2. The navigation scheme maps a key to a semantic action; `dismiss` pops
|
|
10
|
+
* the active tab's top layer, or completes the dialog with the cancel
|
|
11
|
+
* value when no layer is open. Other actions go to the top layer, or the
|
|
12
|
+
* active tab when no layer is open.
|
|
13
|
+
* 3. Unhandled raw keys go to the top layer, else the filter input, else
|
|
14
|
+
* the active tab.
|
|
15
|
+
*/
|
|
16
|
+
import type { KeybindingsManager, Theme } from '@earendil-works/pi-coding-agent';
|
|
17
|
+
import { type Component, type Focusable, Input, Key, matchesKey, type TUI } from '@earendil-works/pi-tui';
|
|
18
|
+
import { PiKeybindingsScheme } from './navigation/pi-scheme';
|
|
19
|
+
import { renderTabStrip } from './tab-strip';
|
|
20
|
+
import { fitToTerminalHeight, hintRow, normalizeTerminalRows, padLine, singleLine } from './text';
|
|
21
|
+
import type { Hint, ModalLayer, ModalTab, NavigationScheme } from './types';
|
|
22
|
+
|
|
23
|
+
/** Framing style: inline rules, or a rounded border for host-centered overlays. */
|
|
24
|
+
export type ModalFrame = 'inline' | 'bordered';
|
|
25
|
+
|
|
26
|
+
/** Height policy: natural content height, or bounded to half the terminal. */
|
|
27
|
+
export type ModalHeight = 'auto' | 'half';
|
|
28
|
+
|
|
29
|
+
export interface ModalDialogFilterOptions {
|
|
30
|
+
/** Query seeded into the filter input and applied to tabs on open. */
|
|
31
|
+
initialQuery?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ModalDialogOptions<TResult> {
|
|
35
|
+
tabs: ModalTab[];
|
|
36
|
+
initialTabIndex?: number;
|
|
37
|
+
/** Navigation scheme; defaults to the host keybindings scheme. */
|
|
38
|
+
navigation?: NavigationScheme;
|
|
39
|
+
frame?: ModalFrame;
|
|
40
|
+
height?: ModalHeight;
|
|
41
|
+
/** Optional title line rendered above the tab strip. */
|
|
42
|
+
title?: string | (() => string);
|
|
43
|
+
/** Warning-styled notice lines rendered below the tab strip. */
|
|
44
|
+
notices?: readonly string[] | (() => readonly string[]);
|
|
45
|
+
/** Cap on rendered notice lines; overflow is summarized. Defaults to unlimited. */
|
|
46
|
+
maxNoticeLines?: number;
|
|
47
|
+
/** Optional shared text filter between the tab strip and content. */
|
|
48
|
+
filter?: ModalDialogFilterOptions;
|
|
49
|
+
/** Value passed to `onComplete` when the dialog is dismissed. */
|
|
50
|
+
cancelValue: TResult;
|
|
51
|
+
onComplete: (result: TResult) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class ModalDialog<TResult> implements Component, Focusable {
|
|
55
|
+
private readonly scheme: NavigationScheme;
|
|
56
|
+
private readonly frame: ModalFrame;
|
|
57
|
+
private readonly height: ModalHeight;
|
|
58
|
+
private readonly filterInput: Input | undefined;
|
|
59
|
+
private readonly layerStacks = new Map<ModalTab, ModalLayer[]>();
|
|
60
|
+
private activeTabIndex: number;
|
|
61
|
+
private _focused = false;
|
|
62
|
+
|
|
63
|
+
public constructor(
|
|
64
|
+
private readonly tui: TUI,
|
|
65
|
+
private readonly theme: Theme,
|
|
66
|
+
private readonly keybindings: KeybindingsManager,
|
|
67
|
+
private readonly options: ModalDialogOptions<TResult>,
|
|
68
|
+
) {
|
|
69
|
+
if (options.tabs.length === 0) throw new Error('ModalDialog requires at least one tab');
|
|
70
|
+
this.scheme = options.navigation ?? new PiKeybindingsScheme(keybindings);
|
|
71
|
+
this.frame = options.frame ?? 'inline';
|
|
72
|
+
this.height = options.height ?? 'auto';
|
|
73
|
+
this.activeTabIndex = Math.min(Math.max(0, options.initialTabIndex ?? 0), options.tabs.length - 1);
|
|
74
|
+
|
|
75
|
+
for (const tab of options.tabs) {
|
|
76
|
+
tab.attach?.({
|
|
77
|
+
pushLayer: layer => this.pushLayer(tab, layer),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (options.filter !== undefined) {
|
|
82
|
+
this.filterInput = new Input();
|
|
83
|
+
this.filterInput.setValue(options.filter.initialQuery ?? '');
|
|
84
|
+
this.filterInput.focused = this._focused;
|
|
85
|
+
this.applyQuery(this.filterInput.getValue());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public get focused(): boolean {
|
|
90
|
+
return this._focused;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public set focused(value: boolean) {
|
|
94
|
+
this._focused = value;
|
|
95
|
+
if (this.filterInput) this.filterInput.focused = value;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public get activeTab(): ModalTab {
|
|
99
|
+
const tab = this.options.tabs[this.activeTabIndex];
|
|
100
|
+
if (tab === undefined) throw new Error('ModalDialog requires at least one tab');
|
|
101
|
+
return tab;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public get activeIndex(): number {
|
|
105
|
+
return this.activeTabIndex;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public invalidate(): void {
|
|
109
|
+
this.filterInput?.invalidate();
|
|
110
|
+
for (const tab of this.options.tabs) tab.invalidate?.();
|
|
111
|
+
for (const stack of this.layerStacks.values()) {
|
|
112
|
+
for (const layer of stack) layer.invalidate?.();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Complete the dialog with an explicit result (for example a selection). */
|
|
117
|
+
public complete(result: TResult): void {
|
|
118
|
+
this.options.onComplete(result);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public handleInput(data: string): void {
|
|
122
|
+
if (this.options.tabs.length > 1) {
|
|
123
|
+
if (matchesKey(data, Key.shift('tab'))) {
|
|
124
|
+
this.switchTab(-1);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (this.keybindings.matches(data, 'tui.input.tab') || matchesKey(data, Key.tab)) {
|
|
128
|
+
this.switchTab(1);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const navigation = this.scheme.consume(data);
|
|
134
|
+
if (navigation.action === 'dismiss') {
|
|
135
|
+
this.dismiss();
|
|
136
|
+
} else if (navigation.action !== undefined) {
|
|
137
|
+
this.content().handleNavigation(navigation.action);
|
|
138
|
+
} else if (!navigation.handled) {
|
|
139
|
+
const layer = this.topLayer();
|
|
140
|
+
if (layer !== undefined) {
|
|
141
|
+
layer.handleInput(data);
|
|
142
|
+
} else if (this.filterInput !== undefined) {
|
|
143
|
+
this.filterInput.handleInput(data);
|
|
144
|
+
this.applyQuery(this.filterInput.getValue());
|
|
145
|
+
} else {
|
|
146
|
+
this.activeTab.handleInput(data);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
this.tui.requestRender();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public render(width: number): string[] {
|
|
153
|
+
const safeWidth = Math.max(3, width);
|
|
154
|
+
if (this.frame === 'bordered') {
|
|
155
|
+
const inner = safeWidth - 2;
|
|
156
|
+
const lines = this.buildContentLines(inner, this.contentHeight());
|
|
157
|
+
const borderColor = (str: string) => this.theme.fg('border', str);
|
|
158
|
+
const horizontal = '─'.repeat(inner);
|
|
159
|
+
const side = borderColor('│');
|
|
160
|
+
return [borderColor(`╭${horizontal}╮`), ...lines.map(line => `${side}${padLine(line, inner)}${side}`), borderColor(`╰${horizontal}╯`)];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const rule = this.theme.fg('border', '─'.repeat(safeWidth));
|
|
164
|
+
const lines = [rule, ...this.buildContentLines(safeWidth, this.contentHeight()), rule];
|
|
165
|
+
const budget = this.heightBudget();
|
|
166
|
+
return budget === undefined ? lines : fitToTerminalHeight(lines, budget, rule);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// === Input routing internals ===
|
|
170
|
+
|
|
171
|
+
private switchTab(direction: -1 | 1): void {
|
|
172
|
+
const count = this.options.tabs.length;
|
|
173
|
+
this.activeTabIndex = (((this.activeTabIndex + direction) % count) + count) % count;
|
|
174
|
+
this.scheme.reset();
|
|
175
|
+
this.tui.requestRender();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private dismiss(): void {
|
|
179
|
+
const stack = this.layerStacks.get(this.activeTab);
|
|
180
|
+
if (stack !== undefined && stack.length > 0) {
|
|
181
|
+
stack.pop();
|
|
182
|
+
this.scheme.reset();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
this.options.onComplete(this.options.cancelValue);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private pushLayer(tab: ModalTab, layer: ModalLayer): void {
|
|
189
|
+
const stack = this.layerStacks.get(tab) ?? [];
|
|
190
|
+
stack.push(layer);
|
|
191
|
+
this.layerStacks.set(tab, stack);
|
|
192
|
+
this.scheme.reset();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private topLayer(): ModalLayer | undefined {
|
|
196
|
+
const stack = this.layerStacks.get(this.activeTab);
|
|
197
|
+
return stack === undefined || stack.length === 0 ? undefined : stack[stack.length - 1];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The input target for navigation actions: top layer, else active tab. */
|
|
201
|
+
private content(): ModalLayer | ModalTab {
|
|
202
|
+
return this.topLayer() ?? this.activeTab;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private applyQuery(query: string): void {
|
|
206
|
+
for (const tab of this.options.tabs) tab.applyFilter?.(query);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// === Rendering internals ===
|
|
210
|
+
|
|
211
|
+
/** Total framed-line budget for bounded dialogs; undefined when natural. */
|
|
212
|
+
private heightBudget(): number | undefined {
|
|
213
|
+
if (this.height !== 'half') return undefined;
|
|
214
|
+
return Math.max(3, Math.floor(normalizeTerminalRows(this.tui.terminal.rows) / 2));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Exact content-region height handed to the active tab or layer, computed
|
|
219
|
+
* from the budget minus chrome (frame, strip, notices, filter, footer).
|
|
220
|
+
*/
|
|
221
|
+
private contentHeight(): number | undefined {
|
|
222
|
+
const budget = this.heightBudget();
|
|
223
|
+
if (budget === undefined) return undefined;
|
|
224
|
+
const titleLines = this.titleText() === undefined ? 0 : 1;
|
|
225
|
+
const noticeLines = this.renderedNoticeCount();
|
|
226
|
+
const captionLines = this.filterInput === undefined ? 0 : (this.activeTab.filterCaption?.().length ?? 0);
|
|
227
|
+
// frame rules (2) + strip (1) + blanks around content (2) + footer (1) + filter input row (1 when present)
|
|
228
|
+
const chrome = 2 + 1 + 2 + 1 + titleLines + noticeLines + captionLines + (this.filterInput === undefined ? 0 : 1);
|
|
229
|
+
return Math.max(1, budget - chrome);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private buildContentLines(width: number, contentHeight: number | undefined): string[] {
|
|
233
|
+
const lines: string[] = [];
|
|
234
|
+
const title = this.titleText();
|
|
235
|
+
if (title !== undefined) lines.push(singleLine(title, width));
|
|
236
|
+
|
|
237
|
+
const labels = this.options.tabs.map(tab => tab.label);
|
|
238
|
+
lines.push(renderTabStrip(this.theme, labels, this.activeTabIndex, width));
|
|
239
|
+
lines.push(...this.noticeLines(width));
|
|
240
|
+
|
|
241
|
+
lines.push('');
|
|
242
|
+
if (this.filterInput !== undefined) {
|
|
243
|
+
for (const caption of this.activeTab.filterCaption?.() ?? []) {
|
|
244
|
+
lines.push(singleLine(caption, width));
|
|
245
|
+
}
|
|
246
|
+
lines.push(...this.filterInput.render(width), '');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const content = this.content().render(width, contentHeight);
|
|
250
|
+
if (contentHeight !== undefined) {
|
|
251
|
+
while (content.length < contentHeight) content.push('');
|
|
252
|
+
content.length = Math.min(content.length, contentHeight);
|
|
253
|
+
}
|
|
254
|
+
lines.push(...content, '');
|
|
255
|
+
|
|
256
|
+
lines.push(singleLine(hintRow(this.theme, this.footerHints()), width));
|
|
257
|
+
return lines;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private titleText(): string | undefined {
|
|
261
|
+
const title = this.options.title;
|
|
262
|
+
if (title === undefined) return undefined;
|
|
263
|
+
return typeof title === 'function' ? title() : title;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private noticeLines(width: number): string[] {
|
|
267
|
+
const all = this.allNotices();
|
|
268
|
+
const shown = all.slice(0, this.options.maxNoticeLines ?? all.length);
|
|
269
|
+
const lines = shown.map(notice => singleLine(this.theme.fg('warning', `⚠ ${notice}`), width));
|
|
270
|
+
if (all.length > shown.length) {
|
|
271
|
+
lines.push(singleLine(this.theme.fg('warning', `⚠ ${all.length - shown.length} more warning(s)`), width));
|
|
272
|
+
}
|
|
273
|
+
return lines;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Notice count as rendered, including the overflow summary line. */
|
|
277
|
+
private renderedNoticeCount(): number {
|
|
278
|
+
const all = this.allNotices();
|
|
279
|
+
const cap = this.options.maxNoticeLines ?? all.length;
|
|
280
|
+
return Math.min(all.length, cap) + (all.length > cap ? 1 : 0);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private allNotices(): readonly string[] {
|
|
284
|
+
const notices = this.options.notices;
|
|
285
|
+
if (notices === undefined) return [];
|
|
286
|
+
return typeof notices === 'function' ? notices() : notices;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private footerHints(): Hint[] {
|
|
290
|
+
const layer = this.topLayer();
|
|
291
|
+
const target = layer ?? this.activeTab;
|
|
292
|
+
const hints: Hint[] = [...this.scheme.hints(layer === undefined ? 'Navigate' : 'Scroll'), ...target.hints()];
|
|
293
|
+
if (this.options.tabs.length > 1) hints.push(['Tab', 'Switch']);
|
|
294
|
+
hints.push(['Esc', layer === undefined ? 'Close' : 'Back']);
|
|
295
|
+
return hints;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { KeybindingsManager } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
import type { Hint, NavigationResult, NavigationScheme } from '../types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Default navigation scheme driven by the host keybindings manager, so modal
|
|
6
|
+
* movement follows the user's configured `tui.select.*` bindings. Hint labels
|
|
7
|
+
* describe the default keys; remapped keys are not reflected (accepted
|
|
8
|
+
* limitation).
|
|
9
|
+
*/
|
|
10
|
+
export class PiKeybindingsScheme implements NavigationScheme {
|
|
11
|
+
public constructor(private readonly keybindings: KeybindingsManager) {}
|
|
12
|
+
|
|
13
|
+
public consume(data: string): NavigationResult {
|
|
14
|
+
if (this.keybindings.matches(data, 'tui.select.up')) return { handled: true, action: 'step-back' };
|
|
15
|
+
if (this.keybindings.matches(data, 'tui.select.down')) return { handled: true, action: 'step-forward' };
|
|
16
|
+
if (this.keybindings.matches(data, 'tui.select.pageUp')) return { handled: true, action: 'page-back' };
|
|
17
|
+
if (this.keybindings.matches(data, 'tui.select.pageDown')) return { handled: true, action: 'page-forward' };
|
|
18
|
+
if (this.keybindings.matches(data, 'tui.select.confirm')) return { handled: true, action: 'confirm' };
|
|
19
|
+
if (this.keybindings.matches(data, 'tui.select.cancel')) return { handled: true, action: 'dismiss' };
|
|
20
|
+
return { handled: false };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public reset(): void {
|
|
24
|
+
// Stateless: no chords to clear.
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public hints(stepLabel = 'Navigate'): Hint[] {
|
|
28
|
+
return [
|
|
29
|
+
['↑↓', stepLabel],
|
|
30
|
+
['PgUp/PgDn', 'Page'],
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Forked from dimk90/pi-context-view at f6f007b867212bcf81a61519c8e40ce209cdd608 (MIT). */
|
|
2
|
+
import { Key, matchesKey } from '@earendil-works/pi-tui';
|
|
3
|
+
import { STEP_KEY_HINT } from '../text';
|
|
4
|
+
import type { Hint, NavigationResult, NavigationScheme } from '../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Vim-style navigation scheme for read-only inspector modals: `j/k` plus
|
|
8
|
+
* arrow keys step, `Ctrl+u/d` page, `gg/G` jump to boundaries, Enter
|
|
9
|
+
* confirms, and Esc/`q` dismiss. PageUp/PageDown/Home/End are intentionally
|
|
10
|
+
* swallowed — they were replaced by the Vim bindings. The stateful `gg`
|
|
11
|
+
* chord is cleared by `reset()` on tab switches and layer changes.
|
|
12
|
+
*
|
|
13
|
+
* Do not combine this scheme with a dialog filter input: printable keys are
|
|
14
|
+
* commands here, so typing cannot reach a text field.
|
|
15
|
+
*/
|
|
16
|
+
export class VimNavigationScheme implements NavigationScheme {
|
|
17
|
+
private pendingG = false;
|
|
18
|
+
|
|
19
|
+
public reset(): void {
|
|
20
|
+
this.pendingG = false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public consume(data: string): NavigationResult {
|
|
24
|
+
if (data === 'g') {
|
|
25
|
+
if (this.pendingG) {
|
|
26
|
+
this.pendingG = false;
|
|
27
|
+
return { handled: true, action: 'first' };
|
|
28
|
+
}
|
|
29
|
+
this.pendingG = true;
|
|
30
|
+
return { handled: true };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.pendingG = false;
|
|
34
|
+
if (data === 'G') return { handled: true, action: 'last' };
|
|
35
|
+
if (matchesKey(data, Key.ctrl('u'))) return { handled: true, action: 'page-back' };
|
|
36
|
+
if (matchesKey(data, Key.ctrl('d'))) return { handled: true, action: 'page-forward' };
|
|
37
|
+
if (matchesKey(data, Key.up) || data === 'k') return { handled: true, action: 'step-back' };
|
|
38
|
+
if (matchesKey(data, Key.down) || data === 'j') return { handled: true, action: 'step-forward' };
|
|
39
|
+
if (matchesKey(data, Key.enter)) return { handled: true, action: 'confirm' };
|
|
40
|
+
if (matchesKey(data, Key.escape) || data === 'q') return { handled: true, action: 'dismiss' };
|
|
41
|
+
|
|
42
|
+
// These keys were intentionally replaced by Vim bindings.
|
|
43
|
+
if (matchesKey(data, Key.pageUp) || matchesKey(data, Key.pageDown) || matchesKey(data, Key.home) || matchesKey(data, Key.end)) {
|
|
44
|
+
return { handled: true };
|
|
45
|
+
}
|
|
46
|
+
return { handled: false };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public hints(stepLabel = 'Navigate'): Hint[] {
|
|
50
|
+
return [
|
|
51
|
+
[STEP_KEY_HINT, stepLabel],
|
|
52
|
+
['Ctrl+u/d', 'Page'],
|
|
53
|
+
['gg/G', 'Bounds'],
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrollable text preview layer. Renders a title line with right-aligned
|
|
3
|
+
* metadata, a scrolling body window, an overflow counter, and optional fixed
|
|
4
|
+
* description lines. Dismissal is owned by the dialog shell: it pops this
|
|
5
|
+
* layer before the dialog itself can close.
|
|
6
|
+
*/
|
|
7
|
+
import type { Theme } from '@earendil-works/pi-coding-agent';
|
|
8
|
+
import { PreviewScroller } from './list-navigator';
|
|
9
|
+
import { BODY_INDENT, calculateViewport, fitLine, spreadLine } from './text';
|
|
10
|
+
import type { Hint, ModalLayer, NavigationAction } from './types';
|
|
11
|
+
|
|
12
|
+
export interface PreviewLayerOptions {
|
|
13
|
+
/** Styled title rendered on the left of the header line. */
|
|
14
|
+
title: string | (() => string);
|
|
15
|
+
/** Muted metadata rendered right-aligned beside the title. */
|
|
16
|
+
meta?: string | (() => string);
|
|
17
|
+
/** Body lines for the given width; wrapping is the caller's concern. */
|
|
18
|
+
body: (width: number) => string[];
|
|
19
|
+
/** Optional fixed description lines pinned below the body. */
|
|
20
|
+
description?: (width: number) => string[];
|
|
21
|
+
/** Layer-specific hints appended after the navigation scheme's hints. */
|
|
22
|
+
hints?: () => Hint[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class PreviewLayer implements ModalLayer {
|
|
26
|
+
private readonly scroller = new PreviewScroller();
|
|
27
|
+
|
|
28
|
+
public constructor(
|
|
29
|
+
private readonly theme: Theme,
|
|
30
|
+
private readonly options: PreviewLayerOptions,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
public hints(): Hint[] {
|
|
34
|
+
return this.options.hints?.() ?? [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public handleInput(_data: string): void {
|
|
38
|
+
// Previews are scroll-only; dismissal is handled by the shell.
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public handleNavigation(action: NavigationAction): void {
|
|
42
|
+
switch (action) {
|
|
43
|
+
case 'step-back':
|
|
44
|
+
this.scroller.scrollBy(-1);
|
|
45
|
+
break;
|
|
46
|
+
case 'step-forward':
|
|
47
|
+
this.scroller.scrollBy(1);
|
|
48
|
+
break;
|
|
49
|
+
case 'page-back':
|
|
50
|
+
this.scroller.page(-1);
|
|
51
|
+
break;
|
|
52
|
+
case 'page-forward':
|
|
53
|
+
this.scroller.page(1);
|
|
54
|
+
break;
|
|
55
|
+
case 'first':
|
|
56
|
+
this.scroller.scrollTo(0);
|
|
57
|
+
break;
|
|
58
|
+
case 'last':
|
|
59
|
+
this.scroller.scrollTo(this.scroller.maxOffset);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public render(width: number, height: number | undefined): string[] {
|
|
65
|
+
const body = this.options.body(width);
|
|
66
|
+
const description = this.options.description?.(width) ?? [];
|
|
67
|
+
// title + blank, plus blank + description when present
|
|
68
|
+
const fixedLineCount = 2 + (description.length > 0 ? description.length + 1 : 0);
|
|
69
|
+
const viewport = calculateViewport(body.length, height ?? body.length + fixedLineCount, fixedLineCount);
|
|
70
|
+
this.scroller.setExtent(body.length, viewport.visibleCount);
|
|
71
|
+
|
|
72
|
+
const title = typeof this.options.title === 'function' ? this.options.title() : this.options.title;
|
|
73
|
+
const metaOption = this.options.meta;
|
|
74
|
+
const meta = typeof metaOption === 'function' ? metaOption() : (metaOption ?? '');
|
|
75
|
+
|
|
76
|
+
const lines: string[] = [spreadLine(title, meta, width), ''];
|
|
77
|
+
const start = this.scroller.offset;
|
|
78
|
+
for (let index = start; index < start + viewport.visibleCount; index++) {
|
|
79
|
+
lines.push(body[index] ?? '');
|
|
80
|
+
}
|
|
81
|
+
if (viewport.showScroll) {
|
|
82
|
+
lines.push(fitLine(this.theme.fg('dim', `${BODY_INDENT}(${this.scroller.visibleEnd}/${body.length})`), width));
|
|
83
|
+
}
|
|
84
|
+
if (description.length > 0) lines.push('', ...description);
|
|
85
|
+
return lines;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Width/height-keyed cache for one rendered frame. Views that re-render only
|
|
3
|
+
* on input, theme, or viewport changes can skip layout work when the host
|
|
4
|
+
* re-requests the same frame.
|
|
5
|
+
*/
|
|
6
|
+
export class RenderCache {
|
|
7
|
+
private cachedWidth: number | undefined;
|
|
8
|
+
private cachedHeight: number | undefined;
|
|
9
|
+
private cachedLines: string[] | undefined;
|
|
10
|
+
|
|
11
|
+
/** The cached frame when it matches the requested viewport, else undefined. */
|
|
12
|
+
public read(width: number, height: number | undefined): string[] | undefined {
|
|
13
|
+
if (this.cachedLines !== undefined && this.cachedWidth === width && this.cachedHeight === height) {
|
|
14
|
+
return this.cachedLines;
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Store one rendered frame for the requested viewport. */
|
|
20
|
+
public write(width: number, height: number | undefined, lines: string[]): string[] {
|
|
21
|
+
this.cachedWidth = width;
|
|
22
|
+
this.cachedHeight = height;
|
|
23
|
+
this.cachedLines = lines;
|
|
24
|
+
return lines;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Drop the cached frame after data, theme, or input changes. */
|
|
28
|
+
public clear(): void {
|
|
29
|
+
this.cachedWidth = undefined;
|
|
30
|
+
this.cachedHeight = undefined;
|
|
31
|
+
this.cachedLines = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Width-aware tab strip: highlights the active label and, when labels
|
|
3
|
+
* overflow, keeps a window of labels around the active tab with leading and
|
|
4
|
+
* trailing omission indicators. Pure rendering — no input handling.
|
|
5
|
+
*/
|
|
6
|
+
import type { Theme } from '@earendil-works/pi-coding-agent';
|
|
7
|
+
import { truncateToWidth, visibleWidth } from '@earendil-works/pi-tui';
|
|
8
|
+
import { singleLine } from './text';
|
|
9
|
+
|
|
10
|
+
const SEPARATOR = ' ';
|
|
11
|
+
const OMISSION = '…';
|
|
12
|
+
|
|
13
|
+
/** Render one tab-strip line fitted to `width`, highlighting `activeIndex`. */
|
|
14
|
+
export function renderTabStrip(theme: Theme, labels: string[], activeIndex: number, width: number): string {
|
|
15
|
+
const safeWidth = Math.max(1, width);
|
|
16
|
+
if (labels.length === 0) return '';
|
|
17
|
+
if (visibleWidth(labels.join(SEPARATOR)) <= safeWidth) {
|
|
18
|
+
return styleLabels(theme, labels, activeIndex, 0, labels.length - 1, false, false);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let start = activeIndex;
|
|
22
|
+
let end = activeIndex;
|
|
23
|
+
let expandLeft = true;
|
|
24
|
+
while (true) {
|
|
25
|
+
const nextStart = expandLeft && start > 0 ? start - 1 : start;
|
|
26
|
+
const nextEnd = !expandLeft && end < labels.length - 1 ? end + 1 : end;
|
|
27
|
+
expandLeft = !expandLeft;
|
|
28
|
+
|
|
29
|
+
if (nextStart === start && nextEnd === end) {
|
|
30
|
+
if ((start === 0 || nextStart === start) && (end === labels.length - 1 || nextEnd === end)) break;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const candidate = plainViewport(labels, nextStart, nextEnd);
|
|
35
|
+
if (visibleWidth(candidate) <= safeWidth) {
|
|
36
|
+
start = nextStart;
|
|
37
|
+
end = nextEnd;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const otherStart = start > 0 ? start - 1 : start;
|
|
42
|
+
const otherEnd = end < labels.length - 1 ? end + 1 : end;
|
|
43
|
+
if ((otherStart === start && otherEnd === end) || visibleWidth(plainViewport(labels, otherStart, otherEnd)) > safeWidth) break;
|
|
44
|
+
start = otherStart;
|
|
45
|
+
end = otherEnd;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const leftOmitted = start > 0;
|
|
49
|
+
const rightOmitted = end < labels.length - 1;
|
|
50
|
+
if (visibleWidth(plainViewport(labels, start, end)) <= safeWidth) {
|
|
51
|
+
return styleLabels(theme, labels, activeIndex, start, end, leftOmitted, rightOmitted);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const prefix = leftOmitted ? `${OMISSION} ` : '';
|
|
55
|
+
const suffix = rightOmitted ? ` ${OMISSION}` : '';
|
|
56
|
+
const activeBudget = Math.max(1, safeWidth - visibleWidth(prefix) - visibleWidth(suffix));
|
|
57
|
+
const active = truncateToWidth(labels[activeIndex] ?? '', activeBudget, '');
|
|
58
|
+
return singleLine(`${theme.fg('muted', prefix)}${theme.fg('accent', theme.bold(active))}${theme.fg('muted', suffix)}`, safeWidth);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Plain joined label window with omission markers, used for width probes. */
|
|
62
|
+
function plainViewport(labels: string[], start: number, end: number): string {
|
|
63
|
+
const parts = labels.slice(start, end + 1);
|
|
64
|
+
if (start > 0) parts.unshift(OMISSION);
|
|
65
|
+
if (end < labels.length - 1) parts.push(OMISSION);
|
|
66
|
+
return parts.join(SEPARATOR);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Join one styled label window: active label accent+bold, others muted. */
|
|
70
|
+
function styleLabels(
|
|
71
|
+
theme: Theme,
|
|
72
|
+
labels: string[],
|
|
73
|
+
activeIndex: number,
|
|
74
|
+
start: number,
|
|
75
|
+
end: number,
|
|
76
|
+
leftOmitted: boolean,
|
|
77
|
+
rightOmitted: boolean,
|
|
78
|
+
): string {
|
|
79
|
+
const parts: string[] = [];
|
|
80
|
+
if (leftOmitted) parts.push(theme.fg('muted', OMISSION));
|
|
81
|
+
for (let index = start; index <= end; index++) {
|
|
82
|
+
const label = labels[index] ?? '';
|
|
83
|
+
parts.push(index === activeIndex ? theme.fg('accent', theme.bold(label)) : theme.fg('muted', label));
|
|
84
|
+
}
|
|
85
|
+
if (rightOmitted) parts.push(theme.fg('muted', OMISSION));
|
|
86
|
+
return parts.join(theme.fg('muted', SEPARATOR));
|
|
87
|
+
}
|