@ftrdotdev/pi-tui 0.2.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/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +80 -0
- package/src/autocomplete-contract.test.ts +126 -0
- package/src/autocomplete-contract.ts +91 -0
- package/src/commands/git-execution.test.ts +69 -0
- package/src/commands/git-execution.ts +198 -0
- package/src/commands/git-status.test.ts +379 -0
- package/src/commands/git-status.ts +320 -0
- package/src/components/bordered-box.test.ts +136 -0
- package/src/components/bordered-box.ts +106 -0
- package/src/components/define-component.ts +33 -0
- package/src/components/index.ts +35 -0
- package/src/config.test.ts +469 -0
- package/src/config.ts +307 -0
- package/src/frame/box-frame.test.ts +44 -0
- package/src/frame/box-frame.ts +160 -0
- package/src/icons.ts +203 -0
- package/src/index.ts +359 -0
- package/src/runtime.ts +256 -0
- package/src/session-lifecycle.ts +41 -0
- package/src/state.test.ts +7 -0
- package/src/state.ts +90 -0
- package/src/telemetry.ts +301 -0
- package/src/ui/editor-tint.test.ts +70 -0
- package/src/ui/editor-tint.ts +78 -0
- package/src/ui/editor.ts +403 -0
- package/src/ui/footer.test.ts +350 -0
- package/src/ui/footer.ts +225 -0
- package/src/ui/header.test.ts +260 -0
- package/src/ui/header.ts +165 -0
- package/src/ui/preview.test.ts +639 -0
- package/src/ui/preview.ts +374 -0
- package/src/ui/render-helpers.ts +275 -0
- package/src/ui/settings-command.test.ts +218 -0
- package/src/ui/settings-command.ts +390 -0
- package/src/utils.ts +448 -0
- package/src/vim/editor-close-autocomplete.ts +26 -0
- package/src/vim/editor-host.test.ts +340 -0
- package/src/vim/editor-state.test.ts +385 -0
- package/src/vim/editor-state.ts +314 -0
- package/src/vim/mode-indicator.test.ts +27 -0
- package/src/vim/mode-indicator.ts +44 -0
- package/src/vim/selection-render.test.ts +77 -0
- package/src/vim/selection-render.ts +85 -0
- package/src/vim/text-model.test.ts +85 -0
- package/src/vim/text-model.ts +75 -0
- package/src/vim/vim-router.test.ts +50 -0
- package/src/vim/vim-router.ts +55 -0
- package/src/vim/word-motion.ts +96 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { GitStatus } from "../commands/git-status.ts";
|
|
5
|
+
import type { TuiConfig } from "../config.ts";
|
|
6
|
+
import type { FooterState } from "../state.ts";
|
|
7
|
+
import { FooterRenderService, installFooter, type FooterHooks } from "./footer.ts";
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Deterministic fixture — the footer's current pixels are frozen here BEFORE
|
|
11
|
+
// its pipeline is rewritten (Slice 3), so the refactor can be proven
|
|
12
|
+
// byte-identical. Determinism requirements:
|
|
13
|
+
// - icon mode pinned to "ascii" (env-free glyphs, icons.ts:119-123)
|
|
14
|
+
// - no active timer and no runtime (no wall-clock or subprocess dependence)
|
|
15
|
+
// - fixed session entries under a constant cache key (state.ts:24-30)
|
|
16
|
+
// - cwd outside any plausible HOME so formatCwd renders the literal path
|
|
17
|
+
// - the same identity theme instance passed as both the factory theme
|
|
18
|
+
// argument and ctx.ui.theme (the recorded identity assumption)
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
const plainTheme = {
|
|
22
|
+
fg: (_color: string, text: string) => text,
|
|
23
|
+
bg: (_color: string, text: string) => text,
|
|
24
|
+
bold: (text: string) => `*${text}*`,
|
|
25
|
+
} as unknown as Theme;
|
|
26
|
+
|
|
27
|
+
const WIDTHS = [30, 40, 60, 80, 100, 120];
|
|
28
|
+
|
|
29
|
+
const state: FooterState = {
|
|
30
|
+
git: GitStatus.make({
|
|
31
|
+
branch: "feature/refactor",
|
|
32
|
+
ahead: 2,
|
|
33
|
+
behind: 1,
|
|
34
|
+
modified: 3,
|
|
35
|
+
untracked: 1,
|
|
36
|
+
staged: 2,
|
|
37
|
+
stashed: 1,
|
|
38
|
+
conflicted: 0,
|
|
39
|
+
renamed: 0,
|
|
40
|
+
deleted: 0,
|
|
41
|
+
commit: null,
|
|
42
|
+
}),
|
|
43
|
+
runtime: null,
|
|
44
|
+
sessionStartEpoch: 1_700_000_000_000,
|
|
45
|
+
workingSince: undefined,
|
|
46
|
+
lastDoneIn: undefined,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const getState = () => state;
|
|
50
|
+
|
|
51
|
+
const config: TuiConfig = {
|
|
52
|
+
enabled: true,
|
|
53
|
+
settingsLanguage: "en",
|
|
54
|
+
vim: false,
|
|
55
|
+
icons: { mode: "ascii" },
|
|
56
|
+
footerSegments: {
|
|
57
|
+
cwd: true,
|
|
58
|
+
gitBranch: true,
|
|
59
|
+
gitStatus: true,
|
|
60
|
+
gitCommit: true,
|
|
61
|
+
runtime: true,
|
|
62
|
+
context: true,
|
|
63
|
+
tokens: true,
|
|
64
|
+
cost: true,
|
|
65
|
+
extensionStatuses: true,
|
|
66
|
+
},
|
|
67
|
+
telemetry: {
|
|
68
|
+
enabled: true,
|
|
69
|
+
tps: true,
|
|
70
|
+
ttft: true,
|
|
71
|
+
duration: true,
|
|
72
|
+
tokens: true,
|
|
73
|
+
stalls: true,
|
|
74
|
+
cost: true,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const getConfig = () => config;
|
|
78
|
+
|
|
79
|
+
const getModelMeta = () => ({
|
|
80
|
+
provider: "Anthropic",
|
|
81
|
+
model: "claude-sonnet-4-0",
|
|
82
|
+
effort: "medium",
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// One user message and two assistant messages with fixed usage values; the
|
|
86
|
+
// list (and therefore the module usage-cache key) never changes.
|
|
87
|
+
const ENTRIES = [
|
|
88
|
+
{
|
|
89
|
+
type: "message",
|
|
90
|
+
id: "u1",
|
|
91
|
+
parentId: null,
|
|
92
|
+
timestamp: "t0",
|
|
93
|
+
message: { role: "user", content: "hi" },
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
type: "message",
|
|
97
|
+
id: "a1",
|
|
98
|
+
parentId: "u1",
|
|
99
|
+
timestamp: "t1",
|
|
100
|
+
message: {
|
|
101
|
+
role: "assistant",
|
|
102
|
+
content: [],
|
|
103
|
+
usage: {
|
|
104
|
+
input: 1200,
|
|
105
|
+
output: 400,
|
|
106
|
+
cacheRead: 800,
|
|
107
|
+
cacheWrite: 100,
|
|
108
|
+
totalTokens: 2500,
|
|
109
|
+
cost: { input: 0.02, output: 0.01, cacheRead: 0.01, cacheWrite: 0.002, total: 0.042 },
|
|
110
|
+
},
|
|
111
|
+
timestamp: 1_700_000_001_000,
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: "message",
|
|
116
|
+
id: "a2",
|
|
117
|
+
parentId: "a1",
|
|
118
|
+
timestamp: "t2",
|
|
119
|
+
message: {
|
|
120
|
+
role: "assistant",
|
|
121
|
+
content: [],
|
|
122
|
+
usage: {
|
|
123
|
+
input: 1200,
|
|
124
|
+
output: 400,
|
|
125
|
+
cacheRead: 800,
|
|
126
|
+
cacheWrite: 100,
|
|
127
|
+
totalTokens: 2500,
|
|
128
|
+
cost: { input: 0.02, output: 0.01, cacheRead: 0.01, cacheWrite: 0.002, total: 0.042 },
|
|
129
|
+
},
|
|
130
|
+
timestamp: 1_700_000_002_000,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
const ctxFixture = {
|
|
136
|
+
model: { provider: "anthropic", id: "claude-sonnet-4-0", contextWindow: 200000, reasoning: true },
|
|
137
|
+
cwd: "/workspace/pi-mono",
|
|
138
|
+
getContextUsage: () => ({ contextWindow: 200000, tokens: 100000, percent: 50.0 }),
|
|
139
|
+
sessionManager: {
|
|
140
|
+
getCwd: () => "/workspace/pi-mono",
|
|
141
|
+
getEntries: () => ENTRIES,
|
|
142
|
+
},
|
|
143
|
+
theme: plainTheme,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Fake ctx.ui.setFooter captures the factory; the SAME identity theme
|
|
147
|
+
// instance is passed as both the factory theme argument and ctx.ui.theme
|
|
148
|
+
// (the recorded identity assumption).
|
|
149
|
+
const makeMountCtx = (captureFactory: (f: unknown) => void) => ({
|
|
150
|
+
model: ctxFixture.model,
|
|
151
|
+
cwd: ctxFixture.cwd,
|
|
152
|
+
getContextUsage: ctxFixture.getContextUsage,
|
|
153
|
+
sessionManager: ctxFixture.sessionManager,
|
|
154
|
+
ui: {
|
|
155
|
+
theme: plainTheme,
|
|
156
|
+
setFooter(factory: unknown) {
|
|
157
|
+
captureFactory(factory);
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const runMount = (): { component: { render(width: number): string[] } } => {
|
|
163
|
+
let capturedFactory: ((tui: unknown, theme: Theme, data: unknown) => unknown) | undefined;
|
|
164
|
+
const mountCtx = makeMountCtx((factory) => {
|
|
165
|
+
capturedFactory = factory as typeof capturedFactory;
|
|
166
|
+
});
|
|
167
|
+
const hooks: FooterHooks = {
|
|
168
|
+
setRequestRender: () => {},
|
|
169
|
+
scheduleGitRefresh: () => {},
|
|
170
|
+
};
|
|
171
|
+
const cleanup = installFooter(mountCtx as never, getState, getConfig, getModelMeta, hooks);
|
|
172
|
+
void cleanup;
|
|
173
|
+
const component = capturedFactory!(undefined, plainTheme, footerData()) as {
|
|
174
|
+
render(width: number): string[];
|
|
175
|
+
};
|
|
176
|
+
return { component };
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const footerData = () => ({
|
|
180
|
+
getGitBranch: () => null,
|
|
181
|
+
getExtensionStatuses: () =>
|
|
182
|
+
new Map([
|
|
183
|
+
["lint", "running"],
|
|
184
|
+
["build", "2 errors"],
|
|
185
|
+
]),
|
|
186
|
+
getAvailableProviderCount: () => 0,
|
|
187
|
+
onBranchChange: () => () => {},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const FOOTER_EXPECTED: Record<string, string[]> = {
|
|
191
|
+
30: [
|
|
192
|
+
"% [####----] 50.0% \u00b7 100k/200k",
|
|
193
|
+
"\u2191 2.4k | \u2193 800 | c 38.1% | \u001b[0m...\u001b[0m",
|
|
194
|
+
"& 2 errors | running",
|
|
195
|
+
],
|
|
196
|
+
40: [
|
|
197
|
+
"* \u001b[0m...\u001b[0m % [######------] 50.0% \u00b7 100k/200k",
|
|
198
|
+
"M \u001b[0m...\u001b[0m \u2191 2.4k | \u2193 800 | c 38.1% | $ 0.084",
|
|
199
|
+
"& 2 errors | running",
|
|
200
|
+
],
|
|
201
|
+
60: [
|
|
202
|
+
"* feature/refactor [!3\u001b[0m...\u001b[0m % [######------] 50.0% \u00b7 100k/200k",
|
|
203
|
+
"M \u00b7 Anthropic \u00b7 claude\u001b[0m...\u001b[0m \u2191 2.4k | \u2193 800 | c 38.1% | $ 0.084",
|
|
204
|
+
"& 2 errors | running",
|
|
205
|
+
],
|
|
206
|
+
80: [
|
|
207
|
+
"@ /\u001b[0m...\u001b[0m * feature/refactor [!3 A2 ?1 S1 ^v2/1] % [######------] 50.0% \u00b7 100k/200k",
|
|
208
|
+
"M \u00b7 Anthropic \u00b7 claude-sonnet-4-0 \u00b7 ~ medium \u2191 2.4k | \u2193 800 | c 38.1% | $ 0.084",
|
|
209
|
+
"& 2 errors | running",
|
|
210
|
+
],
|
|
211
|
+
100: [
|
|
212
|
+
"@ /workspace/pi-mono * feature/refactor [!3 A2 ?1 S1 ^v2/1] % [######------] 50.0% \u00b7 100k/200k",
|
|
213
|
+
"M \u00b7 Anthropic \u00b7 claude-sonnet-4-0 \u00b7 ~ medium \u2191 2.4k | \u2193 800 | c 38.1% | $ 0.084",
|
|
214
|
+
"& 2 errors | running",
|
|
215
|
+
],
|
|
216
|
+
120: [
|
|
217
|
+
"@ /workspace/pi-mono * feature/refactor [!3 A2 ?1 S1 ^v2/1] % [######------] 50.0% \u00b7 100k/200k",
|
|
218
|
+
"M \u00b7 Anthropic \u00b7 claude-sonnet-4-0 \u00b7 ~ medium \u2191 2.4k | \u2193 800 | c 38.1% | $ 0.084",
|
|
219
|
+
"& 2 errors | running",
|
|
220
|
+
],
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
it("the footer renders identical lines across repeated runs under the deterministic fixture", () => {
|
|
224
|
+
const { component } = runMount();
|
|
225
|
+
const first = WIDTHS.map((w) => component.render(w));
|
|
226
|
+
const mirror = WIDTHS.map((w) => component.render(w));
|
|
227
|
+
assert.deepStrictEqual(mirror, first);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("regresses the current footer output byte-for-byte at every fixed width", () => {
|
|
231
|
+
const { component } = runMount();
|
|
232
|
+
for (const w of WIDTHS) {
|
|
233
|
+
assert.deepStrictEqual(component.render(w), FOOTER_EXPECTED[String(w)]);
|
|
234
|
+
assert.deepStrictEqual(component.render(w), FOOTER_EXPECTED[String(w)]);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("installFooter registers the request-render hook and branch subscriber at mount and clears both on dispose", () => {
|
|
239
|
+
let capturedFactory: ((tui: unknown, theme: Theme, data: unknown) => unknown) | undefined;
|
|
240
|
+
const mountCtx = makeMountCtx((factory) => {
|
|
241
|
+
capturedFactory = factory as typeof capturedFactory;
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
let renders = 0;
|
|
245
|
+
let refreshCalls = 0;
|
|
246
|
+
const requestRenderFns: ((() => void) | undefined)[] = [];
|
|
247
|
+
const hooks: FooterHooks = {
|
|
248
|
+
setRequestRender: (fn) => {
|
|
249
|
+
requestRenderFns.push(fn);
|
|
250
|
+
},
|
|
251
|
+
scheduleGitRefresh: () => {
|
|
252
|
+
refreshCalls += 1;
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
const branchCbs: (() => void)[] = [];
|
|
256
|
+
let unsubCalls = 0;
|
|
257
|
+
const data = {
|
|
258
|
+
getExtensionStatuses: () => new Map(),
|
|
259
|
+
onBranchChange(cb: () => void) {
|
|
260
|
+
branchCbs.push(cb);
|
|
261
|
+
return () => {
|
|
262
|
+
unsubCalls += 1;
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const cleanup = installFooter(mountCtx as never, getState, getConfig, getModelMeta, hooks);
|
|
268
|
+
void cleanup;
|
|
269
|
+
const tui = { requestRender: () => void renders++ };
|
|
270
|
+
const component = capturedFactory!(tui, plainTheme, data) as { dispose: () => void };
|
|
271
|
+
|
|
272
|
+
// Mount side effects (footer.ts:196-200): one recorded request-render
|
|
273
|
+
// function and exactly one branch-change subscription.
|
|
274
|
+
assert.strictEqual(requestRenderFns.length, 1);
|
|
275
|
+
assert.strictEqual(typeof requestRenderFns[0], "function");
|
|
276
|
+
assert.strictEqual(branchCbs.length, 1);
|
|
277
|
+
|
|
278
|
+
// The request-render hook drives the tui re-render.
|
|
279
|
+
requestRenderFns[0]!();
|
|
280
|
+
assert.strictEqual(renders, 1);
|
|
281
|
+
|
|
282
|
+
// A branch change triggers a git refresh and a tui re-render (footer.ts:197-200).
|
|
283
|
+
branchCbs[0]!();
|
|
284
|
+
assert.strictEqual(refreshCalls, 1);
|
|
285
|
+
assert.strictEqual(renders, 2);
|
|
286
|
+
|
|
287
|
+
// dispose clears the branch subscription and the hook (footer.ts:203-207).
|
|
288
|
+
component.dispose();
|
|
289
|
+
assert.strictEqual(unsubCalls, 1);
|
|
290
|
+
assert.deepStrictEqual(requestRenderFns.slice(1), [undefined]);
|
|
291
|
+
});
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
// FooterRenderService — direct service-level tests (Slice 3)
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
// The context the service reads: same fixture, no setFooter seam involved.
|
|
297
|
+
const svcCtx = {
|
|
298
|
+
model: ctxFixture.model,
|
|
299
|
+
cwd: ctxFixture.cwd,
|
|
300
|
+
getContextUsage: ctxFixture.getContextUsage,
|
|
301
|
+
sessionManager: ctxFixture.sessionManager,
|
|
302
|
+
ui: { theme: plainTheme },
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const buildFooterService = (getConfigOverride: () => TuiConfig = getConfig) =>
|
|
306
|
+
Effect.runSync(
|
|
307
|
+
Effect.gen(function* () {
|
|
308
|
+
const svc = yield* FooterRenderService;
|
|
309
|
+
return svc;
|
|
310
|
+
}).pipe(
|
|
311
|
+
Effect.provide(
|
|
312
|
+
FooterRenderService.make(
|
|
313
|
+
svcCtx as never,
|
|
314
|
+
getState,
|
|
315
|
+
getConfigOverride,
|
|
316
|
+
getModelMeta,
|
|
317
|
+
footerData(),
|
|
318
|
+
),
|
|
319
|
+
),
|
|
320
|
+
),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
it("FooterRenderService.render reproduces the frozen golden lines at every pinned width", () => {
|
|
324
|
+
const svc = buildFooterService();
|
|
325
|
+
for (const w of WIDTHS) {
|
|
326
|
+
assert.deepStrictEqual(Effect.runSync(svc.render(w)), FOOTER_EXPECTED[String(w)]);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("FooterRenderService.render returns a single empty line for non-positive widths", () => {
|
|
331
|
+
const svc = buildFooterService();
|
|
332
|
+
assert.deepStrictEqual(Effect.runSync(svc.render(0)), [""]);
|
|
333
|
+
assert.deepStrictEqual(Effect.runSync(svc.render(-1)), [""]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("FooterRenderService.render re-invokes the live getters on every run", () => {
|
|
337
|
+
let liveConfig: TuiConfig = config;
|
|
338
|
+
const svc = buildFooterService(() => liveConfig);
|
|
339
|
+
|
|
340
|
+
const withContext = Effect.runSync(svc.render(80));
|
|
341
|
+
assert.ok(withContext[0]!.includes("%"));
|
|
342
|
+
|
|
343
|
+
liveConfig = {
|
|
344
|
+
...config,
|
|
345
|
+
footerSegments: { ...config.footerSegments, context: false },
|
|
346
|
+
};
|
|
347
|
+
const withoutContext = Effect.runSync(svc.render(80));
|
|
348
|
+
assert.ok(!withoutContext[0]!.includes("%"));
|
|
349
|
+
assert.ok(withoutContext[0]!.includes("feature/refactor"));
|
|
350
|
+
});
|
package/src/ui/footer.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionContext,
|
|
3
|
+
ReadonlyFooterDataProvider,
|
|
4
|
+
Theme,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
|
+
import { Context, Effect, Layer } from "effect";
|
|
8
|
+
import type { TuiConfig } from "../config.ts";
|
|
9
|
+
import type { IconGlyphs } from "../icons.ts";
|
|
10
|
+
import { resolveGlyphs } from "../icons.ts";
|
|
11
|
+
import {
|
|
12
|
+
alignRight,
|
|
13
|
+
effortColor,
|
|
14
|
+
fitSegmentsByPriority,
|
|
15
|
+
formatCwd,
|
|
16
|
+
providerColor,
|
|
17
|
+
truncatePath,
|
|
18
|
+
} from "../utils.ts";
|
|
19
|
+
import type { FooterState, ModelMeta, UsageTotals } from "../state.ts";
|
|
20
|
+
import { getUsageTotals } from "../state.ts";
|
|
21
|
+
import { defineComponent } from "../components/define-component.ts";
|
|
22
|
+
import {
|
|
23
|
+
renderContextBar,
|
|
24
|
+
renderExtensionStatusLines,
|
|
25
|
+
renderGitSegment,
|
|
26
|
+
renderRuntimeSegment,
|
|
27
|
+
renderStatsBlock,
|
|
28
|
+
renderTimerSegment,
|
|
29
|
+
} from "./render-helpers.ts";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Live hooks the extension injects into the footer so the widget can
|
|
33
|
+
* request re-renders and git refreshes without owning their scheduling.
|
|
34
|
+
*/
|
|
35
|
+
export interface FooterHooks {
|
|
36
|
+
setRequestRender: (fn: (() => void) | undefined) => void;
|
|
37
|
+
scheduleGitRefresh: () => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The footer render pipeline as an Effect service (house pattern, preview.ts):
|
|
42
|
+
* a `Layer.effect` factory capturing the context and the injected live
|
|
43
|
+
* getters, named synchronous total steps for each line, and a single public
|
|
44
|
+
* `render` member. Live inputs (theme, state, config, model meta, usage
|
|
45
|
+
* totals) are re-read on every run; nothing is snapshotted at service build.
|
|
46
|
+
*/
|
|
47
|
+
export class FooterRenderService extends Context.Service<
|
|
48
|
+
FooterRenderService,
|
|
49
|
+
{ readonly render: (width: number) => Effect.Effect<string[]> }
|
|
50
|
+
>()("tui/footer/FooterRenderService") {
|
|
51
|
+
static make(
|
|
52
|
+
ctx: ExtensionContext,
|
|
53
|
+
getState: () => FooterState,
|
|
54
|
+
getConfig: () => TuiConfig,
|
|
55
|
+
getModelMeta: () => ModelMeta,
|
|
56
|
+
footerData: ReadonlyFooterDataProvider,
|
|
57
|
+
): Layer.Layer<FooterRenderService> {
|
|
58
|
+
return Layer.effect(
|
|
59
|
+
FooterRenderService,
|
|
60
|
+
Effect.sync(function () {
|
|
61
|
+
// Named synchronous total step: line 1 (cwd/git/runtime/timer segments
|
|
62
|
+
// and the context bar), packed by priority and aligned to the width.
|
|
63
|
+
const renderLine1 = (
|
|
64
|
+
theme: Theme,
|
|
65
|
+
state: FooterState,
|
|
66
|
+
config: TuiConfig,
|
|
67
|
+
glyphs: IconGlyphs,
|
|
68
|
+
segments: TuiConfig["footerSegments"],
|
|
69
|
+
width: number,
|
|
70
|
+
): Effect.Effect<string> =>
|
|
71
|
+
Effect.sync(function () {
|
|
72
|
+
const leftParts: { text: string; priority: number }[] = [];
|
|
73
|
+
if (segments.cwd) {
|
|
74
|
+
const maxCwd = Math.min(30, Math.max(10, Math.floor(width * 0.4)));
|
|
75
|
+
leftParts.push({
|
|
76
|
+
text: `${theme.fg("mdLink", glyphs.cwd)} ${theme.fg("accent", truncatePath(formatCwd(ctx.sessionManager.getCwd()), maxCwd))}`,
|
|
77
|
+
priority: 0,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const gitSeg = renderGitSegment(theme, state.git, glyphs, segments);
|
|
81
|
+
if (gitSeg) leftParts.push({ text: gitSeg, priority: 3 });
|
|
82
|
+
if (segments.runtime) {
|
|
83
|
+
const runtimeSeg = renderRuntimeSegment(theme, state.runtime, config.icons.mode);
|
|
84
|
+
if (runtimeSeg) leftParts.push({ text: runtimeSeg, priority: 1 });
|
|
85
|
+
}
|
|
86
|
+
const timerSeg = renderTimerSegment(theme, state, glyphs);
|
|
87
|
+
if (timerSeg) leftParts.push({ text: timerSeg, priority: 2 });
|
|
88
|
+
|
|
89
|
+
let rightBlock = "";
|
|
90
|
+
if (segments.context) {
|
|
91
|
+
rightBlock = renderContextBar(theme, ctx, width, glyphs, config.icons.mode);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const rightW = visibleWidth(rightBlock);
|
|
95
|
+
const availLeft = Math.max(0, width - rightW - (rightBlock ? 1 : 0));
|
|
96
|
+
const fittedLeft = fitSegmentsByPriority(leftParts, availLeft, theme.fg("dim", "..."));
|
|
97
|
+
return alignRight(fittedLeft.join(" "), rightBlock, width, theme);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Named synchronous total step: line 2 (model block and stats block).
|
|
101
|
+
const renderLine2 = (
|
|
102
|
+
theme: Theme,
|
|
103
|
+
totals: UsageTotals,
|
|
104
|
+
meta: ModelMeta,
|
|
105
|
+
glyphs: IconGlyphs,
|
|
106
|
+
segments: TuiConfig["footerSegments"],
|
|
107
|
+
width: number,
|
|
108
|
+
): Effect.Effect<string> =>
|
|
109
|
+
Effect.sync(function () {
|
|
110
|
+
const modelParts: string[] = [];
|
|
111
|
+
modelParts.push(theme.fg("mdLink", glyphs.model));
|
|
112
|
+
if (meta.provider && meta.provider !== "Unknown") {
|
|
113
|
+
modelParts.push(
|
|
114
|
+
theme.fg(providerColor(ctx.model?.provider ?? "none"), meta.provider),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
modelParts.push(theme.fg("text", meta.model));
|
|
118
|
+
if (meta.effort && meta.effort !== "off") {
|
|
119
|
+
modelParts.push(
|
|
120
|
+
theme.fg(effortColor(meta.effort), `${glyphs.thinking} ${meta.effort}`),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const modelBlock = modelParts.join(theme.fg("dim", " · "));
|
|
124
|
+
|
|
125
|
+
const statsBlock = renderStatsBlock(theme, totals, glyphs, segments);
|
|
126
|
+
|
|
127
|
+
return alignRight(modelBlock, statsBlock, width, theme);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Named synchronous total step: wrapped extension status lines.
|
|
131
|
+
const renderExtensionLines = (
|
|
132
|
+
theme: Theme,
|
|
133
|
+
extensionStatuses: ReadonlyMap<string, string>,
|
|
134
|
+
glyphs: IconGlyphs,
|
|
135
|
+
width: number,
|
|
136
|
+
) =>
|
|
137
|
+
Effect.sync(function () {
|
|
138
|
+
return renderExtensionStatusLines(theme, extensionStatuses, glyphs, width);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const render = Effect.fn("FooterRenderService.render")(function* (
|
|
142
|
+
width: number,
|
|
143
|
+
): Effect.fn.Return<string[]> {
|
|
144
|
+
if (width <= 0) return [""]; // guard, keeps the seam safe on tiny widths
|
|
145
|
+
|
|
146
|
+
const theme = ctx.ui.theme; // per-render live read (single theme source)
|
|
147
|
+
const state = getState(); // live getters re-invoked per run
|
|
148
|
+
const config = getConfig();
|
|
149
|
+
const glyphs = resolveGlyphs(config.icons.mode);
|
|
150
|
+
const segments = config.footerSegments;
|
|
151
|
+
const meta = getModelMeta();
|
|
152
|
+
const totals = getUsageTotals(ctx); // sync module cache, unchanged
|
|
153
|
+
|
|
154
|
+
const line1 = yield* renderLine1(theme, state, config, glyphs, segments, width);
|
|
155
|
+
const line2 = yield* renderLine2(theme, totals, meta, glyphs, segments, width);
|
|
156
|
+
const mainLines = [line1, line2].map((line) =>
|
|
157
|
+
truncateToWidth(line, width, theme.fg("dim", "...")),
|
|
158
|
+
);
|
|
159
|
+
if (!segments.extensionStatuses) return mainLines;
|
|
160
|
+
const ext = yield* renderExtensionLines(
|
|
161
|
+
theme,
|
|
162
|
+
footerData.getExtensionStatuses(),
|
|
163
|
+
glyphs,
|
|
164
|
+
width,
|
|
165
|
+
);
|
|
166
|
+
return [...mainLines, ...ext];
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return FooterRenderService.of({ render });
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Register the footer widget with `ctx.ui.setFooter` and return a cleanup
|
|
177
|
+
* that unregisters it. The mount factory keeps the plain mount-time side
|
|
178
|
+
* effects (request-render hook + branch-change subscription), builds the
|
|
179
|
+
* service layer synchronously at mount, and hands pi a closure component
|
|
180
|
+
* whose `render` runs the service's render effect synchronously and whose
|
|
181
|
+
* `dispose` clears the subscription and the hook.
|
|
182
|
+
*/
|
|
183
|
+
export function installFooter(
|
|
184
|
+
ctx: ExtensionContext,
|
|
185
|
+
getState: () => FooterState,
|
|
186
|
+
getConfig: () => TuiConfig,
|
|
187
|
+
getModelMeta: () => ModelMeta,
|
|
188
|
+
hooks: FooterHooks,
|
|
189
|
+
): () => void {
|
|
190
|
+
ctx.ui.setFooter((_tui, _theme, footerData) => {
|
|
191
|
+
hooks.setRequestRender(() => _tui.requestRender());
|
|
192
|
+
const unsubBranch = footerData.onBranchChange(() => {
|
|
193
|
+
hooks.scheduleGitRefresh();
|
|
194
|
+
_tui.requestRender();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Build the service layer and run construction synchronously at mount;
|
|
198
|
+
// the render effect runs per invocation at the seam below.
|
|
199
|
+
const svc = Context.get(
|
|
200
|
+
Effect.runSync(
|
|
201
|
+
Effect.context<FooterRenderService>().pipe(
|
|
202
|
+
Effect.provide(
|
|
203
|
+
FooterRenderService.make(ctx, getState, getConfig, getModelMeta, footerData),
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
),
|
|
207
|
+
FooterRenderService,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
return defineComponent({
|
|
211
|
+
dispose() {
|
|
212
|
+
unsubBranch();
|
|
213
|
+
hooks.setRequestRender(undefined);
|
|
214
|
+
},
|
|
215
|
+
invalidate() {},
|
|
216
|
+
render(width: number): string[] {
|
|
217
|
+
return Effect.runSync(svc.render(width)); // only sync run at the seam
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
return () => {
|
|
223
|
+
ctx.ui.setFooter(undefined);
|
|
224
|
+
};
|
|
225
|
+
}
|