@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.
Files changed (76) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +5 -0
  3. package/package.json +18 -15
  4. package/src/activity-service.test.ts +97 -0
  5. package/src/activity.test.ts +1 -1
  6. package/src/activity.ts +38 -0
  7. package/src/chrome/budget.test.ts +345 -0
  8. package/src/chrome/budget.ts +220 -0
  9. package/src/chrome/declarations.ts +303 -0
  10. package/src/chrome/facts.test.ts +165 -0
  11. package/src/chrome/facts.ts +267 -0
  12. package/src/chrome/frame.ts +45 -0
  13. package/src/chrome/laws.test.ts +231 -0
  14. package/src/chrome/port.test.ts +164 -0
  15. package/src/chrome/port.ts +67 -0
  16. package/src/chrome/service-provider.test.ts +383 -0
  17. package/src/chrome/service-provider.ts +55 -0
  18. package/src/commands/git-execution.test.ts +5 -2
  19. package/src/commands/git-execution.ts +27 -15
  20. package/src/commands/git-status.test.ts +47 -15
  21. package/src/commands/git-status.ts +83 -31
  22. package/src/components/bordered-box.test.ts +12 -15
  23. package/src/components/bordered-box.ts +11 -7
  24. package/src/components/define-component.ts +1 -1
  25. package/src/components/index.ts +7 -0
  26. package/src/components/row-list.test.ts +310 -0
  27. package/src/components/row-list.ts +272 -0
  28. package/src/config.test.ts +118 -3
  29. package/src/config.ts +50 -42
  30. package/src/frame/box-frame.test.ts +53 -7
  31. package/src/harness-policy.test.ts +47 -0
  32. package/src/icons.test.ts +19 -0
  33. package/src/icons.ts +50 -31
  34. package/src/index.test.ts +135 -0
  35. package/src/index.ts +118 -119
  36. package/src/layers.test.ts +94 -0
  37. package/src/layers.ts +92 -0
  38. package/src/runtime-service.test.ts +218 -0
  39. package/src/runtime-service.ts +103 -0
  40. package/src/runtime.test.ts +165 -66
  41. package/src/runtime.ts +127 -93
  42. package/src/session-lifecycle-service.test.ts +95 -0
  43. package/src/session-lifecycle.test.ts +77 -0
  44. package/src/session-lifecycle.ts +21 -0
  45. package/src/state.test.ts +132 -3
  46. package/src/state.ts +43 -13
  47. package/src/telemetry-service.test.ts +250 -0
  48. package/src/telemetry.test.ts +322 -0
  49. package/src/telemetry.ts +130 -23
  50. package/src/ui/editor-tint.test.ts +58 -45
  51. package/src/ui/editor-tint.ts +1 -1
  52. package/src/ui/editor.ts +117 -53
  53. package/src/ui/footer.test.ts +349 -120
  54. package/src/ui/footer.ts +147 -124
  55. package/src/ui/header.test.ts +203 -223
  56. package/src/ui/header.ts +32 -96
  57. package/src/ui/preview.test.ts +73 -70
  58. package/src/ui/preview.ts +48 -33
  59. package/src/ui/render-helpers.test.ts +82 -13
  60. package/src/ui/render-helpers.ts +110 -122
  61. package/src/ui/settings-command.test.ts +70 -11
  62. package/src/ui/settings-command.ts +30 -45
  63. package/src/ui/theme-color.test.ts +4 -3
  64. package/src/usage-totals-service.test.ts +230 -0
  65. package/src/usage-totals-service.ts +59 -0
  66. package/src/utils.test.ts +46 -50
  67. package/src/utils.ts +41 -239
  68. package/src/vim/editor-host.test.ts +201 -75
  69. package/src/vim/editor-state.ts +2 -2
  70. package/src/vim/mode-indicator.test.ts +6 -5
  71. package/src/vim/mode-indicator.ts +13 -9
  72. package/src/vim/selection-render.ts +8 -4
  73. package/src/vim/text-model.test.ts +52 -14
  74. package/src/vim/text-model.ts +1 -1
  75. package/src/vim/vim-router.test.ts +3 -0
  76. package/src/vim/vim-router.ts +1 -1
package/src/ui/footer.ts CHANGED
@@ -1,31 +1,23 @@
1
- import type {
2
- ExtensionContext,
3
- ReadonlyFooterDataProvider,
4
- Theme,
5
- } from "@earendil-works/pi-coding-agent";
1
+ import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
6
2
  import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
7
3
  import { Context, Effect, Layer } from "effect";
8
- import type { TuiConfig } from "../config.ts";
9
- import type { IconGlyphs } from "../icons.ts";
10
4
  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";
5
+ import { effortColor, formatCwd, isNonEmptyString, providerColor, truncatePath } from "../utils.ts";
6
+ import type { ModelMeta, UsageTotals } from "../state.ts";
21
7
  import { defineComponent } from "../components/define-component.ts";
8
+ import { DECLARATIONS, narrowCap } from "../chrome/declarations.ts";
9
+ import { composeLine, fitSurface, type Decision } from "../chrome/budget.ts";
10
+ import type { ChromePort } from "../chrome/port.ts";
22
11
  import {
23
12
  renderContextBar,
13
+ renderCostSegment,
24
14
  renderExtensionStatusLines,
25
- renderGitSegment,
15
+ renderGitBranchSegment,
16
+ renderGitCommitSegment,
17
+ renderGitStatusSegment,
26
18
  renderRuntimeSegment,
27
- renderStatsBlock,
28
19
  renderTimerSegment,
20
+ renderTokenCountsSegment,
29
21
  } from "./render-helpers.ts";
30
22
 
31
23
  /**
@@ -37,106 +29,139 @@ export interface FooterHooks {
37
29
  scheduleGitRefresh: () => void;
38
30
  }
39
31
 
32
+ /** The fit options every footer planner passes to the budget engine. */
33
+ const fitOptions = (theme: Theme, width: number) => {
34
+ const cap = narrowCap(width);
35
+ return {
36
+ measure: visibleWidth,
37
+ ...(cap === undefined ? {} : { maxPerLine: cap }),
38
+ truncate: (text: string, grant: number, ellipsis: string) =>
39
+ truncateToWidth(text, grant, theme.fg("dim", ellipsis)),
40
+ };
41
+ };
42
+
43
+ /** The composite model block: glyph, provider and model, joined by dim dots. */
44
+ function renderModelIdentity(
45
+ theme: Theme,
46
+ meta: ModelMeta,
47
+ glyphs: ReturnType<typeof resolveGlyphs>,
48
+ ): string {
49
+ const provider =
50
+ meta.provider !== "" && meta.provider !== "Unknown"
51
+ ? ` ${theme.fg(providerColor(meta.provider), meta.provider)}`
52
+ : "";
53
+ return `${theme.fg("mdLink", glyphs.model)}${provider}${theme.fg("dim", " · ")}${theme.fg("text", meta.model)}`;
54
+ }
55
+
56
+ /**
57
+ * Line 1's fact decisions: cwd, git and the timer on the left, the context
58
+ * occupancy bar on the right, fitted by declared priority and degradation.
59
+ */
60
+ export function planFooterLine1(port: ChromePort, width: number): readonly Decision[] {
61
+ const theme = port.facts.theme();
62
+ const config = port.facts.config();
63
+ const segments = config.footerSegments;
64
+ const glyphs = resolveGlyphs(config.icons.mode);
65
+ const resolved: { id: string; text: string }[] = [];
66
+ const push = (id: string, text: string) => {
67
+ if (text !== "") resolved.push({ id, text });
68
+ };
69
+
70
+ if (segments.cwd) {
71
+ const maxCwd = Math.min(30, Math.max(10, Math.floor(width * 0.4)));
72
+ push(
73
+ "cwd",
74
+ `${theme.fg("mdLink", glyphs.cwd)} ${theme.fg("accent", truncatePath(formatCwd(port.facts.cwd()), maxCwd))}`,
75
+ );
76
+ }
77
+ const git = port.facts.git();
78
+ push("gitBranch", renderGitBranchSegment(theme, git, glyphs, segments));
79
+ push("gitCommit", renderGitCommitSegment(theme, git, segments));
80
+ push("gitStatus", renderGitStatusSegment(theme, git, glyphs, segments));
81
+ const timer = renderTimerSegment(
82
+ theme,
83
+ port.facts.activity(),
84
+ port.facts.lastDoneIn(),
85
+ glyphs,
86
+ port.facts.now(),
87
+ );
88
+ if (timer !== undefined) {
89
+ push("workTimer", timer.total);
90
+ push("workSplit", timer.split);
91
+ }
92
+ if (segments.runtime) {
93
+ push("runtime", renderRuntimeSegment(theme, port.facts.runtime(), config.icons.mode));
94
+ }
95
+ if (segments.context) {
96
+ push("contextOccupancy", renderContextBar(theme, port.facts.contextUsage(), glyphs));
97
+ }
98
+
99
+ return fitSurface(
100
+ DECLARATIONS.footer.filter((declaration) => declaration.line === 1),
101
+ resolved,
102
+ width,
103
+ fitOptions(theme, width),
104
+ );
105
+ }
106
+
107
+ /**
108
+ * Line 2's fact decisions: the model identity block on the left, token totals
109
+ * and cost on the right. The thinking level is a separate declared fact, so it
110
+ * yields before the model identity.
111
+ */
112
+ export function planFooterLine2(port: ChromePort, width: number): readonly Decision[] {
113
+ const theme = port.facts.theme();
114
+ const config = port.facts.config();
115
+ const segments = config.footerSegments;
116
+ const glyphs = resolveGlyphs(config.icons.mode);
117
+ const meta = port.facts.model();
118
+ const totals: UsageTotals = port.facts.usage();
119
+ const resolved: { id: string; text: string }[] = [
120
+ { id: "modelIdentity", text: renderModelIdentity(theme, meta, glyphs) },
121
+ {
122
+ id: "thinking",
123
+ text:
124
+ isNonEmptyString(meta.effort) && meta.effort !== "off"
125
+ ? theme.fg(effortColor(meta.effort), `${glyphs.thinking} ${meta.effort}`)
126
+ : "",
127
+ },
128
+ { id: "tokenCounts", text: renderTokenCountsSegment(theme, totals, glyphs, segments) },
129
+ { id: "cost", text: renderCostSegment(theme, totals, glyphs, segments) },
130
+ ];
131
+
132
+ return fitSurface(
133
+ DECLARATIONS.footer.filter((declaration) => declaration.line === 2),
134
+ resolved,
135
+ width,
136
+ fitOptions(theme, width),
137
+ );
138
+ }
139
+
140
+ /** Every fitted footer decision (lines 1 and 2), for the frame planner. */
141
+ export function planFooter(port: ChromePort, width: number): readonly Decision[] {
142
+ return [...planFooterLine1(port, width), ...planFooterLine2(port, width)];
143
+ }
144
+
40
145
  /**
41
146
  * 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.
147
+ * a `Layer.effect` factory capturing the port and the injected live getters,
148
+ * named synchronous total steps for each line, and a single public `render`
149
+ * member. Live inputs are re-read on every run; nothing is snapshotted at
150
+ * service build.
46
151
  */
47
152
  export class FooterRenderService extends Context.Service<
48
153
  FooterRenderService,
49
154
  { 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> {
155
+ >()("@ftrdotdev/pi-tui/ui/footer/FooterRenderService") {
156
+ static make(port: ChromePort): Layer.Layer<FooterRenderService> {
58
157
  return Layer.effect(
59
158
  FooterRenderService,
60
159
  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 timerParts = renderTimerSegment(theme, state, glyphs);
87
- if (timerParts) {
88
- leftParts.push({ text: timerParts.total, priority: 2 });
89
- // The split yields before the total on narrow widths: it packs
90
- // at priority 1, below the total's priority 2.
91
- if (timerParts.split) leftParts.push({ text: timerParts.split, priority: 1 });
92
- }
93
-
94
- let rightBlock = "";
95
- if (segments.context) {
96
- rightBlock = renderContextBar(theme, ctx, width, glyphs, config.icons.mode);
97
- }
98
-
99
- const rightW = visibleWidth(rightBlock);
100
- const availLeft = Math.max(0, width - rightW - (rightBlock ? 1 : 0));
101
- const fittedLeft = fitSegmentsByPriority(leftParts, availLeft, theme.fg("dim", "..."));
102
- return alignRight(fittedLeft.join(" "), rightBlock, width, theme);
103
- });
104
-
105
- // Named synchronous total step: line 2 (model block and stats block).
106
- const renderLine2 = (
107
- theme: Theme,
108
- totals: UsageTotals,
109
- meta: ModelMeta,
110
- glyphs: IconGlyphs,
111
- segments: TuiConfig["footerSegments"],
112
- width: number,
113
- ): Effect.Effect<string> =>
114
- Effect.sync(function () {
115
- const modelParts: string[] = [];
116
- modelParts.push(theme.fg("mdLink", glyphs.model));
117
- if (meta.provider && meta.provider !== "Unknown") {
118
- modelParts.push(
119
- theme.fg(providerColor(ctx.model?.provider ?? "none"), meta.provider),
120
- );
121
- }
122
- modelParts.push(theme.fg("text", meta.model));
123
- if (meta.effort && meta.effort !== "off") {
124
- modelParts.push(
125
- theme.fg(effortColor(meta.effort), `${glyphs.thinking} ${meta.effort}`),
126
- );
127
- }
128
- const modelBlock = modelParts.join(theme.fg("dim", " · "));
129
-
130
- const statsBlock = renderStatsBlock(theme, totals, glyphs, segments);
131
-
132
- return alignRight(modelBlock, statsBlock, width, theme);
133
- });
134
-
135
160
  // Named synchronous total step: wrapped extension status lines.
136
161
  const renderExtensionLines = (
137
162
  theme: Theme,
138
163
  extensionStatuses: ReadonlyMap<string, string>,
139
- glyphs: IconGlyphs,
164
+ glyphs: ReturnType<typeof resolveGlyphs>,
140
165
  width: number,
141
166
  ) =>
142
167
  Effect.sync(function () {
@@ -148,23 +173,18 @@ export class FooterRenderService extends Context.Service<
148
173
  ): Effect.fn.Return<string[]> {
149
174
  if (width <= 0) return [""]; // guard, keeps the seam safe on tiny widths
150
175
 
151
- const theme = ctx.ui.theme; // per-render live read (single theme source)
152
- const state = getState(); // live getters re-invoked per run
153
- const config = getConfig();
154
- const glyphs = resolveGlyphs(config.icons.mode);
176
+ const theme = port.facts.theme(); // per-render live read (single theme source)
177
+ const config = port.facts.config();
155
178
  const segments = config.footerSegments;
156
- const meta = getModelMeta();
157
- const totals = getUsageTotals(ctx); // sync module cache, unchanged
179
+ const glyphs = resolveGlyphs(config.icons.mode);
158
180
 
159
- const line1 = yield* renderLine1(theme, state, config, glyphs, segments, width);
160
- const line2 = yield* renderLine2(theme, totals, meta, glyphs, segments, width);
161
- const mainLines = [line1, line2].map((line) =>
162
- truncateToWidth(line, width, theme.fg("dim", "...")),
163
- );
181
+ const line1 = composeLine(planFooterLine1(port, width), width, (s) => theme.fg("dim", s));
182
+ const line2 = composeLine(planFooterLine2(port, width), width, (s) => theme.fg("dim", s));
183
+ const mainLines = [line1, line2];
164
184
  if (!segments.extensionStatuses) return mainLines;
165
185
  const ext = yield* renderExtensionLines(
166
186
  theme,
167
- footerData.getExtensionStatuses(),
187
+ port.facts.extensionStatuses(),
168
188
  glyphs,
169
189
  width,
170
190
  );
@@ -187,9 +207,7 @@ export class FooterRenderService extends Context.Service<
187
207
  */
188
208
  export function installFooter(
189
209
  ctx: ExtensionContext,
190
- getState: () => FooterState,
191
- getConfig: () => TuiConfig,
192
- getModelMeta: () => ModelMeta,
210
+ port: ChromePort,
193
211
  hooks: FooterHooks,
194
212
  ): () => void {
195
213
  ctx.ui.setFooter((_tui, _theme, footerData) => {
@@ -199,13 +217,18 @@ export function installFooter(
199
217
  _tui.requestRender();
200
218
  });
201
219
 
202
- // Build the service layer and run construction synchronously at mount;
203
- // the render effect runs per invocation at the seam below.
220
+ // The mount supplies the live extension statuses; every other fact comes
221
+ // from the shared port.
204
222
  const svc = Context.get(
205
223
  Effect.runSync(
206
224
  Effect.context<FooterRenderService>().pipe(
207
225
  Effect.provide(
208
- FooterRenderService.make(ctx, getState, getConfig, getModelMeta, footerData),
226
+ FooterRenderService.make({
227
+ facts: {
228
+ ...port.facts,
229
+ extensionStatuses: () => footerData.getExtensionStatuses(),
230
+ },
231
+ }),
209
232
  ),
210
233
  ),
211
234
  ),