@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,212 @@
|
|
|
1
|
+
# Modal UI Library
|
|
2
|
+
|
|
3
|
+
Build a consistent TUI modal for a Pi extension without rewriting modal plumbing. You supply **tab content**; the shell supplies everything else — frame, tab strip, tab cycling, navigation, filtering, previews, dismissal, focus, and the help footer.
|
|
4
|
+
|
|
5
|
+
**Self-containment rule:** this directory imports only from Pi host packages (`@earendil-works/pi-tui`, `@earendil-works/pi-coding-agent` types). Never import plugin-specific modules here — `test/modal/dependency-audit.test.ts` enforces it, and it's what makes this folder copy-portable to other Pi plugins.
|
|
6
|
+
|
|
7
|
+
## 60-second quickstart
|
|
8
|
+
|
|
9
|
+
A minimal "pick a thing" modal:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import type { ExtensionCommandContext } from '@earendil-works/pi-coding-agent';
|
|
13
|
+
import { ListTab, ModalDialog } from './libs/modal/index.js'; // adjust path
|
|
14
|
+
|
|
15
|
+
async function openPicker(ctx: ExtensionCommandContext) {
|
|
16
|
+
let dialog: ModalDialog<string | null>;
|
|
17
|
+
const result = await ctx.ui.custom<string | null>((tui, theme, keybindings, done) => {
|
|
18
|
+
dialog = new ModalDialog<string | null>(tui, theme, keybindings, {
|
|
19
|
+
tabs: [
|
|
20
|
+
new ListTab(theme, {
|
|
21
|
+
label: counts => `[Branches ${counts.filtered}]`,
|
|
22
|
+
items: ['main', 'develop', 'feature-x'],
|
|
23
|
+
renderRow: (item, selected) => `${selected ? '→ ' : ' '}${item}`,
|
|
24
|
+
// `dialog` is assigned before any key can be pressed, so the
|
|
25
|
+
// closure safely completes the dialog with the picked value:
|
|
26
|
+
onConfirm: item => dialog.complete(item),
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
cancelValue: null,
|
|
30
|
+
onComplete: done,
|
|
31
|
+
});
|
|
32
|
+
return dialog;
|
|
33
|
+
});
|
|
34
|
+
// result === selected item, or null when the user pressed Esc
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
That's the whole job. The shell gives you: Esc-to-cancel, `↑↓`/PgUp/PgDn via the user's own keybindings, Enter-to-confirm, a tab strip, and a help footer.
|
|
39
|
+
|
|
40
|
+
## Concepts
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
┌──────────────────────────────────────────────┐
|
|
44
|
+
│ frame: 'inline' rules | 'bordered' rounded │ ← shell
|
|
45
|
+
│ ┌──────────────────────────────────────────┐ │
|
|
46
|
+
│ │ title? (string | () => string) │ │ ← shell option
|
|
47
|
+
│ │ tab strip [One 3] [Two 12] │ │ ← shell, labels live
|
|
48
|
+
│ │ ⚠ notices? │ │ ← shell option
|
|
49
|
+
│ │ filter caption? + filter input? │ │ ← shell option + tab caption
|
|
50
|
+
│ │ │ │
|
|
51
|
+
│ │ content: active tab, or its top layer │ │ ← YOUR code
|
|
52
|
+
│ │ │ │
|
|
53
|
+
│ │ ↑↓ Navigate · Enter Select · Esc Close │ │ ← shell footer
|
|
54
|
+
│ └──────────────────────────────────────────┘ │
|
|
55
|
+
└──────────────────────────────────────────────┘
|
|
56
|
+
|
|
57
|
+
Input routing (first match wins):
|
|
58
|
+
1. Tab / Shift+Tab → cycle tabs (wraps; scheme resets)
|
|
59
|
+
2. navigation scheme → semantic action:
|
|
60
|
+
dismiss → pop active tab's top layer, else cancel dialog
|
|
61
|
+
step/page/first/last/ → top layer, else active tab
|
|
62
|
+
confirm
|
|
63
|
+
3. raw key → top layer, else filter input, else active tab
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Three interfaces cover all content:
|
|
67
|
+
|
|
68
|
+
- **`ModalTab`** — one tab of content (usually you use the pre-built `ListTab` instead of writing your own).
|
|
69
|
+
- **`ModalLayer`** — full-content overlay above a tab (usually the pre-built `PreviewLayer`). Each tab has its **own** layer stack: Tab-switching works while a layer is open, and each tab restores its layer when you return.
|
|
70
|
+
- **`NavigationScheme`** — pure key→action mapper. Default: `PiKeybindingsScheme` (follows the user's `tui.select.*` remappings). Alternative: `VimNavigationScheme` (`j/k`, `Ctrl+u/d`, `gg/G`, `q`) for read-only inspectors.
|
|
71
|
+
|
|
72
|
+
## Recipes
|
|
73
|
+
|
|
74
|
+
### Filterable picker (model-select style)
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const tab = new ListTab<ModelItem>(theme, {
|
|
78
|
+
label: counts => `[Models ${counts.filtered}]`, // re-read every render
|
|
79
|
+
items,
|
|
80
|
+
filterText: item => item.searchText, // enables fuzzy filtering
|
|
81
|
+
filterCaption: () => [theme.fg('muted', 'Filter:')], // line(s) above the input
|
|
82
|
+
emptyMessage: 'No models available.', // items.length === 0
|
|
83
|
+
noMatchMessage: 'No matching models', // filter hides everything
|
|
84
|
+
wrap: true, // wrap selection at ends (picker convention)
|
|
85
|
+
visibleCount: 10, // fixed window (default: fill height)
|
|
86
|
+
initialIndex: currentIndex, // preselect the current entry
|
|
87
|
+
renderRow: (item, selected) => ...,
|
|
88
|
+
footer: ({ selected }) => selected ? ['', theme.fg('muted', ` ${selected.description}`)] : [],
|
|
89
|
+
onConfirm: item => dialog.complete(item.model),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
new ModalDialog(tui, theme, keybindings, {
|
|
93
|
+
tabs: [tab],
|
|
94
|
+
filter: { initialQuery: '' }, // one query shared across ALL tabs
|
|
95
|
+
title: () => `${theme.bold('Select Model')}`,
|
|
96
|
+
notices: configWarnings, // '⚠'-prefixed, warning-styled
|
|
97
|
+
maxNoticeLines: 4, // overflow becomes "N more warning(s)"
|
|
98
|
+
cancelValue: null,
|
|
99
|
+
onComplete: done,
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`footer` runs in **all** states (list, empty, no-match) — use it for things like config warnings that must always show.
|
|
104
|
+
|
|
105
|
+
### Read-only inspector with Vim keys and previews (context-view style)
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
new ModalDialog<undefined>(tui, theme, keybindings, {
|
|
109
|
+
tabs: [new UsageView(theme, input), new InjectionsView(theme, input)],
|
|
110
|
+
navigation: new VimNavigationScheme(), // j/k, Ctrl+u/d, gg/G, q
|
|
111
|
+
height: 'half', // bound to half the terminal; tabs get exact content height
|
|
112
|
+
notices: degradedReason ? [degradedReason] : [],
|
|
113
|
+
cancelValue: undefined,
|
|
114
|
+
onComplete: done,
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
A custom tab opens a preview by pushing a layer via the context it receives in `attach`:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
class InspectorTab implements ModalTab {
|
|
122
|
+
private context: ModalTabContext | undefined;
|
|
123
|
+
private readonly navigator = new ListNavigator(this.rows.length, 1);
|
|
124
|
+
private readonly cache = new RenderCache();
|
|
125
|
+
|
|
126
|
+
get label() { return '[Inspector]'; }
|
|
127
|
+
attach(context: ModalTabContext) { this.context = context; }
|
|
128
|
+
hints() { return [['Enter', 'Preview']]; } // shell adds scheme + Esc/Tab hints
|
|
129
|
+
handleInput(data: string) { /* raw keys only, e.g. matchesKey(data, 'z') */ }
|
|
130
|
+
|
|
131
|
+
handleNavigation(action: NavigationAction) {
|
|
132
|
+
if (action === 'confirm') return this.openPreview();
|
|
133
|
+
if (action === 'step-forward') this.navigator.moveBy(1);
|
|
134
|
+
// ...map step/page/first/last to the navigator; 'dismiss' never arrives here
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private openPreview() {
|
|
138
|
+
this.context?.pushLayer(new PreviewLayer(theme, {
|
|
139
|
+
title: () => this.theme.fg('accent', this.theme.bold(row.label)),
|
|
140
|
+
meta: () => this.theme.fg('muted', `${row.tokens} tokens`),
|
|
141
|
+
body: width => wrapMyText(width), // you wrap; layer windows + counts
|
|
142
|
+
description: width => [], // optional pinned lines below body
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
render(width: number, height: number | undefined): string[] {
|
|
147
|
+
// height is the EXACT content region when the dialog is bounded ('half');
|
|
148
|
+
// pad/truncate to fill it (shell also pads defensively). undefined = natural height.
|
|
149
|
+
}
|
|
150
|
+
invalidate() { this.cache.clear(); } // shell calls this on theme change
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`PreviewLayer` handles scrolling, the `(12/87)` overflow counter, and Esc-pops-itself. It reacts to navigation actions; raw keys are ignored.
|
|
155
|
+
|
|
156
|
+
### Centered overlay
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
// frame 'bordered' draws the rounded border; the HOST centers it:
|
|
160
|
+
await ctx.ui.custom(
|
|
161
|
+
(tui, theme, keybindings, done) => new ModalDialog(tui, theme, keybindings, { frame: 'bordered', /* ... */ }),
|
|
162
|
+
{ overlay: true, overlayOptions: { anchor: 'center', width: '85%', margin: 1 } },
|
|
163
|
+
);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Behavior contract (what users can rely on)
|
|
167
|
+
|
|
168
|
+
- **Esc/q** closes the top preview layer first, then the dialog (`onComplete(cancelValue)`).
|
|
169
|
+
- **Tab / Shift+Tab** cycles tabs even while a preview is open; every tab keeps its selection, scroll, zoom, and open layers.
|
|
170
|
+
- **Tab labels are live** — return counts from a closure and the strip updates on the next render.
|
|
171
|
+
- **`gg` chords never leak** across tab switches or layer push/pop (shell calls `scheme.reset()`).
|
|
172
|
+
- **Filter + Vim don't mix** — a modal gets a text filter **or** a command-style scheme, never both (`j` can't mean "down" and "j"). No insert/normal modes; don't build one.
|
|
173
|
+
- **Height 'half'** bounds the whole dialog to `floor(terminal.rows / 2)` lines including frame; tabs must render exactly the `height` they're given.
|
|
174
|
+
|
|
175
|
+
## API reference
|
|
176
|
+
|
|
177
|
+
### `ModalDialog<TResult>` options
|
|
178
|
+
|
|
179
|
+
| Option | Type | Default | Purpose |
|
|
180
|
+
| --- | --- | --- | --- |
|
|
181
|
+
| `tabs` | `ModalTab[]` | required | ≥1 tab; first is active unless `initialTabIndex` |
|
|
182
|
+
| `navigation` | `NavigationScheme` | `PiKeybindingsScheme` | key→action mapping |
|
|
183
|
+
| `frame` | `'inline' \| 'bordered'` | `'inline'` | rules vs rounded border |
|
|
184
|
+
| `height` | `'auto' \| 'half'` | `'auto'` | bound to half terminal |
|
|
185
|
+
| `title` | `string \| () => string` | — | line above the tab strip |
|
|
186
|
+
| `notices` | `string[] \| () => string[]` | — | `⚠` warning lines below the strip |
|
|
187
|
+
| `maxNoticeLines` | `number` | unlimited | overflow summary line |
|
|
188
|
+
| `filter` | `{ initialQuery? }` | — | shared text input; calls `tab.applyFilter` |
|
|
189
|
+
| `cancelValue` / `onComplete` | `TResult` / callback | required | dismissal result + sink |
|
|
190
|
+
|
|
191
|
+
Methods: `complete(result)`, `handleInput(data)`, `render(width)`, `invalidate()`, `focused`, `activeTab`, `activeIndex`.
|
|
192
|
+
|
|
193
|
+
### `ListTab<T>` options
|
|
194
|
+
|
|
195
|
+
`label` (string or `(counts) => string`), `items`, `renderRow(item, selected, width)`, `onConfirm(item)` are required. Optional: `filterText`, `filterCaption`, `footer(state)`, `emptyMessage`, `noMatchMessage`, `wrap` (default clamp), `visibleCount`, `initialIndex`, `hints`.
|
|
196
|
+
|
|
197
|
+
### Building blocks for custom tabs
|
|
198
|
+
|
|
199
|
+
`ListNavigator` (selection + scroll window; `wrap` option; `setRowCount` for filtered lists) · `PreviewScroller` · `PreviewLayer` · `RenderCache` (`read`/`write`/`clear` keyed on width+height) · text helpers `fitLine`, `spreadLine`, `padLine`, `singleLine`, `hintRow`, `wrapDescriptionLines`, `calculateViewport`, `fitToTerminalHeight`, `BODY_INDENT`.
|
|
200
|
+
|
|
201
|
+
### `NavigationAction`
|
|
202
|
+
|
|
203
|
+
`'step-back' | 'step-forward' | 'page-back' | 'page-forward' | 'first' | 'last' | 'confirm' | 'dismiss'` — tabs/layers never receive `'dismiss'`; the shell owns it.
|
|
204
|
+
|
|
205
|
+
## Testing your modal
|
|
206
|
+
|
|
207
|
+
Construct with stub host objects and drive `handleInput` with raw keys — no terminal needed. See `test/modal/modal-dialog.test.ts` for the pattern (identity `theme`, `{ terminal: { rows: 24 }, requestRender() {} }` TUI, a `matches()` stub for `KeybindingsManager`).
|
|
208
|
+
|
|
209
|
+
## In-repo examples
|
|
210
|
+
|
|
211
|
+
- `src/extensions/model-select/model-select-dialog.ts` — filterable multi-tab picker, both frames.
|
|
212
|
+
- `src/extensions/context-view/ui/context-view-dialog.ts` + `usage-view.ts` / `injections-view.ts` — Vim inspector, half-height, custom tabs, preview layers.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# src/libs/modal/
|
|
2
|
+
|
|
3
|
+
## Responsibility
|
|
4
|
+
|
|
5
|
+
`src/libs/modal/` is the shared modal dialog library for building consistent TUI modals: a shell that owns modal plumbing (framing, tab strip, tab cycling, per-tab input layers, shared filter input, notices, help footer, focus), pluggable navigation schemes, tab content strategies, a generic selectable-list tab, a scrollable preview layer, and pure text/layout helpers. Extensions supply tab data and content rendering only.
|
|
6
|
+
|
|
7
|
+
The library is self-contained: it imports only from Pi host packages (`@earendil-works/pi-tui`, `@earendil-works/pi-coding-agent` types) so the directory can be copied unchanged into other Pi plugin projects. A dependency-audit test enforces this. Fork-attribution headers on files originating from `dimk90/pi-context-view` (MIT) are preserved.
|
|
8
|
+
|
|
9
|
+
## Module Roles
|
|
10
|
+
|
|
11
|
+
- `types.ts` — Contracts: `NavigationAction`/`NavigationResult`/`NavigationScheme` (pure key→action mappers), `ModalTab` (content strategy), `ModalLayer`, `ModalTabContext` (`pushLayer`), `Hint`.
|
|
12
|
+
- `modal-dialog.ts` — `ModalDialog<TResult>` shell (`Component + Focusable`). Options: tabs, initial tab, navigation scheme (default `PiKeybindingsScheme`), frame (`inline` rules | `bordered` rounded border), height (`auto` | `half` terminal), title, notices (+ cap), filter, cancel value, completion callback. Routes input: tab cycling (`Tab`/`Shift+Tab`, wrapping, scheme reset) → scheme action (`dismiss` pops the active tab's layer stack or completes with the cancel value; other actions go to the top layer, else active tab) → raw keys (top layer, else filter input, else active tab). Renders frame, strip, notices, filter slot, content region (exact height when bounded), and a footer composed of scheme + tab/layer hints + universal hints. `complete(result)` resolves explicit results (for example selections).
|
|
13
|
+
- `navigation/pi-scheme.ts` — `PiKeybindingsScheme`: maps host `tui.select.*` keybindings to actions (default scheme; stateless).
|
|
14
|
+
- `navigation/vim-scheme.ts` — `VimNavigationScheme`: `j/k`/arrows, `Ctrl+u/d`, `gg/G` chord (cleared by `reset()`), Enter confirm, Esc/`q` dismiss; swallows PageUp/PageDown/Home/End. For read-only inspectors; do not combine with a filter input.
|
|
15
|
+
- `tabs/list-tab.ts` — `ListTab<T>` generic picker tab: items + row renderer + confirm callback, optional fuzzy filtering (`filterText`) wired to the dialog filter, dynamic count labels, filter captions, footer hook (rendered below the list in all states), empty/no-match messages, fixed or height-driven window, clamp (default) or wrap selection.
|
|
16
|
+
- `preview-layer.ts` — `PreviewLayer`: title + right-aligned meta header, `PreviewScroller`-windowed body, overflow counter, optional pinned description; dismissal is shell-owned (pops the layer).
|
|
17
|
+
- `list-navigator.ts` — `ListNavigator` (selection + scroll window; clamp default, `wrap` option; `setRowCount` for filtered lists) and `PreviewScroller` (scroll-only window over wrapped lines). Forked from `pi-context-view`.
|
|
18
|
+
- `tab-strip.ts` — Width-aware tab strip: active label highlighted; overflow keeps a window around the active tab with `…` omission indicators. Labels are re-read each render.
|
|
19
|
+
- `text.ts` — Pure helpers: `fitLine`, `singleLine`, `padLine`, `spreadLine`, `hintRow`, `wrapDescriptionLines`, `calculateViewport`, `fitToTerminalHeight`, `normalizeTerminalRows`, `BODY_INDENT`, `STEP_KEY_HINT`. Forked from `pi-context-view`.
|
|
20
|
+
- `render-cache.ts` — `RenderCache`: width/height-keyed frame cache (`read`/`write`/`clear`) for stateful tabs.
|
|
21
|
+
- `index.ts` — Barrel exports.
|
|
22
|
+
|
|
23
|
+
## Data & Control Flow
|
|
24
|
+
|
|
25
|
+
1. An extension builds `ModalTab`s (commonly `ListTab`, or custom tabs using `ListNavigator`/`PreviewLayer`/`RenderCache`) and constructs `ModalDialog` with frame/height/scheme/filter options plus `cancelValue`/`onComplete`.
|
|
26
|
+
2. The shell attaches a `ModalTabContext` to each tab (for `pushLayer`) and applies any initial filter query.
|
|
27
|
+
3. The host (`ctx.ui.custom`) drives `render(width)` and `handleInput(data)`; the shell routes input per the order above and calls `tui.requestRender()` after input.
|
|
28
|
+
4. Confirm flows: scheme `confirm` → tab `handleNavigation` → tab's confirm callback → `dialog.complete(value)`. Dismissal pops the active tab's top layer first, then completes with the cancel value.
|
|
29
|
+
|
|
30
|
+
## Integration Points
|
|
31
|
+
|
|
32
|
+
- **Consumers**: `src/extensions/model-select/model-select-dialog.ts` (three `ListTab` sections, filter slot, both frame styles, wrap selection) and `src/extensions/context-view/ui/context-view-dialog.ts` (Vim scheme, half-height, notices; custom `UsageView`/`InjectionsView` tabs with `PreviewLayer` previews).
|
|
33
|
+
- **Host packages**: `pi-tui` (`Component`, `Focusable`, `Input`, `Key`, `matchesKey`, `fuzzyFilter`, width helpers), `pi-coding-agent` types (`Theme`, `KeybindingsManager`).
|
|
34
|
+
- **Tests**: `test/modal/` (schemes, shell behavior, self-containment audit).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared modal dialog library: shell, navigation schemes, tab strategies,
|
|
3
|
+
* layers, and layout helpers. Self-contained — imports only from Pi host
|
|
4
|
+
* packages so the directory can be copied into other Pi plugin projects.
|
|
5
|
+
*/
|
|
6
|
+
export { ListNavigator, type ListNavigatorOptions, PreviewScroller } from './list-navigator';
|
|
7
|
+
export { ModalDialog, type ModalDialogOptions, type ModalFrame, type ModalHeight } from './modal-dialog';
|
|
8
|
+
export { PiKeybindingsScheme } from './navigation/pi-scheme';
|
|
9
|
+
export { VimNavigationScheme } from './navigation/vim-scheme';
|
|
10
|
+
export { PreviewLayer, type PreviewLayerOptions } from './preview-layer';
|
|
11
|
+
export { RenderCache } from './render-cache';
|
|
12
|
+
export { renderTabStrip } from './tab-strip';
|
|
13
|
+
export { ListTab, type ListTabCounts, type ListTabFooterState, type ListTabOptions } from './tabs/list-tab';
|
|
14
|
+
export {
|
|
15
|
+
BODY_INDENT,
|
|
16
|
+
calculateViewport,
|
|
17
|
+
DEFAULT_TERMINAL_ROWS,
|
|
18
|
+
fitLine,
|
|
19
|
+
fitToTerminalHeight,
|
|
20
|
+
hintRow,
|
|
21
|
+
normalizeTerminalRows,
|
|
22
|
+
padLine,
|
|
23
|
+
STEP_KEY_HINT,
|
|
24
|
+
singleLine,
|
|
25
|
+
spreadLine,
|
|
26
|
+
type Viewport,
|
|
27
|
+
wrapDescriptionLines,
|
|
28
|
+
} from './text';
|
|
29
|
+
export type { Hint, ModalLayer, ModalTab, ModalTabContext, NavigationAction, NavigationResult, NavigationScheme } from './types';
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/** Forked from dimk90/pi-context-view at f6f007b867212bcf81a61519c8e40ce209cdd608 (MIT). */
|
|
2
|
+
/**
|
|
3
|
+
* Selection and scroll-window state over fixed rows, plus scroll-only preview
|
|
4
|
+
* state. Pure logic — no pi or TUI access.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Options controlling list selection behavior. */
|
|
8
|
+
export interface ListNavigatorOptions {
|
|
9
|
+
/** Wrap around list boundaries instead of clamping (picker convention). */
|
|
10
|
+
wrap?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Selection and scroll-window state over fixed rows. A trailing summary can
|
|
15
|
+
* participate in scrolling without being included in selection navigation.
|
|
16
|
+
* Selection clamps at boundaries unless `wrap` is enabled.
|
|
17
|
+
*/
|
|
18
|
+
export class ListNavigator {
|
|
19
|
+
private rowCount: number;
|
|
20
|
+
private selectableRowCount: number;
|
|
21
|
+
private visibleCount: number;
|
|
22
|
+
private readonly wrap: boolean;
|
|
23
|
+
private selectedIndex = 0;
|
|
24
|
+
private scrollOffset = 0;
|
|
25
|
+
|
|
26
|
+
public constructor(rowCount: number, visibleCount: number, selectableRowCount = rowCount, options: ListNavigatorOptions = {}) {
|
|
27
|
+
this.rowCount = Math.max(0, rowCount);
|
|
28
|
+
this.selectableRowCount = Math.min(this.rowCount, Math.max(0, selectableRowCount));
|
|
29
|
+
this.visibleCount = Math.max(1, visibleCount);
|
|
30
|
+
this.wrap = options.wrap === true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get selected(): number {
|
|
34
|
+
return this.selectedIndex;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public get selectedOrdinal(): number {
|
|
38
|
+
return this.selectedIndex;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public get selectableCount(): number {
|
|
42
|
+
return this.selectableRowCount;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public get offset(): number {
|
|
46
|
+
return this.scrollOffset;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public get windowSize(): number {
|
|
50
|
+
return Math.min(this.visibleCount, this.rowCount);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One-based final row currently visible, suitable for a scroll counter. */
|
|
54
|
+
public get visibleEnd(): number {
|
|
55
|
+
return Math.min(this.rowCount, this.scrollOffset + this.windowSize);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public get hasOverflow(): boolean {
|
|
59
|
+
return this.rowCount > this.visibleCount;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public setVisibleCount(count: number): void {
|
|
63
|
+
this.visibleCount = Math.max(1, count);
|
|
64
|
+
this.ensureVisible();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Replace the row extent, clamping selection and scroll into range. */
|
|
68
|
+
public setRowCount(rowCount: number, selectableRowCount = rowCount): void {
|
|
69
|
+
this.rowCount = Math.max(0, rowCount);
|
|
70
|
+
this.selectableRowCount = Math.min(this.rowCount, Math.max(0, selectableRowCount));
|
|
71
|
+
this.selectedIndex = Math.min(Math.max(0, this.selectableRowCount - 1), this.selectedIndex);
|
|
72
|
+
this.ensureVisible();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public moveBy(delta: number): boolean {
|
|
76
|
+
if (this.wrap && this.selectableRowCount > 0) {
|
|
77
|
+
const count = this.selectableRowCount;
|
|
78
|
+
const next = (((this.selectedIndex + delta) % count) + count) % count;
|
|
79
|
+
return this.moveTo(next);
|
|
80
|
+
}
|
|
81
|
+
return this.moveTo(this.selectedIndex + delta);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public moveTo(index: number): boolean {
|
|
85
|
+
if (this.selectableRowCount === 0) return false;
|
|
86
|
+
const next = Math.min(this.selectableRowCount - 1, Math.max(0, index));
|
|
87
|
+
if (next === this.selectedIndex) return false;
|
|
88
|
+
this.selectedIndex = next;
|
|
89
|
+
this.ensureVisible();
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public page(direction: -1 | 1): boolean {
|
|
94
|
+
return this.moveBy(direction * Math.max(1, this.visibleCount - 1));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private ensureVisible(): void {
|
|
98
|
+
const maxOffset = Math.max(0, this.rowCount - this.visibleCount);
|
|
99
|
+
if (this.selectedIndex < this.scrollOffset) {
|
|
100
|
+
this.scrollOffset = this.selectedIndex;
|
|
101
|
+
} else if (this.selectedIndex >= this.scrollOffset + this.visibleCount) {
|
|
102
|
+
this.scrollOffset = this.selectedIndex - this.visibleCount + 1;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const trailingRows = this.rowCount - this.selectedIndex - 1;
|
|
106
|
+
if (this.selectedIndex === this.selectableRowCount - 1 && trailingRows < this.visibleCount) {
|
|
107
|
+
this.scrollOffset = maxOffset;
|
|
108
|
+
}
|
|
109
|
+
this.scrollOffset = Math.min(maxOffset, Math.max(0, this.scrollOffset));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Scroll-only window over wrapped preview lines. Extent is re-declared each
|
|
115
|
+
* render (wrapping depends on width); the offset is clamped to stay valid.
|
|
116
|
+
*/
|
|
117
|
+
export class PreviewScroller {
|
|
118
|
+
private lineCount = 0;
|
|
119
|
+
private visibleCount = 1;
|
|
120
|
+
private offsetValue = 0;
|
|
121
|
+
|
|
122
|
+
public get offset(): number {
|
|
123
|
+
return this.offsetValue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public get windowSize(): number {
|
|
127
|
+
return Math.min(this.visibleCount, this.lineCount);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One-based final line currently visible, suitable for a progress counter. */
|
|
131
|
+
public get visibleEnd(): number {
|
|
132
|
+
return Math.min(this.lineCount, this.offsetValue + this.windowSize);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public get hasOverflow(): boolean {
|
|
136
|
+
return this.lineCount > this.visibleCount;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public get maxOffset(): number {
|
|
140
|
+
return Math.max(0, this.lineCount - this.visibleCount);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public setExtent(lineCount: number, visibleCount: number): void {
|
|
144
|
+
this.lineCount = Math.max(0, lineCount);
|
|
145
|
+
this.visibleCount = Math.max(1, visibleCount);
|
|
146
|
+
this.offsetValue = Math.min(this.maxOffset, this.offsetValue);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public scrollBy(delta: number): boolean {
|
|
150
|
+
return this.scrollTo(this.offsetValue + delta);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public scrollTo(offset: number): boolean {
|
|
154
|
+
const next = Math.min(this.maxOffset, Math.max(0, offset));
|
|
155
|
+
if (next === this.offsetValue) return false;
|
|
156
|
+
this.offsetValue = next;
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public page(direction: -1 | 1): boolean {
|
|
161
|
+
return this.scrollBy(direction * Math.max(1, this.visibleCount - 1));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public reset(): void {
|
|
165
|
+
this.offsetValue = 0;
|
|
166
|
+
}
|
|
167
|
+
}
|