@ftrdotdev/pi-tui 0.7.0 → 0.9.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 +225 -0
- package/README.md +5 -0
- package/package.json +18 -15
- package/src/activity-service.test.ts +97 -0
- package/src/activity.test.ts +1 -1
- package/src/activity.ts +38 -0
- package/src/chrome/budget.test.ts +345 -0
- package/src/chrome/budget.ts +220 -0
- package/src/chrome/declarations.ts +303 -0
- package/src/chrome/facts.test.ts +165 -0
- package/src/chrome/facts.ts +267 -0
- package/src/chrome/frame.ts +45 -0
- package/src/chrome/laws.test.ts +231 -0
- package/src/chrome/port.test.ts +164 -0
- package/src/chrome/port.ts +67 -0
- package/src/chrome/service-provider.test.ts +383 -0
- package/src/chrome/service-provider.ts +55 -0
- package/src/commands/git-execution.test.ts +5 -2
- package/src/commands/git-execution.ts +27 -15
- package/src/commands/git-status.test.ts +47 -15
- package/src/commands/git-status.ts +83 -31
- package/src/components/bordered-box.test.ts +12 -15
- package/src/components/bordered-box.ts +11 -7
- package/src/components/define-component.ts +1 -1
- package/src/components/index.ts +7 -0
- package/src/components/row-list.test.ts +310 -0
- package/src/components/row-list.ts +272 -0
- package/src/config.test.ts +118 -3
- package/src/config.ts +50 -42
- package/src/frame/box-frame.test.ts +53 -7
- package/src/harness-policy.test.ts +47 -0
- package/src/icons.test.ts +19 -0
- package/src/icons.ts +50 -31
- package/src/index.test.ts +135 -0
- package/src/index.ts +118 -119
- package/src/layers.test.ts +94 -0
- package/src/layers.ts +92 -0
- package/src/runtime-service.test.ts +218 -0
- package/src/runtime-service.ts +103 -0
- package/src/runtime.test.ts +165 -66
- package/src/runtime.ts +127 -93
- package/src/session-lifecycle-service.test.ts +95 -0
- package/src/session-lifecycle.test.ts +77 -0
- package/src/session-lifecycle.ts +21 -0
- package/src/state.test.ts +132 -3
- package/src/state.ts +43 -13
- package/src/telemetry-service.test.ts +250 -0
- package/src/telemetry.test.ts +322 -0
- package/src/telemetry.ts +130 -23
- package/src/ui/editor-tint.test.ts +58 -45
- package/src/ui/editor-tint.ts +1 -1
- package/src/ui/editor.ts +117 -53
- package/src/ui/footer.test.ts +349 -120
- package/src/ui/footer.ts +147 -124
- package/src/ui/header.test.ts +203 -223
- package/src/ui/header.ts +32 -96
- package/src/ui/preview.test.ts +73 -70
- package/src/ui/preview.ts +48 -33
- package/src/ui/render-helpers.test.ts +82 -13
- package/src/ui/render-helpers.ts +110 -122
- package/src/ui/settings-command.test.ts +70 -11
- package/src/ui/settings-command.ts +30 -45
- package/src/ui/theme-color.test.ts +4 -3
- package/src/usage-totals-service.test.ts +230 -0
- package/src/usage-totals-service.ts +59 -0
- package/src/utils.test.ts +46 -50
- package/src/utils.ts +41 -239
- package/src/vim/editor-host.test.ts +201 -75
- package/src/vim/editor-state.ts +2 -2
- package/src/vim/mode-indicator.test.ts +6 -5
- package/src/vim/mode-indicator.ts +13 -9
- package/src/vim/selection-render.ts +8 -4
- package/src/vim/text-model.test.ts +52 -14
- package/src/vim/text-model.ts +1 -1
- package/src/vim/vim-router.test.ts +3 -0
- package/src/vim/vim-router.ts +1 -1
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
import { VimRouter } from "../vim/vim-router.ts";
|
|
6
|
+
import { makeRowList, SELECTION_MARKER, type SelectableRow } from "./row-list.ts";
|
|
7
|
+
|
|
8
|
+
const plainTheme = {
|
|
9
|
+
fg: (_color: string, text: string): string => text,
|
|
10
|
+
bold: (text: string): string => text,
|
|
11
|
+
style: (text: string): string => text,
|
|
12
|
+
} as unknown as Theme;
|
|
13
|
+
|
|
14
|
+
interface StyleCall {
|
|
15
|
+
readonly text: string;
|
|
16
|
+
readonly options?: { readonly fg?: string; readonly bg?: string; readonly bold?: boolean };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Records every style call so a test can prove styling runs per render. */
|
|
20
|
+
const recordingTheme = (): { readonly theme: Theme; readonly calls: StyleCall[] } => {
|
|
21
|
+
const calls: StyleCall[] = [];
|
|
22
|
+
return {
|
|
23
|
+
calls,
|
|
24
|
+
theme: {
|
|
25
|
+
fg: (_color: string, text: string): string => text,
|
|
26
|
+
bold: (text: string): string => text,
|
|
27
|
+
style: (text: string, options?: StyleCall["options"]): string => {
|
|
28
|
+
calls.push({ text, ...(options === undefined ? {} : { options }) });
|
|
29
|
+
return text;
|
|
30
|
+
},
|
|
31
|
+
} as unknown as Theme,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const vimRouter = (enabled: boolean): VimRouter["Service"] =>
|
|
36
|
+
Effect.service(VimRouter).pipe(Effect.provide(VimRouter.make(() => enabled)), Effect.runSync);
|
|
37
|
+
|
|
38
|
+
const rows = (labels: readonly string[]): SelectableRow[] =>
|
|
39
|
+
labels.map((label) => ({ id: label, lines: [label] }));
|
|
40
|
+
|
|
41
|
+
it("renders one line per row in order with the house cursor on the selected row", () => {
|
|
42
|
+
const { theme, calls } = recordingTheme();
|
|
43
|
+
const list = makeRowList({ rows: () => rows(["r1", "r2", "r3"]), theme });
|
|
44
|
+
const lines = list.render(40);
|
|
45
|
+
assert.deepStrictEqual(lines, ["→ r1", " r2", " r3"]);
|
|
46
|
+
assert.ok(lines[0]?.startsWith(SELECTION_MARKER));
|
|
47
|
+
assert.ok(!(lines[1]?.startsWith(SELECTION_MARKER) ?? false));
|
|
48
|
+
assert.ok(calls.some((call) => call.options?.bg === "selectedBg"));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("a multi-line row keeps the cursor on its first line and rules its detail span", () => {
|
|
52
|
+
const list = makeRowList({
|
|
53
|
+
rows: () => [{ id: "r1", lines: ["r1", "detail one", "detail two"] }],
|
|
54
|
+
theme: plainTheme,
|
|
55
|
+
});
|
|
56
|
+
assert.deepStrictEqual(list.render(40), ["→ r1", " ├ detail one", " └ detail two"]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("arrow keys always move and stop at the ends", () => {
|
|
60
|
+
const list = makeRowList({ rows: () => rows(["r1", "r2", "r3"]), theme: plainTheme });
|
|
61
|
+
assert.strictEqual(list.selectedId(), "r1");
|
|
62
|
+
list.handleInput("\x1b[B");
|
|
63
|
+
assert.strictEqual(list.selectedId(), "r2");
|
|
64
|
+
list.handleInput("\x1b[B");
|
|
65
|
+
assert.strictEqual(list.selectedId(), "r3");
|
|
66
|
+
list.handleInput("\x1b[B");
|
|
67
|
+
assert.strictEqual(list.selectedId(), "r3");
|
|
68
|
+
list.handleInput("\x1b[A");
|
|
69
|
+
list.handleInput("\x1b[A");
|
|
70
|
+
assert.strictEqual(list.selectedId(), "r1");
|
|
71
|
+
list.handleInput("\x1b[A");
|
|
72
|
+
assert.strictEqual(list.selectedId(), "r1");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("vim keys move only when the router is enabled", () => {
|
|
76
|
+
const enabled = makeRowList({
|
|
77
|
+
rows: () => rows(["r1", "r2", "r3"]),
|
|
78
|
+
theme: plainTheme,
|
|
79
|
+
router: vimRouter(true),
|
|
80
|
+
});
|
|
81
|
+
const disabled = makeRowList({
|
|
82
|
+
rows: () => rows(["r1", "r2", "r3"]),
|
|
83
|
+
theme: plainTheme,
|
|
84
|
+
router: vimRouter(false),
|
|
85
|
+
});
|
|
86
|
+
enabled.handleInput("j");
|
|
87
|
+
disabled.handleInput("j");
|
|
88
|
+
assert.strictEqual(enabled.selectedId(), "r2");
|
|
89
|
+
assert.strictEqual(disabled.selectedId(), "r1");
|
|
90
|
+
enabled.handleInput("\x1b[B");
|
|
91
|
+
disabled.handleInput("\x1b[B");
|
|
92
|
+
assert.strictEqual(enabled.selectedId(), "r3");
|
|
93
|
+
assert.strictEqual(disabled.selectedId(), "r2");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("enter reports activation of the selected row by identity", () => {
|
|
97
|
+
const activated: string[] = [];
|
|
98
|
+
const list = makeRowList({
|
|
99
|
+
rows: () => rows(["r1", "r2", "r3"]),
|
|
100
|
+
theme: plainTheme,
|
|
101
|
+
onActivate: (id) => activated.push(id),
|
|
102
|
+
});
|
|
103
|
+
list.render(40);
|
|
104
|
+
list.handleInput("\x1b[B");
|
|
105
|
+
list.handleInput("\r");
|
|
106
|
+
assert.deepStrictEqual(activated, ["r2"]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("selection survives a replaced row set and falls back to the nearest survivor", () => {
|
|
110
|
+
let current = rows(["r1", "r2", "r3"]);
|
|
111
|
+
const changes: (string | undefined)[] = [];
|
|
112
|
+
const list = makeRowList({
|
|
113
|
+
rows: () => current,
|
|
114
|
+
theme: plainTheme,
|
|
115
|
+
onSelectionChange: (id) => changes.push(id),
|
|
116
|
+
});
|
|
117
|
+
list.render(40);
|
|
118
|
+
list.select("r2");
|
|
119
|
+
current = rows(["r1", "r3"]);
|
|
120
|
+
const lines = list.render(40);
|
|
121
|
+
assert.strictEqual(list.selectedId(), "r3");
|
|
122
|
+
assert.strictEqual(changes.filter((id) => id === "r3").length, 1);
|
|
123
|
+
assert.ok(lines.some((line) => line.startsWith(SELECTION_MARKER) && line.includes("r3")));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("an empty row list renders nothing and ignores input", () => {
|
|
127
|
+
let activated = 0;
|
|
128
|
+
const list = makeRowList({
|
|
129
|
+
rows: () => [],
|
|
130
|
+
theme: plainTheme,
|
|
131
|
+
onActivate: () => {
|
|
132
|
+
activated += 1;
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
assert.deepStrictEqual(list.render(40), []);
|
|
136
|
+
list.handleInput("\r");
|
|
137
|
+
list.handleInput("\x1b[B");
|
|
138
|
+
assert.strictEqual(activated, 0);
|
|
139
|
+
assert.strictEqual(list.selectedId(), undefined);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("a section header renders above the first row of its group only", () => {
|
|
143
|
+
const list = makeRowList({
|
|
144
|
+
rows: () => [
|
|
145
|
+
{ id: "r1", header: "Runs", lines: ["r1"] },
|
|
146
|
+
{ id: "r2", header: "Runs", lines: ["r2"] },
|
|
147
|
+
{ id: "r3", header: "Gates", lines: ["r3"] },
|
|
148
|
+
],
|
|
149
|
+
theme: plainTheme,
|
|
150
|
+
});
|
|
151
|
+
const lines = list.render(40);
|
|
152
|
+
assert.deepStrictEqual(lines, ["Runs", "→ r1", " r2", "Gates", " r3"]);
|
|
153
|
+
list.handleInput("\x1b[B");
|
|
154
|
+
list.handleInput("\x1b[B");
|
|
155
|
+
assert.strictEqual(list.selectedId(), "r3");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("the footer line renders below the rows", () => {
|
|
159
|
+
const list = makeRowList({
|
|
160
|
+
rows: () => rows(["r1"]),
|
|
161
|
+
theme: plainTheme,
|
|
162
|
+
footer: () => "esc close",
|
|
163
|
+
});
|
|
164
|
+
const lines = list.render(40);
|
|
165
|
+
assert.strictEqual(lines.at(-1), "esc close");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("an indented row keeps its text under its parent and the cursor at the left edge", () => {
|
|
169
|
+
const list = makeRowList({
|
|
170
|
+
rows: () => [
|
|
171
|
+
{ id: "parent", lines: ["parent"] },
|
|
172
|
+
{ id: "child", indent: 1, lines: ["child"] },
|
|
173
|
+
{ id: "grandchild", indent: 2, lines: ["grandchild"] },
|
|
174
|
+
],
|
|
175
|
+
theme: plainTheme,
|
|
176
|
+
});
|
|
177
|
+
assert.deepStrictEqual(list.render(40), ["→ parent", " child", " grandchild"]);
|
|
178
|
+
list.select("child");
|
|
179
|
+
assert.deepStrictEqual(list.render(40), [" parent", "→ child", " grandchild"]);
|
|
180
|
+
list.select("grandchild");
|
|
181
|
+
assert.deepStrictEqual(list.render(40), [" parent", " child", "→ grandchild"]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("a content change under stable identities renders fresh without invalidate", () => {
|
|
185
|
+
let content = "first";
|
|
186
|
+
const list = makeRowList({
|
|
187
|
+
rows: () => [{ id: "r1", lines: [content] }],
|
|
188
|
+
theme: plainTheme,
|
|
189
|
+
});
|
|
190
|
+
assert.ok(list.render(40)[0]?.includes("first"));
|
|
191
|
+
content = "second";
|
|
192
|
+
const lines = list.render(40);
|
|
193
|
+
assert.ok(lines[0]?.includes("second"), lines[0]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("a group span keeps its children inside the window when the parent sits at the bottom", () => {
|
|
197
|
+
const rows: SelectableRow[] = Array.from({ length: 10 }, (_, index) => ({
|
|
198
|
+
id: `filler ${index}`,
|
|
199
|
+
lines: [`filler ${index}`],
|
|
200
|
+
}));
|
|
201
|
+
rows.push(
|
|
202
|
+
{ id: "parent", lines: ["parent"] },
|
|
203
|
+
{ id: "child 0", follows: "parent", indent: 1, lines: ["child 0"] },
|
|
204
|
+
{ id: "child 1", follows: "parent", indent: 1, lines: ["child 1"] },
|
|
205
|
+
);
|
|
206
|
+
const list = makeRowList({ rows: () => rows, theme: plainTheme, maxLines: 4 });
|
|
207
|
+
list.select("parent");
|
|
208
|
+
const lines = list.render(40);
|
|
209
|
+
assert.strictEqual(lines.length, 4);
|
|
210
|
+
assert.ok(
|
|
211
|
+
lines.some((line) => line.includes("→ parent")),
|
|
212
|
+
lines.join("\n"),
|
|
213
|
+
);
|
|
214
|
+
assert.ok(
|
|
215
|
+
lines.some((line) => line.includes("child 0")),
|
|
216
|
+
lines.join("\n"),
|
|
217
|
+
);
|
|
218
|
+
assert.ok(
|
|
219
|
+
lines.some((line) => line.includes("child 1")),
|
|
220
|
+
lines.join("\n"),
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("the cursor stays inside maxLines and the list scrolls", () => {
|
|
225
|
+
const list = makeRowList({
|
|
226
|
+
rows: () => rows(Array.from({ length: 12 }, (_, index) => `row ${index}`)),
|
|
227
|
+
theme: plainTheme,
|
|
228
|
+
maxLines: 5,
|
|
229
|
+
});
|
|
230
|
+
list.select("row 11");
|
|
231
|
+
const lines = list.render(40);
|
|
232
|
+
assert.strictEqual(lines.length, 5);
|
|
233
|
+
assert.ok(lines.some((line) => line.startsWith(SELECTION_MARKER) && line.includes("row 11")));
|
|
234
|
+
assert.ok(!(lines[0]?.includes("row 0") ?? false), lines.join("\n"));
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("the window keeps an expanded row's detail span visible", () => {
|
|
238
|
+
const all = Array.from({ length: 8 }, (_, index) =>
|
|
239
|
+
index === 7
|
|
240
|
+
? { id: `r${index}`, lines: [`r${index}`, "detail one", "detail two", "detail three"] }
|
|
241
|
+
: { id: `r${index}`, lines: [`r${index}`] },
|
|
242
|
+
);
|
|
243
|
+
const list = makeRowList({ rows: () => all, theme: plainTheme, maxLines: 4 });
|
|
244
|
+
list.select("r7");
|
|
245
|
+
const lines = list.render(40);
|
|
246
|
+
assert.strictEqual(lines.length, 4);
|
|
247
|
+
assert.ok(lines.some((line) => line.includes("→ r7")));
|
|
248
|
+
assert.ok(lines.some((line) => line.includes("detail three")));
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("a distant parent does not push the selected child out of the window", () => {
|
|
252
|
+
const rows: SelectableRow[] = [{ id: "parent", lines: ["parent"] }];
|
|
253
|
+
for (let index = 0; index < 20; index += 1) {
|
|
254
|
+
rows.push({
|
|
255
|
+
id: `child ${index}`,
|
|
256
|
+
follows: "parent",
|
|
257
|
+
indent: 1,
|
|
258
|
+
lines: [`child ${index}`],
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const list = makeRowList({ rows: () => rows, theme: plainTheme, maxLines: 5 });
|
|
262
|
+
list.select("child 19");
|
|
263
|
+
const lines = list.render(40);
|
|
264
|
+
assert.strictEqual(lines.length, 5);
|
|
265
|
+
assert.ok(
|
|
266
|
+
lines.some((line) => line.includes("→ child 19")),
|
|
267
|
+
lines.join("\n"),
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("cached lines carry no theme colors; styling happens on every render", () => {
|
|
272
|
+
const { theme, calls } = recordingTheme();
|
|
273
|
+
const list = makeRowList({ rows: () => rows(["r1", "r2"]), theme });
|
|
274
|
+
const first = list.render(40);
|
|
275
|
+
const firstCalls = calls.length;
|
|
276
|
+
const second = list.render(40);
|
|
277
|
+
const secondCalls = calls.length - firstCalls;
|
|
278
|
+
assert.deepStrictEqual(second, first);
|
|
279
|
+
assert.ok(firstCalls > 0);
|
|
280
|
+
assert.strictEqual(secondCalls, firstCalls);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("an unchanged width and row set serves the cached render", () => {
|
|
284
|
+
let lineReads = 0;
|
|
285
|
+
const row: SelectableRow = {
|
|
286
|
+
id: "r1",
|
|
287
|
+
get lines() {
|
|
288
|
+
lineReads += 1;
|
|
289
|
+
return ["r1"];
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
const list = makeRowList({ rows: () => [row], theme: plainTheme });
|
|
293
|
+
const first = list.render(40);
|
|
294
|
+
const afterFirst = lineReads;
|
|
295
|
+
const second = list.render(40);
|
|
296
|
+
assert.deepStrictEqual(second, first);
|
|
297
|
+
// The signature reads the lines once; a cache miss would read them again.
|
|
298
|
+
assert.strictEqual(afterFirst, 2);
|
|
299
|
+
assert.strictEqual(lineReads, 3);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("nothing renders wider than the width", () => {
|
|
303
|
+
const list = makeRowList({
|
|
304
|
+
rows: () => [{ id: "r1", lines: ["a label far wider than the width"] }],
|
|
305
|
+
theme: plainTheme,
|
|
306
|
+
});
|
|
307
|
+
for (const line of list.render(20)) {
|
|
308
|
+
assert.ok(visibleWidth(line) <= 20, `${visibleWidth(line)} > 20: ${line}`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Key, matchesKey, type Component } from "@earendil-works/pi-tui";
|
|
3
|
+
import { truncateToWidth } from "../utils.ts";
|
|
4
|
+
import type { VimRouter } from "../vim/vim-router.ts";
|
|
5
|
+
import { defineComponent } from "./define-component.ts";
|
|
6
|
+
|
|
7
|
+
/** The house selection glyph: a plain arrow, not Pi's zero-width hardware-cursor marker. */
|
|
8
|
+
export const SELECTION_MARKER = "→ ";
|
|
9
|
+
|
|
10
|
+
export interface SelectableRow {
|
|
11
|
+
/** Stable identity: selection is held by this value, never by index. */
|
|
12
|
+
readonly id: string;
|
|
13
|
+
/** Rendered above the row when it differs from the previous row's header. */
|
|
14
|
+
readonly header?: string;
|
|
15
|
+
/** Extra two-space levels before the row text; selection stays at the left edge. */
|
|
16
|
+
readonly indent?: number;
|
|
17
|
+
/** The row whose expansion owns this row; a group span stays visible with its owner. */
|
|
18
|
+
readonly follows?: string;
|
|
19
|
+
readonly lines: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RowListOptions {
|
|
23
|
+
readonly rows: () => readonly SelectableRow[];
|
|
24
|
+
readonly theme: Theme;
|
|
25
|
+
/** Optional: the router self-gates on the shared vim toggle; arrows work regardless. */
|
|
26
|
+
readonly router?: VimRouter["Service"];
|
|
27
|
+
readonly footer?: () => string | undefined;
|
|
28
|
+
/** Visible row lines; the list scrolls to keep the cursor inside. Defaults to no bound. */
|
|
29
|
+
readonly maxLines?: number;
|
|
30
|
+
readonly onSelectionChange?: (id: string | undefined) => void;
|
|
31
|
+
readonly onActivate?: (id: string) => void;
|
|
32
|
+
readonly requestRender?: () => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RowList {
|
|
36
|
+
readonly render: (width: number) => string[];
|
|
37
|
+
readonly invalidate: () => void;
|
|
38
|
+
readonly handleInput: (data: string) => void;
|
|
39
|
+
readonly selectedId: () => string | undefined;
|
|
40
|
+
readonly select: (id: string) => void;
|
|
41
|
+
readonly component: Component;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface Entry {
|
|
45
|
+
readonly header: boolean;
|
|
46
|
+
readonly id?: string;
|
|
47
|
+
readonly text: string;
|
|
48
|
+
readonly indent: number;
|
|
49
|
+
/** The row whose expansion owns this row; `undefined` for a top-level row. */
|
|
50
|
+
readonly follows: string | undefined;
|
|
51
|
+
readonly selected: boolean;
|
|
52
|
+
/** A detail line under a multi-line row: indented and closed by a rule, never a cursor. */
|
|
53
|
+
readonly continuation: boolean;
|
|
54
|
+
readonly first: boolean;
|
|
55
|
+
readonly last: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const buildEntries = (rows: readonly SelectableRow[], selectedId: string | undefined): Entry[] => {
|
|
59
|
+
const entries: Entry[] = [];
|
|
60
|
+
let previousHeader: string | undefined;
|
|
61
|
+
for (const row of rows) {
|
|
62
|
+
if (row.header !== undefined && row.header !== previousHeader) {
|
|
63
|
+
entries.push({
|
|
64
|
+
header: true,
|
|
65
|
+
text: row.header,
|
|
66
|
+
indent: 0,
|
|
67
|
+
follows: undefined,
|
|
68
|
+
selected: false,
|
|
69
|
+
continuation: false,
|
|
70
|
+
first: false,
|
|
71
|
+
last: false,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
previousHeader = row.header;
|
|
75
|
+
const lines = row.lines;
|
|
76
|
+
for (const [index, line] of lines.entries()) {
|
|
77
|
+
entries.push({
|
|
78
|
+
header: false,
|
|
79
|
+
id: row.id,
|
|
80
|
+
text: line,
|
|
81
|
+
indent: row.indent ?? 0,
|
|
82
|
+
follows: row.follows,
|
|
83
|
+
selected: row.id === selectedId && index === 0,
|
|
84
|
+
continuation: index > 0,
|
|
85
|
+
first: index === 1,
|
|
86
|
+
last: index === lines.length - 1,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return entries;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** The window that keeps the selected row, its detail span and its open group inside `maxLines`. */
|
|
94
|
+
const windowOf = (entries: readonly Entry[], maxLines: number | undefined): readonly Entry[] => {
|
|
95
|
+
if (maxLines === undefined || entries.length <= maxLines) return entries;
|
|
96
|
+
const selectedIndex = entries.findIndex((entry) => entry.selected);
|
|
97
|
+
const selectedLine = selectedIndex === -1 ? 0 : selectedIndex;
|
|
98
|
+
const selectedEntry = entries[selectedLine];
|
|
99
|
+
let first = selectedLine;
|
|
100
|
+
let anchor = selectedLine;
|
|
101
|
+
while (entries[anchor + 1]?.continuation ?? false) anchor += 1;
|
|
102
|
+
if (selectedEntry !== undefined) {
|
|
103
|
+
while (entries[anchor + 1]?.follows === selectedEntry.id) anchor += 1;
|
|
104
|
+
}
|
|
105
|
+
if (selectedEntry?.follows !== undefined) {
|
|
106
|
+
const owner = entries.findIndex((entry) => entry.id === selectedEntry.follows);
|
|
107
|
+
// A distant owner must never push the selected child out of the window.
|
|
108
|
+
if (owner !== -1 && anchor - owner + 1 <= maxLines) first = Math.min(first, owner);
|
|
109
|
+
}
|
|
110
|
+
const start = Math.max(
|
|
111
|
+
0,
|
|
112
|
+
Math.min(entries.length - maxLines, Math.min(first, anchor - maxLines + 1)),
|
|
113
|
+
);
|
|
114
|
+
return entries.slice(start, start + maxLines);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A generic selectable list: identity-held selection, arrow and router-decoded
|
|
119
|
+
* movement, section headers, an optional footer, the house selection style,
|
|
120
|
+
* a bounded window that scrolls with the cursor, and a render cache keyed by
|
|
121
|
+
* width plus content. It imports no domain type and knows nothing about runs,
|
|
122
|
+
* lanes or gates.
|
|
123
|
+
*/
|
|
124
|
+
export const makeRowList = (options: RowListOptions): RowList => {
|
|
125
|
+
const { theme } = options;
|
|
126
|
+
let selected: string | undefined;
|
|
127
|
+
let lastIds: readonly string[] = [];
|
|
128
|
+
let cachedSignature: string | undefined;
|
|
129
|
+
let cachedWindow: readonly Entry[] = [];
|
|
130
|
+
|
|
131
|
+
const notify = (id: string | undefined): void => {
|
|
132
|
+
options.onSelectionChange?.(id);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** Hold selection by id; a replaced row set falls back to the nearest index. */
|
|
136
|
+
const syncSelection = (rows: readonly SelectableRow[]): void => {
|
|
137
|
+
const ids = rows.map((row) => row.id);
|
|
138
|
+
if (ids.length === 0) {
|
|
139
|
+
lastIds = [];
|
|
140
|
+
if (selected !== undefined) {
|
|
141
|
+
selected = undefined;
|
|
142
|
+
notify(undefined);
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (selected !== undefined && ids.includes(selected)) {
|
|
147
|
+
lastIds = ids;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const previousIndex = selected === undefined ? 0 : Math.max(0, lastIds.indexOf(selected));
|
|
151
|
+
const next = ids[Math.min(previousIndex, ids.length - 1)] as string;
|
|
152
|
+
lastIds = ids;
|
|
153
|
+
if (next !== selected) {
|
|
154
|
+
selected = next;
|
|
155
|
+
notify(next);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const move = (delta: 1 | -1): void => {
|
|
160
|
+
const rows = options.rows();
|
|
161
|
+
syncSelection(rows);
|
|
162
|
+
const ids = rows.map((row) => row.id);
|
|
163
|
+
if (ids.length === 0) return;
|
|
164
|
+
const index = selected === undefined ? 0 : ids.indexOf(selected);
|
|
165
|
+
const next = ids[Math.max(0, Math.min(ids.length - 1, index + delta))] as string;
|
|
166
|
+
if (next !== selected) {
|
|
167
|
+
selected = next;
|
|
168
|
+
notify(next);
|
|
169
|
+
options.requestRender?.();
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const styleEntry = (entry: Entry, width: number): string => {
|
|
174
|
+
if (entry.header) {
|
|
175
|
+
return theme.style(truncateToWidth(entry.text, width), { fg: "muted", bold: true });
|
|
176
|
+
}
|
|
177
|
+
if (entry.continuation) {
|
|
178
|
+
const gutter = " ".repeat(entry.indent + 1);
|
|
179
|
+
const prefix = entry.last ? `${gutter}└ ` : entry.first ? `${gutter}├ ` : `${gutter}│ `;
|
|
180
|
+
return theme.fg("dim", truncateToWidth(`${prefix}${entry.text}`, width));
|
|
181
|
+
}
|
|
182
|
+
if (!entry.selected) {
|
|
183
|
+
return truncateToWidth(`${" ".repeat(entry.indent + 1)}${entry.text}`, width);
|
|
184
|
+
}
|
|
185
|
+
const plain = truncateToWidth(
|
|
186
|
+
`${SELECTION_MARKER}${" ".repeat(entry.indent)}${entry.text}`,
|
|
187
|
+
width,
|
|
188
|
+
);
|
|
189
|
+
const marked = plain.startsWith(SELECTION_MARKER)
|
|
190
|
+
? `${theme.fg("accent", SELECTION_MARKER)}${plain.slice(SELECTION_MARKER.length)}`
|
|
191
|
+
: plain;
|
|
192
|
+
return theme.style(marked, { bg: "selectedBg" });
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const render = (width: number): string[] => {
|
|
196
|
+
const rows = options.rows();
|
|
197
|
+
syncSelection(rows);
|
|
198
|
+
if (rows.length === 0) return [];
|
|
199
|
+
|
|
200
|
+
const footer = options.footer?.();
|
|
201
|
+
const signature = [
|
|
202
|
+
String(width),
|
|
203
|
+
selected ?? "",
|
|
204
|
+
footer ?? "",
|
|
205
|
+
String(options.maxLines ?? ""),
|
|
206
|
+
...rows.map(
|
|
207
|
+
(row) =>
|
|
208
|
+
`${row.id}\u0001${String(row.indent ?? 0)}\u0001${row.follows ?? ""}\u0001${row.header ?? ""}\u0001${row.lines.join("\u0000")}`,
|
|
209
|
+
),
|
|
210
|
+
].join("\u0002");
|
|
211
|
+
if (signature !== cachedSignature) {
|
|
212
|
+
cachedWindow = windowOf(buildEntries(rows, selected), options.maxLines);
|
|
213
|
+
cachedSignature = signature;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const lines = cachedWindow.map((entry) => styleEntry(entry, width));
|
|
217
|
+
if (footer !== undefined) lines.push(theme.fg("dim", truncateToWidth(footer, width)));
|
|
218
|
+
return lines;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const handleInput = (data: string): void => {
|
|
222
|
+
const rows = options.rows();
|
|
223
|
+
if (rows.length === 0) return;
|
|
224
|
+
syncSelection(rows);
|
|
225
|
+
if (matchesKey(data, Key.up)) {
|
|
226
|
+
move(-1);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (matchesKey(data, Key.down)) {
|
|
230
|
+
move(1);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const intent = options.router?.decodeNavigation(data);
|
|
234
|
+
if (intent?.kind === "move" && intent.dir === "up") {
|
|
235
|
+
move(-1);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (intent?.kind === "move" && intent.dir === "down") {
|
|
239
|
+
move(1);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (matchesKey(data, Key.enter) && selected !== undefined) {
|
|
243
|
+
options.onActivate?.(selected);
|
|
244
|
+
options.requestRender?.();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const invalidate = (): void => {
|
|
249
|
+
cachedSignature = undefined;
|
|
250
|
+
cachedWindow = [];
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const component = defineComponent({ render, invalidate, handleInput });
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
render,
|
|
257
|
+
invalidate,
|
|
258
|
+
handleInput,
|
|
259
|
+
selectedId: () => {
|
|
260
|
+
syncSelection(options.rows());
|
|
261
|
+
return selected;
|
|
262
|
+
},
|
|
263
|
+
select: (id: string) => {
|
|
264
|
+
if (id === selected) return;
|
|
265
|
+
if (!options.rows().some((row) => row.id === id)) return;
|
|
266
|
+
selected = id;
|
|
267
|
+
notify(id);
|
|
268
|
+
options.requestRender?.();
|
|
269
|
+
},
|
|
270
|
+
component,
|
|
271
|
+
};
|
|
272
|
+
};
|