@ftrdotdev/pi-tui 0.8.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 +46 -40
- 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
package/src/telemetry.ts
CHANGED
|
@@ -9,9 +9,15 @@ import type {
|
|
|
9
9
|
TurnStartEvent,
|
|
10
10
|
Theme,
|
|
11
11
|
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import { Context, Layer } from "effect";
|
|
12
13
|
import type { IconMode, TelemetryConfig } from "./config.ts";
|
|
14
|
+
import type { IconGlyphs } from "./icons.ts";
|
|
13
15
|
import { resolveGlyphs } from "./icons.ts";
|
|
14
16
|
import { fmtTokens, formatCost, formatDuration } from "./utils.ts";
|
|
17
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
18
|
+
import { DECLARATIONS, narrowCap } from "./chrome/declarations.ts";
|
|
19
|
+
import { composeLine, fitSurface, type Decision } from "./chrome/budget.ts";
|
|
20
|
+
import type { FactId } from "./chrome/facts.ts";
|
|
15
21
|
|
|
16
22
|
const STALL_THRESHOLD_MS = 1000;
|
|
17
23
|
|
|
@@ -119,7 +125,7 @@ export class TurnTelemetryTracker {
|
|
|
119
125
|
}
|
|
120
126
|
|
|
121
127
|
private startMessage(message: AgentMessage): void {
|
|
122
|
-
if (
|
|
128
|
+
if (this.turn === undefined || !isAssistantMessage(message)) return;
|
|
123
129
|
const now = this.now();
|
|
124
130
|
this.turn.currentMessage = {
|
|
125
131
|
lastUpdateMs: now,
|
|
@@ -140,7 +146,13 @@ export class TurnTelemetryTracker {
|
|
|
140
146
|
return;
|
|
141
147
|
if (streamEvent.delta.length === 0) return;
|
|
142
148
|
const message = event.message;
|
|
143
|
-
if (
|
|
149
|
+
if (
|
|
150
|
+
turn === undefined ||
|
|
151
|
+
current === undefined ||
|
|
152
|
+
current === null ||
|
|
153
|
+
!isAssistantMessage(message)
|
|
154
|
+
)
|
|
155
|
+
return;
|
|
144
156
|
|
|
145
157
|
const now = this.now();
|
|
146
158
|
if (current.firstOutputMs === null) {
|
|
@@ -163,10 +175,10 @@ export class TurnTelemetryTracker {
|
|
|
163
175
|
|
|
164
176
|
private endMessage(message: AgentMessage): void {
|
|
165
177
|
const turn = this.turn;
|
|
166
|
-
if (
|
|
178
|
+
if (turn === undefined || !isAssistantMessage(message)) return;
|
|
167
179
|
|
|
168
180
|
const current = turn.currentMessage;
|
|
169
|
-
if (current) {
|
|
181
|
+
if (current !== null) {
|
|
170
182
|
const endMs = this.now();
|
|
171
183
|
turn.generationMs = endMs - turn.startMs;
|
|
172
184
|
if (current.firstOutputMs === null && message.usage.output > 0) {
|
|
@@ -179,14 +191,14 @@ export class TurnTelemetryTracker {
|
|
|
179
191
|
|
|
180
192
|
private endTurnAndCollect(): TurnTelemetry | undefined {
|
|
181
193
|
const telemetry = this.endTurn();
|
|
182
|
-
if (telemetry && this.agentStartMs !== null) this.agentTurns.push(telemetry);
|
|
194
|
+
if (telemetry !== undefined && this.agentStartMs !== null) this.agentTurns.push(telemetry);
|
|
183
195
|
return telemetry;
|
|
184
196
|
}
|
|
185
197
|
|
|
186
198
|
private endTurn(): TurnTelemetry | undefined {
|
|
187
199
|
const turn = this.turn;
|
|
188
200
|
this.turn = undefined;
|
|
189
|
-
if (
|
|
201
|
+
if (turn === undefined || turn.firstTokenMs === null || turn.messages.length === 0) return;
|
|
190
202
|
|
|
191
203
|
const endMs = this.now();
|
|
192
204
|
let inputTokens = 0;
|
|
@@ -262,40 +274,135 @@ function formatTurnDuration(ms: number): string {
|
|
|
262
274
|
return ms < 60_000 ? `${(ms / 1000).toFixed(1)}s` : formatDuration(ms);
|
|
263
275
|
}
|
|
264
276
|
|
|
265
|
-
|
|
277
|
+
/** Notify fact → its telemetry config flag. */
|
|
278
|
+
const NOTIFY_FLAGS = {
|
|
279
|
+
turnTps: "tps",
|
|
280
|
+
turnTtft: "ttft",
|
|
281
|
+
turnStalls: "stalls",
|
|
282
|
+
turnDuration: "duration",
|
|
283
|
+
turnTokens: "tokens",
|
|
284
|
+
turnCostRate: "cost",
|
|
285
|
+
} as const satisfies Partial<Record<FactId, keyof TelemetryConfig>>;
|
|
286
|
+
|
|
287
|
+
/** True when the fact's telemetry switch is on. */
|
|
288
|
+
function notifyFactEnabled(fact: FactId, config: TelemetryConfig): boolean {
|
|
289
|
+
const flag = (NOTIFY_FLAGS as Partial<Record<FactId, keyof TelemetryConfig>>)[fact];
|
|
290
|
+
return flag !== undefined && config[flag];
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** The resolved notify parts, one text per enabled fact. */
|
|
294
|
+
function notifyParts(
|
|
266
295
|
telemetry: TurnTelemetry,
|
|
267
296
|
theme: Theme,
|
|
268
297
|
config: TelemetryConfig,
|
|
269
|
-
|
|
270
|
-
): string {
|
|
271
|
-
const
|
|
272
|
-
const
|
|
298
|
+
glyphs: IconGlyphs,
|
|
299
|
+
): { id: string; text: string }[] {
|
|
300
|
+
const parts: { id: string; text: string }[] = [];
|
|
301
|
+
const push = (id: string, text: string) => parts.push({ id, text });
|
|
273
302
|
if (config.tps) {
|
|
274
303
|
const value = telemetry.tps === null ? "—" : `${telemetry.tps.toFixed(1)} tok/s`;
|
|
275
|
-
|
|
304
|
+
push(
|
|
305
|
+
"turnTps",
|
|
276
306
|
theme.fg(telemetry.tps === null ? "muted" : "accent", `${glyphs.speed} TPS ${value}`),
|
|
277
307
|
);
|
|
278
308
|
}
|
|
279
309
|
if (config.ttft) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
if (config.tokens) {
|
|
286
|
-
parts.push(theme.fg("accent", `${glyphs.input} ${fmtTokens(telemetry.inputTokens)}`));
|
|
287
|
-
parts.push(theme.fg("success", `${glyphs.output} ${fmtTokens(telemetry.outputTokens)}`));
|
|
310
|
+
push(
|
|
311
|
+
"turnTtft",
|
|
312
|
+
theme.fg("text", `${glyphs.latency} TTFT ${formatTurnDuration(telemetry.ttftMs)}`),
|
|
313
|
+
);
|
|
288
314
|
}
|
|
289
315
|
if (config.stalls && telemetry.stallMs > 0) {
|
|
290
|
-
|
|
316
|
+
push(
|
|
317
|
+
"turnStalls",
|
|
291
318
|
theme.fg(
|
|
292
319
|
"warning",
|
|
293
320
|
`${glyphs.stall} stall ${telemetry.stallCount}x / ${formatTurnDuration(telemetry.stallMs)}`,
|
|
294
321
|
),
|
|
295
322
|
);
|
|
296
323
|
}
|
|
324
|
+
if (config.duration) {
|
|
325
|
+
push(
|
|
326
|
+
"turnDuration",
|
|
327
|
+
theme.fg("success", `${glyphs.done} ${formatTurnDuration(telemetry.totalMs)}`),
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
if (config.tokens) {
|
|
331
|
+
push(
|
|
332
|
+
"turnTokens",
|
|
333
|
+
`${theme.fg("accent", `${glyphs.input} ${fmtTokens(telemetry.inputTokens)}`)} ${theme.fg("dim", "|")} ${theme.fg("success", `${glyphs.output} ${fmtTokens(telemetry.outputTokens)}`)}`,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
297
336
|
if (config.cost && telemetry.rateUsdPerMTokens !== null) {
|
|
298
|
-
|
|
337
|
+
push(
|
|
338
|
+
"turnCostRate",
|
|
339
|
+
theme.fg("warning", formatCost(glyphs.cost, telemetry.rateUsdPerMTokens, 2) + "/M"),
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
return parts;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** The notify surface's fact decisions, fitted by declared priority. */
|
|
346
|
+
export function planNotify(
|
|
347
|
+
telemetry: TurnTelemetry,
|
|
348
|
+
config: TelemetryConfig,
|
|
349
|
+
width: number,
|
|
350
|
+
glyphs: IconGlyphs,
|
|
351
|
+
theme: Theme,
|
|
352
|
+
): readonly Decision[] {
|
|
353
|
+
const cap = narrowCap(width);
|
|
354
|
+
return fitSurface(
|
|
355
|
+
DECLARATIONS.notify.filter((declaration) => notifyFactEnabled(declaration.fact, config)),
|
|
356
|
+
notifyParts(telemetry, theme, config, glyphs),
|
|
357
|
+
width,
|
|
358
|
+
{
|
|
359
|
+
measure: visibleWidth,
|
|
360
|
+
...(cap === undefined ? {} : { maxPerLine: cap }),
|
|
361
|
+
truncate: (text, grant, ellipsis) => truncateToWidth(text, grant, ellipsis),
|
|
362
|
+
},
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** The agent-settled notify line, or `""` when the surface switch is off. */
|
|
367
|
+
export function composeNotifyLine(
|
|
368
|
+
telemetry: TurnTelemetry,
|
|
369
|
+
input: {
|
|
370
|
+
readonly theme: Theme;
|
|
371
|
+
readonly config: TelemetryConfig;
|
|
372
|
+
readonly iconMode: IconMode;
|
|
373
|
+
readonly width: number;
|
|
374
|
+
},
|
|
375
|
+
): string {
|
|
376
|
+
if (!input.config.enabled) return "";
|
|
377
|
+
const glyphs = resolveGlyphs(input.iconMode);
|
|
378
|
+
return composeLine(
|
|
379
|
+
planNotify(telemetry, input.config, input.width, glyphs, input.theme),
|
|
380
|
+
input.width,
|
|
381
|
+
(separator) => input.theme.fg("dim", separator),
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export class TurnTelemetryService extends Context.Service<
|
|
386
|
+
TurnTelemetryService,
|
|
387
|
+
{
|
|
388
|
+
/** Synchronous: pi's event handlers and the notify call site are synchronous. */
|
|
389
|
+
readonly handle: (event: TelemetryEvent) => TurnTelemetry | undefined;
|
|
390
|
+
/** Session boundary: drop the open turn and the agent window (D6). */
|
|
391
|
+
readonly reset: () => void;
|
|
299
392
|
}
|
|
300
|
-
|
|
393
|
+
>()("@ftrdotdev/pi-tui/telemetry/TurnTelemetryService") {
|
|
394
|
+
static make(now: () => number = () => performance.now()): Layer.Layer<TurnTelemetryService> {
|
|
395
|
+
return Layer.sync(TurnTelemetryService, () => {
|
|
396
|
+
let tracker = new TurnTelemetryTracker(now);
|
|
397
|
+
return TurnTelemetryService.of({
|
|
398
|
+
handle: (event) => tracker.handle(event),
|
|
399
|
+
reset: () => {
|
|
400
|
+
tracker = new TurnTelemetryTracker(now);
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
static readonly layer = TurnTelemetryService.make();
|
|
407
|
+
static readonly layerTest = (now?: () => number) => TurnTelemetryService.make(now);
|
|
301
408
|
}
|
|
@@ -1,58 +1,71 @@
|
|
|
1
|
+
// Each test is its own entry point: it builds a fresh layer for one scenario and
|
|
2
|
+
// runs its effects inside it, so providing there is the pattern the rule allows.
|
|
3
|
+
/* oxlint-disable effecttsgo/strict-effect-provide */
|
|
1
4
|
import { assert, it } from "@effect/vitest";
|
|
2
5
|
import { Effect } from "effect";
|
|
3
6
|
import type { ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import {
|
|
5
|
-
EditorTintService,
|
|
6
|
-
type EditorTintServiceHandle,
|
|
7
|
-
upsertBorderTintProvider,
|
|
8
|
-
} from "./editor-tint.ts";
|
|
9
|
-
|
|
10
|
-
const resolveTint = (): EditorTintServiceHandle =>
|
|
11
|
-
Effect.runSync(Effect.service(EditorTintService).pipe(Effect.provide(EditorTintService.layer)));
|
|
7
|
+
import { EditorTintService, upsertBorderTintProvider } from "./editor-tint.ts";
|
|
12
8
|
|
|
13
9
|
// ---------------------------------------------------------------------------
|
|
14
10
|
// EditorTintService (configure / getTint)
|
|
15
11
|
// ---------------------------------------------------------------------------
|
|
16
12
|
|
|
17
|
-
it("returns undefined with no providers registered", () =>
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
it.effect("returns undefined with no providers registered", () =>
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const tint = yield* EditorTintService;
|
|
16
|
+
assert.strictEqual(tint.getTint(), undefined);
|
|
17
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
18
|
+
);
|
|
20
19
|
|
|
21
|
-
it("returns the registered provider's tint", () =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
it.effect("returns the registered provider's tint", () =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
const tint = yield* EditorTintService;
|
|
23
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "a", getTint: () => "success" }));
|
|
24
|
+
assert.strictEqual(tint.getTint(), "success");
|
|
25
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
26
|
+
);
|
|
26
27
|
|
|
27
|
-
it("the last provider with a defined tint wins (later .configure overrides)", () =>
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
it.effect("the last provider with a defined tint wins (later .configure overrides)", () =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const tint = yield* EditorTintService;
|
|
31
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "a", getTint: () => "success" }));
|
|
32
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "b", getTint: () => "error" }));
|
|
33
|
+
assert.strictEqual(tint.getTint(), "error");
|
|
34
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
35
|
+
);
|
|
33
36
|
|
|
34
|
-
it("providers whose tint is undefined fall through to earlier ones", () =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
it.effect("providers whose tint is undefined fall through to earlier ones", () =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const tint = yield* EditorTintService;
|
|
40
|
+
tint.configure((cur) =>
|
|
41
|
+
upsertBorderTintProvider(cur, { id: "base", getTint: () => "success" }),
|
|
42
|
+
);
|
|
43
|
+
tint.configure((cur) =>
|
|
44
|
+
upsertBorderTintProvider(cur, { id: "pass", getTint: () => undefined }),
|
|
45
|
+
);
|
|
46
|
+
// "pass" is last but undefined → falls through to "base"
|
|
47
|
+
assert.strictEqual(tint.getTint(), "success");
|
|
48
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
49
|
+
);
|
|
41
50
|
|
|
42
|
-
it("upserting the same id replaces it in place", () =>
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
it.effect("upserting the same id replaces it in place", () =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const tint = yield* EditorTintService;
|
|
54
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "x", getTint: () => "success" }));
|
|
55
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "x", getTint: () => "error" }));
|
|
56
|
+
assert.strictEqual(tint.getTint(), "error");
|
|
57
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
58
|
+
);
|
|
48
59
|
|
|
49
|
-
it("a provider's tint is read live on every getTint (dynamic)", () =>
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
it.effect("a provider's tint is read live on every getTint (dynamic)", () =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const tint = yield* EditorTintService;
|
|
63
|
+
let current: ThemeColor | undefined = "success";
|
|
64
|
+
tint.configure((cur) => upsertBorderTintProvider(cur, { id: "dyn", getTint: () => current }));
|
|
65
|
+
assert.strictEqual(tint.getTint(), "success");
|
|
66
|
+
current = "warning";
|
|
67
|
+
assert.strictEqual(tint.getTint(), "warning");
|
|
68
|
+
current = undefined;
|
|
69
|
+
assert.strictEqual(tint.getTint(), undefined);
|
|
70
|
+
}).pipe(Effect.provide(EditorTintService.layer)),
|
|
71
|
+
);
|
package/src/ui/editor-tint.ts
CHANGED
|
@@ -36,7 +36,7 @@ export class EditorTintService extends Context.Service<
|
|
|
36
36
|
) => void;
|
|
37
37
|
readonly getTint: () => ThemeColor | Color | undefined;
|
|
38
38
|
}
|
|
39
|
-
>()("tui/editor/EditorTintService") {
|
|
39
|
+
>()("@ftrdotdev/pi-tui/ui/editor-tint/EditorTintService") {
|
|
40
40
|
static readonly layer = Layer.sync(EditorTintService, () => {
|
|
41
41
|
let providers: readonly BorderTintProvider[] = [];
|
|
42
42
|
|
package/src/ui/editor.ts
CHANGED
|
@@ -14,7 +14,16 @@ import {
|
|
|
14
14
|
} from "../autocomplete-contract.ts";
|
|
15
15
|
import { makeBorderedBox } from "../components/bordered-box.ts";
|
|
16
16
|
import { buildBoxFrame, composeBorderLine } from "../frame/box-frame.ts";
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
findBottomBorderIndex,
|
|
19
|
+
isEditorBorderLine,
|
|
20
|
+
isNonEmptyString,
|
|
21
|
+
padRight,
|
|
22
|
+
stripAnsi,
|
|
23
|
+
} from "../utils.ts";
|
|
24
|
+
import { DECLARATIONS, narrowCap } from "../chrome/declarations.ts";
|
|
25
|
+
import { composeLine, fitSurface, type Decision } from "../chrome/budget.ts";
|
|
26
|
+
import type { IconGlyphs } from "../icons.ts";
|
|
18
27
|
import { closeAutocomplete } from "../vim/editor-close-autocomplete.ts";
|
|
19
28
|
import { VimEditorState } from "../vim/editor-state.ts";
|
|
20
29
|
import { vimModeGlyph, vimModeTint } from "../vim/mode-indicator.ts";
|
|
@@ -31,20 +40,65 @@ interface BorderStatus {
|
|
|
31
40
|
readonly spinner: string;
|
|
32
41
|
}
|
|
33
42
|
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
/** The editor-owned state the top border's fact decisions read. */
|
|
44
|
+
export interface BorderState {
|
|
45
|
+
readonly statusText?: string;
|
|
46
|
+
readonly vimGlyph?: string;
|
|
47
|
+
readonly scrollHint?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The frame width threshold passed to `buildBoxFrame`; the box default is 8. */
|
|
51
|
+
const MIN_BORDER_LABEL_WIDTH = 3;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The status label's full → spinner → spinner-alone reduction, unchanged from
|
|
55
|
+
* the former loop: a candidate fits when the status frame keeps at least one
|
|
56
|
+
* dash after `╭─ <label> ─`. The vim glyph participates as the prefix the
|
|
57
|
+
* filter must leave room for, then rides its own declaration.
|
|
58
|
+
*/
|
|
59
|
+
function resolveBorderStatus(
|
|
60
|
+
status: BorderStatus,
|
|
61
|
+
width: number,
|
|
62
|
+
glyph: string | undefined,
|
|
63
|
+
): string {
|
|
64
|
+
const inner = Math.max(0, width - 2);
|
|
65
|
+
const prefix = glyph === undefined || glyph.length === 0 ? "" : `${glyph} · `;
|
|
66
|
+
const fits = (text: string): boolean => {
|
|
67
|
+
const textWidth = visibleWidth(text);
|
|
68
|
+
return textWidth > 0 && inner - 2 - textWidth - 1 >= 0;
|
|
69
|
+
};
|
|
70
|
+
if (fits(`${prefix}${status.full}`)) return status.full;
|
|
71
|
+
if (fits(`${prefix}${status.spinner}`)) return status.spinner;
|
|
72
|
+
return status.spinner;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The border's fact decisions: work state → vim glyph → scroll hint, in
|
|
77
|
+
* declared priority order. Below the narrow threshold the cap keeps one field;
|
|
78
|
+
* the frame later decides whether the label geometry fits.
|
|
79
|
+
*/
|
|
80
|
+
export function planBorder(state: BorderState, width: number): readonly Decision[] {
|
|
81
|
+
const inner = Math.max(0, width - 2);
|
|
82
|
+
const resolved = [
|
|
83
|
+
isNonEmptyString(state.statusText) ? { id: "editorStatus", text: state.statusText } : undefined,
|
|
84
|
+
isNonEmptyString(state.vimGlyph) ? { id: "vimMode", text: state.vimGlyph } : undefined,
|
|
85
|
+
isNonEmptyString(state.scrollHint) ? { id: "scrollHint", text: state.scrollHint } : undefined,
|
|
86
|
+
].filter((entry): entry is { id: string; text: string } => entry !== undefined);
|
|
87
|
+
const cap = narrowCap(inner);
|
|
88
|
+
const decisions = fitSurface(DECLARATIONS.border, resolved, inner, {
|
|
89
|
+
measure: visibleWidth,
|
|
90
|
+
...(cap === undefined ? {} : { maxPerLine: cap }),
|
|
91
|
+
truncate: (text, grant, ellipsis) => truncateToWidth(text, grant, ellipsis),
|
|
92
|
+
});
|
|
93
|
+
return decisions;
|
|
38
94
|
}
|
|
39
95
|
|
|
40
96
|
/**
|
|
41
97
|
* Top or bottom border of the editor frame, composed from the shared
|
|
42
98
|
* {@link buildBoxFrame} model and {@link composeBorderLine} so every box in
|
|
43
99
|
* the package uses the same geometry. The top border carries the host
|
|
44
|
-
* editor's scroll hint (`↓ N more`)
|
|
45
|
-
*
|
|
46
|
-
* the model's default decorative dash tail — hence the `labelSuffix`
|
|
47
|
-
* override.
|
|
100
|
+
* editor's scroll hint (`↓ N more`), the mode indicator glyph (`N · ↓ 3 more`)
|
|
101
|
+
* and any embedded status, fitted by declaration priority.
|
|
48
102
|
*/
|
|
49
103
|
function roundedBorder(
|
|
50
104
|
width: number,
|
|
@@ -56,32 +110,35 @@ function roundedBorder(
|
|
|
56
110
|
): string {
|
|
57
111
|
const scrollLabel =
|
|
58
112
|
sourceLine === undefined ? undefined : stripAnsi(sourceLine).match(/([↑↓]\s+\d+\s+more)/)?.[1];
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
113
|
+
const statusText =
|
|
114
|
+
status === undefined ? undefined : resolveBorderStatus(status, width, modeGlyph);
|
|
115
|
+
const state: BorderState = {
|
|
116
|
+
...(statusText === undefined ? {} : { statusText }),
|
|
117
|
+
...(modeGlyph === undefined || modeGlyph.length === 0 ? {} : { vimGlyph: modeGlyph }),
|
|
118
|
+
...(scrollLabel === undefined ? {} : { scrollHint: scrollLabel }),
|
|
119
|
+
};
|
|
120
|
+
const inner = Math.max(0, width - 2);
|
|
121
|
+
const decisions = planBorder(state, width);
|
|
122
|
+
const label = composeLine(decisions, inner);
|
|
123
|
+
const hasStatus = decisions.some(
|
|
124
|
+
(decision) => decision.fact === "workState" && decision.action !== "drop",
|
|
125
|
+
);
|
|
126
|
+
// An embedded status keeps the host editor's `╭─ <status> ─────╮` shape; the
|
|
127
|
+
// status outranks the glyph and the hint, so its frame is drawn first.
|
|
128
|
+
if (kind === "top" && hasStatus && visibleWidth(label) > 0) {
|
|
129
|
+
const dashes = inner - 2 - visibleWidth(label) - 1;
|
|
130
|
+
if (dashes >= 0) {
|
|
131
|
+
return paint("╭─ ") + label + paint(` ${"─".repeat(dashes)}`) + paint("╮");
|
|
72
132
|
}
|
|
73
133
|
}
|
|
74
|
-
const label = joinLabel(modeGlyph, scrollLabel);
|
|
75
134
|
const frame = buildBoxFrame(width, {
|
|
76
|
-
label,
|
|
77
|
-
|
|
78
|
-
labelSuffix: label === undefined ? undefined : " ",
|
|
79
|
-
minLabelWidth: 3,
|
|
135
|
+
...(label === "" ? {} : { label, labelWidth: visibleWidth(label), labelSuffix: " " }),
|
|
136
|
+
minLabelWidth: MIN_BORDER_LABEL_WIDTH,
|
|
80
137
|
paddingX: 0,
|
|
81
138
|
});
|
|
82
139
|
// The caller guards `width < 4`, so the frame is never null here; the
|
|
83
140
|
// fallback only narrows the type for the degenerate model.
|
|
84
|
-
if (
|
|
141
|
+
if (frame === null) return paint(kind === "top" ? "╭╮" : "╰╯");
|
|
85
142
|
return composeBorderLine(frame, kind, paint);
|
|
86
143
|
}
|
|
87
144
|
|
|
@@ -116,11 +173,11 @@ export class TuiEditor extends CustomEditor {
|
|
|
116
173
|
// mode / thinking-level recoloring and any registered border tint like the
|
|
117
174
|
// input frame does.
|
|
118
175
|
private readonly boxTheme: Theme;
|
|
119
|
-
private readonly onAutocompleteMismatch
|
|
176
|
+
private readonly onAutocompleteMismatch: ((detail: string) => void) | undefined;
|
|
120
177
|
private readonly paintSelection: (s: string) => string;
|
|
121
|
-
private readonly getBorderTint
|
|
122
|
-
private readonly tintPaint
|
|
123
|
-
private readonly getModeIndicator
|
|
178
|
+
private readonly getBorderTint: (() => ThemeColor | Color | undefined) | undefined;
|
|
179
|
+
private readonly tintPaint: ((color: ThemeColor | Color, s: string) => string) | undefined;
|
|
180
|
+
private readonly getModeIndicator: (() => string | undefined) | undefined;
|
|
124
181
|
/**
|
|
125
182
|
* The status indicator pi embeds in the top border. pi only calls
|
|
126
183
|
* `setWorkingStatusIndicator()` when `embedWorkingStatus` is true, so we
|
|
@@ -243,7 +300,7 @@ export class TuiEditor extends CustomEditor {
|
|
|
243
300
|
state?: { cursorLine?: number; cursorCol?: number };
|
|
244
301
|
setCursorCol?: (col: number) => void;
|
|
245
302
|
};
|
|
246
|
-
if (
|
|
303
|
+
if (internal.state === undefined) return;
|
|
247
304
|
const line = Math.max(0, Math.min(c.line, this.getLineCount() - 1));
|
|
248
305
|
const col = Math.max(0, Math.min(c.col, this.lineLength(line)));
|
|
249
306
|
internal.state.cursorLine = line;
|
|
@@ -298,7 +355,7 @@ export class TuiEditor extends CustomEditor {
|
|
|
298
355
|
if (decision.kind === "consumed") {
|
|
299
356
|
// Entering Normal (Escape in Insert) closes the autocomplete popup
|
|
300
357
|
// (binding decision 2); the TUI re-renders after handleInput.
|
|
301
|
-
if (decision.enteredNormal) closeAutocomplete(this);
|
|
358
|
+
if (decision.enteredNormal ?? false) closeAutocomplete(this);
|
|
302
359
|
return;
|
|
303
360
|
}
|
|
304
361
|
super.handleInput(data);
|
|
@@ -312,7 +369,7 @@ export class TuiEditor extends CustomEditor {
|
|
|
312
369
|
*/
|
|
313
370
|
private borderPaint(s: string): string {
|
|
314
371
|
const tint = this.getBorderTint?.();
|
|
315
|
-
if (tint !== undefined && this.tintPaint) return this.tintPaint(tint, s);
|
|
372
|
+
if (tint !== undefined && this.tintPaint !== undefined) return this.tintPaint(tint, s);
|
|
316
373
|
return this.borderColor(s);
|
|
317
374
|
}
|
|
318
375
|
|
|
@@ -330,7 +387,7 @@ export class TuiEditor extends CustomEditor {
|
|
|
330
387
|
// internals changed shape) we fall back to the scan below.
|
|
331
388
|
const internals = this.getAutocompleteView();
|
|
332
389
|
const list = internals?.autocompleteList;
|
|
333
|
-
const listLines = list ? list.render(innerWidth) : [];
|
|
390
|
+
const listLines = list !== undefined ? list.render(innerWidth) : [];
|
|
334
391
|
const hasFramedList =
|
|
335
392
|
this.isShowingAutocomplete() &&
|
|
336
393
|
listLines.length > 0 &&
|
|
@@ -343,28 +400,33 @@ export class TuiEditor extends CustomEditor {
|
|
|
343
400
|
// the content lines (between the top border at 0 and the bottom border at
|
|
344
401
|
// bottomIdx). First cut maps the logical range onto the rendered lines.
|
|
345
402
|
const selection = this.vimState.selection;
|
|
346
|
-
const contentLines =
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
403
|
+
const contentLines =
|
|
404
|
+
selection !== undefined
|
|
405
|
+
? renderSelection({
|
|
406
|
+
lines: baseLines.slice(1, bottomIdx),
|
|
407
|
+
selection,
|
|
408
|
+
paint: this.paintSelection,
|
|
409
|
+
})
|
|
410
|
+
: undefined;
|
|
353
411
|
|
|
354
412
|
const result: string[] = [];
|
|
355
413
|
const indicator = this.statusIndicator;
|
|
356
|
-
const status: BorderStatus | undefined =
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
414
|
+
const status: BorderStatus | undefined =
|
|
415
|
+
indicator !== undefined
|
|
416
|
+
? {
|
|
417
|
+
full: indicator.renderInBorder(Math.max(1, width - 5)),
|
|
418
|
+
spinner: indicator.renderSpinnerInBorder(width),
|
|
419
|
+
}
|
|
420
|
+
: undefined;
|
|
362
421
|
result.push(
|
|
363
422
|
roundedBorder(width, "top", borderPaint, baseLines[0], this.getModeIndicator?.(), status),
|
|
364
423
|
);
|
|
365
424
|
|
|
366
425
|
for (let i = 1; i < bottomIdx; i++) {
|
|
367
|
-
const line =
|
|
426
|
+
const line =
|
|
427
|
+
contentLines !== undefined && contentLines.length > 0
|
|
428
|
+
? (contentLines[i - 1] ?? "")
|
|
429
|
+
: (baseLines[i] ?? "");
|
|
368
430
|
if (isEditorBorderLine(line)) {
|
|
369
431
|
result.push(`${rail} ${padRight("", innerWidth)} ${rail}`);
|
|
370
432
|
} else {
|
|
@@ -377,7 +439,7 @@ export class TuiEditor extends CustomEditor {
|
|
|
377
439
|
if (hasFramedList) {
|
|
378
440
|
const prefix = internals?.autocompletePrefix;
|
|
379
441
|
const label = truncateToWidth(
|
|
380
|
-
`suggestions${prefix ? ` · ${prefix}` : ""}`,
|
|
442
|
+
`suggestions${isNonEmptyString(prefix) ? ` · ${prefix}` : ""}`,
|
|
381
443
|
Math.max(4, width - 16),
|
|
382
444
|
"",
|
|
383
445
|
);
|
|
@@ -402,6 +464,7 @@ export function installEditor(
|
|
|
402
464
|
ctx: ExtensionContext,
|
|
403
465
|
options: {
|
|
404
466
|
readonly getVimEnabled: () => boolean;
|
|
467
|
+
readonly getGlyphs: () => Pick<IconGlyphs, "vimNormal" | "vimVisual" | "vimInsert">;
|
|
405
468
|
readonly tint: EditorTintServiceHandle;
|
|
406
469
|
},
|
|
407
470
|
): () => void {
|
|
@@ -414,8 +477,9 @@ export function installEditor(
|
|
|
414
477
|
// knows nothing about vim. vim is just one provider registered here.
|
|
415
478
|
getBorderTint: () => options.tint.getTint(),
|
|
416
479
|
// vim registers its own live mode indicator (the editor stays generic
|
|
417
|
-
// and just asks the injected getter per render, like the tint).
|
|
418
|
-
|
|
480
|
+
// and just asks the injected getter per render, like the tint). The
|
|
481
|
+
// glyph comes from the configured icon mode's table.
|
|
482
|
+
getModeIndicator: () => vimModeGlyph(editor.vimState, options.getGlyphs()),
|
|
419
483
|
paintSelection: (s) => ctx.ui.theme.inverse(s),
|
|
420
484
|
onAutocompleteMismatch: (detail) => {
|
|
421
485
|
const message =
|