@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
@@ -1,19 +1,19 @@
1
- import { afterEach, assert, it } from "@effect/vitest";
2
- import { vi } from "vitest";
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 */
4
+ import { afterEach, assert, it, vi } from "@effect/vitest";
3
5
  import { Effect } from "effect";
6
+ import { CustomEditor, type ThemeColor } from "@earendil-works/pi-coding-agent";
4
7
  import {
5
- CustomEditor,
6
- type KeybindingsManager,
7
- type ThemeColor,
8
- } from "@earendil-works/pi-coding-agent";
9
- import {
10
- KeybindingsManager as TuiKeybindingsManager,
8
+ getKeybindings,
11
9
  setKeybindings,
12
- TUI_KEYBINDINGS,
10
+ type Color,
13
11
  type EditorTheme,
14
- type TUI,
15
12
  } from "@earendil-works/pi-tui";
16
- import { TuiEditor, type TuiEditorOptions } from "../ui/editor.ts";
13
+ import { TuiEditor, installEditor, planBorder, type TuiEditorOptions } from "../ui/editor.ts";
14
+ import { fakeKeybindings, fakeTui } from "../../test-support/doubles.ts";
15
+ import { themeDouble } from "@ftrdotdev/pi-test-kit";
16
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
17
17
  import { vimModeGlyph, vimModeTint } from "./mode-indicator.ts";
18
18
  import {
19
19
  EditorTintService,
@@ -21,6 +21,7 @@ import {
21
21
  upsertBorderTintProvider,
22
22
  } from "../ui/editor-tint.ts";
23
23
  import { closeAutocomplete } from "./editor-close-autocomplete.ts";
24
+ import { resolveGlyphs } from "../icons.ts";
24
25
 
25
26
  const fakeEditorTheme: EditorTheme = {
26
27
  borderColor: (s: string) => s,
@@ -34,14 +35,8 @@ const fakeEditorTheme: EditorTheme = {
34
35
  };
35
36
 
36
37
  const makeEditor = (options: TuiEditorOptions = {}): TuiEditor => {
37
- const tui = {
38
- terminal: { rows: 24, write() {} },
39
- requestRender() {},
40
- } as unknown as TUI;
41
- // pi hands the editor its richer KeybindingsManager in production; a base
42
- // pi-tui manager (with the same `matches` surface the tests exercise)
43
- // suffices here.
44
- const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
38
+ const tui = fakeTui();
39
+ const kb = fakeKeybindings();
45
40
  setKeybindings(kb);
46
41
  return new TuiEditor(tui, fakeEditorTheme, kb, options);
47
42
  };
@@ -53,7 +48,13 @@ const spyOnSuper = (): ReturnType<typeof vi.spyOn> =>
53
48
  const argsOf = (spy: ReturnType<typeof vi.spyOn>): string[] =>
54
49
  spy.mock.calls.map((call: unknown[]) => call[0] as string);
55
50
 
56
- afterEach(() => vi.restoreAllMocks());
51
+ /** The super-handler the runner started with; a spy must not outlive its test. */
52
+ // oxlint-disable-next-line typescript/unbound-method -- identity capture only.
53
+ const REAL_HANDLE_INPUT = CustomEditor.prototype.handleInput;
54
+
55
+ /** The keybindings global lives inside the dependency; every test puts it back. */
56
+ const ORIGINAL_KEYBINDINGS = getKeybindings();
57
+ afterEach(() => setKeybindings(ORIGINAL_KEYBINDINGS));
57
58
 
58
59
  // ---------------------------------------------------------------------------
59
60
  // Gating (vim off ⇒ plain always-insert editor)
@@ -183,18 +184,35 @@ it("degrades softly when the cancel method has been reshaped", () => {
183
184
  // Editor border color (generic tint source + vim registration)
184
185
  // ---------------------------------------------------------------------------
185
186
 
187
+ /**
188
+ * Render a tint color as a stable token. Theme colors are strings and pass
189
+ * through; a concrete `Color` is an object, so printing it directly would
190
+ * yield `[object Object]` — name its kind and channels instead.
191
+ */
192
+ const tintLabel = (color: ThemeColor | Color): string => {
193
+ if (typeof color === "string") return color;
194
+ switch (color.kind) {
195
+ case "indexed":
196
+ return `indexed(${color.index})`;
197
+ case "rgb":
198
+ return `rgb(${color.r},${color.g},${color.b})`;
199
+ case "oklch":
200
+ return `oklch(${color.l},${color.c},${color.h})`;
201
+ }
202
+ };
203
+
186
204
  const makeBorderEditor = (
187
205
  tint: (() => ThemeColor | undefined) | undefined,
188
206
  extra: TuiEditorOptions = {},
189
207
  ): TuiEditor => {
190
- const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
191
- const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
208
+ const tui = fakeTui();
209
+ const kb = fakeKeybindings();
192
210
  setKeybindings(kb);
193
211
  // `borderColor` is identity in the fake theme, so a tint shows up as a
194
212
  // literal `<color>:` prefix on the border tokens we assert on.
195
213
  return new TuiEditor(tui, fakeEditorTheme, kb, {
196
- tintPaint: (color, s) => `${color}:${s}`,
197
- getBorderTint: tint,
214
+ tintPaint: (color, s) => `${tintLabel(color)}:${s}`,
215
+ ...(tint === undefined ? {} : { getBorderTint: tint }),
198
216
  ...extra,
199
217
  });
200
218
  };
@@ -231,25 +249,16 @@ it("tracks a changing tint source across renders", () => {
231
249
  // vim border-tint registration lives in the editor, not the state machine
232
250
  // ---------------------------------------------------------------------------
233
251
 
234
- const resolveTint = (): EditorTintServiceHandle =>
235
- Effect.runSync(Effect.service(EditorTintService).pipe(Effect.provide(EditorTintService.layer)));
236
-
237
- /**
238
- * Mirror installEditor's wiring on a bare TuiEditor. installEditor receives a
239
- * single resolved service handle and uses it for both the editor's border
240
- * getter and vim's provider registration; a per-test handle is passed in (or
241
- * freshly resolved) so both closures see the same provider registry.
242
- */
243
252
  const makeVimTintEditor = (
244
253
  getVimEnabled: () => boolean,
245
- tint: EditorTintServiceHandle = resolveTint(),
254
+ tint: EditorTintServiceHandle,
246
255
  ): TuiEditor => {
247
- const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
248
- const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
256
+ const tui = fakeTui();
257
+ const kb = fakeKeybindings();
249
258
  setKeybindings(kb);
250
259
  const editor = new TuiEditor(tui, fakeEditorTheme, kb, {
251
260
  getVimEnabled,
252
- tintPaint: (color, s) => `${color}:${s}`,
261
+ tintPaint: (color, s) => `${tintLabel(color)}:${s}`,
253
262
  getBorderTint: () => tint.getTint(),
254
263
  });
255
264
  tint.configure((current) =>
@@ -258,38 +267,46 @@ const makeVimTintEditor = (
258
267
  return editor;
259
268
  };
260
269
 
261
- it("vim registers a live provider so the border tracks normal/visual/insert", () => {
262
- const editor = makeVimTintEditor(() => true);
263
- assert.ok(!topBorderOf(editor).includes("syntaxOperator:")); // insert
264
- editor.handleInput("\x1b"); // → normal
265
- assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
266
- editor.handleInput("v"); // → visual
267
- assert.ok(topBorderOf(editor).includes("syntaxNumber:"));
268
- assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
269
- editor.handleInput("\x1b"); // → normal
270
- assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
271
- editor.handleInput("i"); // → insert
272
- assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
273
- });
274
-
275
- it("vim stays untinted when the toggle is off", () => {
276
- const editor = makeVimTintEditor(() => false);
277
- editor.handleInput("\x1b"); // even an escape cannot leave insert gated off
278
- assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
279
- });
280
-
281
- it("another extension can override vim's tint through configure", () => {
282
- const tint = resolveTint();
283
- const editor = makeVimTintEditor(() => true, tint);
284
- editor.handleInput("\x1b"); // normal → vim tints syntaxOperator
285
- assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
286
- // A later .configure from a different extension takes precedence.
287
- tint.configure((cur) =>
288
- upsertBorderTintProvider(cur, { id: "my-extension", getTint: () => "error" }),
289
- );
290
- assert.ok(topBorderOf(editor).includes("error:"));
291
- assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
292
- });
270
+ it.effect("vim registers a live provider so the border tracks normal/visual/insert", () =>
271
+ Effect.gen(function* () {
272
+ const enabledTint = yield* EditorTintService;
273
+ const editor = makeVimTintEditor(() => true, enabledTint);
274
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:")); // insert
275
+ editor.handleInput("\x1b"); // → normal
276
+ assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
277
+ editor.handleInput("v"); // → visual
278
+ assert.ok(topBorderOf(editor).includes("syntaxNumber:"));
279
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
280
+ editor.handleInput("\x1b"); // → normal
281
+ assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
282
+ editor.handleInput("i"); // → insert
283
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
284
+ }).pipe(Effect.provide(EditorTintService.layer)),
285
+ );
286
+
287
+ it.effect("vim stays untinted when the toggle is off", () =>
288
+ Effect.gen(function* () {
289
+ const disabledTint = yield* EditorTintService;
290
+ const editor = makeVimTintEditor(() => false, disabledTint);
291
+ editor.handleInput("\x1b"); // even an escape cannot leave insert gated off
292
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
293
+ }).pipe(Effect.provide(EditorTintService.layer)),
294
+ );
295
+
296
+ it.effect("another extension can override vim's tint through configure", () =>
297
+ Effect.gen(function* () {
298
+ const tint = yield* EditorTintService;
299
+ const editor = makeVimTintEditor(() => true, tint);
300
+ editor.handleInput("\x1b"); // normal → vim tints syntaxOperator
301
+ assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
302
+ // A later .configure from a different extension takes precedence.
303
+ tint.configure((cur) =>
304
+ upsertBorderTintProvider(cur, { id: "my-extension", getTint: () => "error" }),
305
+ );
306
+ assert.ok(topBorderOf(editor).includes("error:"));
307
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
308
+ }).pipe(Effect.provide(EditorTintService.layer)),
309
+ );
293
310
 
294
311
  // ---------------------------------------------------------------------------
295
312
  // vim mode indicator (injectable top-border glyph)
@@ -301,16 +318,98 @@ it("another extension can override vim's tint through configure", () => {
301
318
  * state.
302
319
  */
303
320
  const makeVimGlyphEditor = (getVimEnabled: () => boolean): TuiEditor => {
304
- const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
305
- const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
321
+ const tui = fakeTui();
322
+ const kb = fakeKeybindings();
306
323
  setKeybindings(kb);
307
324
  const editor = new TuiEditor(tui, fakeEditorTheme, kb, {
308
325
  getVimEnabled,
309
- getModeIndicator: () => vimModeGlyph(editor.vimState),
326
+ getModeIndicator: () => vimModeGlyph(editor.vimState, resolveGlyphs("ascii")),
310
327
  });
311
328
  return editor;
312
329
  };
313
330
 
331
+ it("the border composes work-state \u2192 vim glyph \u2192 scroll hint in declared order", () => {
332
+ // Act: plan the three declared facts at a wide frame width.
333
+ const wide = planBorder(
334
+ { statusText: "Thinking\u2026", vimGlyph: "N", scrollHint: "\u2193 3 more" },
335
+ 80,
336
+ );
337
+
338
+ // Assert: all three kept, in declaration order.
339
+ assert.deepStrictEqual(
340
+ wide.filter((decision) => decision.action !== "drop").map((decision) => decision.id),
341
+ ["editorStatus", "vimMode", "scrollHint"],
342
+ );
343
+
344
+ // Arrange: at 40 the frame's 38-column inner width is already narrow, so
345
+ // the cap keeps only the highest-priority fact that reaches its floor.
346
+ const narrowFrame = planBorder(
347
+ { statusText: "Thinking\u2026", vimGlyph: "N", scrollHint: "\u2193 3 more" },
348
+ 40,
349
+ );
350
+
351
+ // Assert: the status outranks the glyph and the hint.
352
+ assert.deepStrictEqual(
353
+ narrowFrame.filter((decision) => decision.action !== "drop").map((decision) => decision.id),
354
+ ["editorStatus"],
355
+ );
356
+
357
+ // Arrange: below the narrow threshold the cap keeps one field per line; the
358
+ // one-column spinner is below its declared floor, so the vim glyph survives.
359
+ const narrow = planBorder(
360
+ { statusText: "\u280b", vimGlyph: "N", scrollHint: "\u2193 3 more" },
361
+ 14,
362
+ );
363
+
364
+ // Assert: only the highest-priority fact that reaches its floor is kept.
365
+ assert.deepStrictEqual(
366
+ narrow.filter((decision) => decision.action !== "drop").map((decision) => decision.id),
367
+ ["vimMode"],
368
+ );
369
+
370
+ // Arrange: at 8 columns the frame has 6 inner columns; the one-column vim
371
+ // glyph still reaches its floor.
372
+ const narrowest = planBorder(
373
+ { statusText: "\u280b", vimGlyph: "N", scrollHint: "\u2193 3 more" },
374
+ 8,
375
+ );
376
+
377
+ // Assert: the vim glyph is the only survivor at the narrowest frame.
378
+ assert.deepStrictEqual(
379
+ narrowest.filter((decision) => decision.action !== "drop").map((decision) => decision.id),
380
+ ["vimMode"],
381
+ );
382
+ });
383
+
384
+ it("installEditor passes the configured icon mode through", () => {
385
+ // Arrange: a mount context that captures the editor factory.
386
+ const stubGlyphs = { vimNormal: "<N>", vimVisual: "<V>", vimInsert: "<I>" };
387
+ const plainTheme = themeDouble();
388
+ let captured: ((tui: unknown, theme: unknown, kb: unknown) => TuiEditor) | undefined;
389
+ const ctx = {
390
+ ui: {
391
+ theme: plainTheme,
392
+ notify: () => {},
393
+ setEditorComponent(factory: unknown) {
394
+ captured = factory as typeof captured;
395
+ },
396
+ },
397
+ } as unknown as ExtensionContext;
398
+
399
+ installEditor(undefined as never, ctx, {
400
+ getVimEnabled: () => true,
401
+ getGlyphs: () => stubGlyphs,
402
+ tint: { configure: () => {}, getTint: () => undefined },
403
+ });
404
+
405
+ // Act: render the captured editor in insert mode.
406
+ const editor = captured!(fakeTui(), fakeEditorTheme, fakeKeybindings());
407
+ const top = editor.render(40)[0] ?? "";
408
+
409
+ // Assert: the top border carries the supplied insert glyph.
410
+ assert.ok(top.includes(stubGlyphs.vimInsert));
411
+ });
412
+
314
413
  it("keeps the border glyph-free when no indicator is injected", () => {
315
414
  const editor = makeEditor({ getVimEnabled: () => true });
316
415
  assert.ok(!topBorderOf(editor).includes("N"));
@@ -318,7 +417,7 @@ it("keeps the border glyph-free when no indicator is injected", () => {
318
417
  assert.ok(!topBorderOf(editor).includes("V"));
319
418
  });
320
419
 
321
- it("shows the insert letter by default and tracks mode transitions", () => {
420
+ it("shows the insert glyph by default and tracks mode transitions", () => {
322
421
  const editor = makeVimGlyphEditor(() => true);
323
422
  assert.ok(topBorderOf(editor).includes("I"));
324
423
  editor.handleInput("\x1b"); // → normal
@@ -358,12 +457,27 @@ it("renders an embedded status indicator in the top border", () => {
358
457
  assert.ok(topBorderOf(editor).includes("Working"));
359
458
  });
360
459
 
361
- it("keeps the status spinner when the message does not fit the border", () => {
460
+ it("drops the status spinner when it cannot reach its declared floor", () => {
362
461
  const editor = makeEditor();
363
462
  editor.setWorkingStatusIndicator(fakeStatus("⠋ Working on a long task", "⠋"));
364
463
  const top = editor.render(14)[0] ?? "";
365
- assert.ok(top.includes("⠋"));
366
464
  assert.ok(!top.includes("long task"));
465
+ assert.ok(!top.includes("⠋"));
466
+ assert.ok(top.startsWith("╭"));
467
+ });
468
+
469
+ it("keeps the vim glyph and a labelled frame at 8 columns", () => {
470
+ // Arrange: vim normal mode and a status spinner below its declared floor.
471
+ const editor = makeVimGlyphEditor(() => true);
472
+ editor.handleInput("\x1b"); // insert → normal
473
+ editor.setWorkingStatusIndicator(fakeStatus("⠋ Working", "⠋"));
474
+
475
+ // Act: render at the narrowest frame width.
476
+ const top = editor.render(8)[0] ?? "";
477
+
478
+ // Assert: the vim glyph survives and the frame still carries a label.
479
+ assert.ok(top.includes("N"));
480
+ assert.ok(top.startsWith("╭───"));
367
481
  });
368
482
 
369
483
  it("returns to the plain border after the status indicator clears", () => {
@@ -373,3 +487,15 @@ it("returns to the plain border after the status indicator clears", () => {
373
487
  editor.setWorkingStatusIndicator(undefined);
374
488
  assert.ok(!topBorderOf(editor).includes("Working"));
375
489
  });
490
+
491
+ it("the super-handler spy does not outlive the test that made it", () => {
492
+ // oxlint-disable typescript/unbound-method -- identity comparison only: the
493
+ // policy under test is whether the runner put the original method back.
494
+ assert.strictEqual(vi.isMockFunction(CustomEditor.prototype.handleInput), false);
495
+ assert.strictEqual(CustomEditor.prototype.handleInput, REAL_HANDLE_INPUT);
496
+ // oxlint-enable typescript/unbound-method
497
+ });
498
+
499
+ it("the keybindings global is restored after each test", () => {
500
+ assert.strictEqual(getKeybindings(), ORIGINAL_KEYBINDINGS);
501
+ });
@@ -179,7 +179,7 @@ export class VimEditorState {
179
179
  }
180
180
  if (printable === "d" || printable === "c" || printable === "y") {
181
181
  const sel = this.selection;
182
- if (sel) {
182
+ if (sel !== undefined) {
183
183
  if (printable === "d") {
184
184
  this.yankBuffer = this.model.yankRange(sel);
185
185
  this.model.deleteRange(sel);
@@ -196,7 +196,7 @@ export class VimEditorState {
196
196
  }
197
197
  if (MOTIONS.has(printable)) {
198
198
  this.moveCursorByMotion(printable);
199
- if (this.selection) {
199
+ if (this.selection !== undefined) {
200
200
  // Extend the selection: the anchor stays fixed, the active end moves.
201
201
  this.selection = makeRange(this.selection.anchor, this.model.getCursor());
202
202
  }
@@ -19,9 +19,10 @@ it("normal tints syntaxOperator; visual tints syntaxNumber", () => {
19
19
  // vimModeGlyph (pure mode → top-border glyph mapping)
20
20
  // ---------------------------------------------------------------------------
21
21
 
22
- it("maps modes to single letters; vim off yields nothing", () => {
23
- assert.strictEqual(vimModeGlyph({ enabled: true, mode: "insert" }), "I");
24
- assert.strictEqual(vimModeGlyph({ enabled: true, mode: "normal" }), "N");
25
- assert.strictEqual(vimModeGlyph({ enabled: true, mode: "visual" }), "V");
26
- assert.strictEqual(vimModeGlyph({ enabled: false, mode: "normal" }), undefined);
22
+ it("maps modes through the supplied glyph set; vim off yields nothing", () => {
23
+ const glyphs = { vimNormal: "<N>", vimVisual: "<V>", vimInsert: "<I>" };
24
+ assert.strictEqual(vimModeGlyph({ enabled: true, mode: "insert" }, glyphs), "<I>");
25
+ assert.strictEqual(vimModeGlyph({ enabled: true, mode: "normal" }, glyphs), "<N>");
26
+ assert.strictEqual(vimModeGlyph({ enabled: true, mode: "visual" }, glyphs), "<V>");
27
+ assert.strictEqual(vimModeGlyph({ enabled: false, mode: "normal" }, glyphs), undefined);
27
28
  });
@@ -1,4 +1,5 @@
1
1
  import type { ThemeColor } from "@earendil-works/pi-coding-agent";
2
+ import type { IconGlyphs } from "../icons.ts";
2
3
  import type { EditorMode } from "./editor-state.ts";
3
4
 
4
5
  /**
@@ -30,15 +31,18 @@ export function vimModeTint(state: VimModeState): ThemeColor | undefined {
30
31
  }
31
32
 
32
33
  /**
33
- * vim's mode indicator for the current modal state: a single letter on the
34
- * editor's top border (N normal, V visual, I insert); vim-off yields
35
- * nothing. Injectable via `TuiEditorOptions.getModeIndicator` — the editor
36
- * never knows vim exists, installEditor supplies the live glyph reading the
37
- * modal state.
34
+ * vim's mode indicator for the current modal state: the resolved glyph table's
35
+ * `vimNormal`/`vimVisual`/`vimInsert` entry on the editor's top border; vim-off
36
+ * yields nothing. Injectable via `TuiEditorOptions.getModeIndicator` — the
37
+ * editor never knows vim exists, installEditor supplies the live glyph reading
38
+ * the modal state and the configured icon mode.
38
39
  */
39
- export function vimModeGlyph(state: VimModeState): string | undefined {
40
+ export function vimModeGlyph(
41
+ state: VimModeState,
42
+ glyphs: Pick<IconGlyphs, "vimNormal" | "vimVisual" | "vimInsert">,
43
+ ): string | undefined {
40
44
  if (!state.enabled) return undefined;
41
- if (state.mode === "normal") return "N";
42
- if (state.mode === "visual") return "V";
43
- return "I";
45
+ if (state.mode === "normal") return glyphs.vimNormal;
46
+ if (state.mode === "visual") return glyphs.vimVisual;
47
+ return glyphs.vimInsert;
44
48
  }
@@ -15,12 +15,16 @@ import type { Range } from "./text-model.ts";
15
15
  /** Length of the escape sequence starting at `i`, or 0 when not an escape. */
16
16
  function escapeLength(line: string, i: number): number {
17
17
  const rest = line.slice(i);
18
+ // The escape patterns below match ESC/BEL by design.
19
+ // eslint-disable-next-line no-control-regex
18
20
  const csi = rest.match(/^\x1b\[[0-9;?]*[A-Za-z]/);
19
- if (csi) return csi[0].length;
21
+ if (csi !== null) return csi[0].length;
22
+ // eslint-disable-next-line no-control-regex
20
23
  const osc = rest.match(/^\x1b\][^\x07]*(?:\x07|\x1b\\)/);
21
- if (osc) return osc[0].length;
24
+ if (osc !== null) return osc[0].length;
25
+ // eslint-disable-next-line no-control-regex
22
26
  const oscu = rest.match(/^\x1b_[^\x07]*(?:\x07|\x1b\\)/);
23
- if (oscu) return oscu[0].length;
27
+ if (oscu !== null) return oscu[0].length;
24
28
  return 0;
25
29
  }
26
30
 
@@ -74,7 +78,7 @@ export interface SelectionRenderInput {
74
78
  }
75
79
 
76
80
  export function renderSelection({ lines, selection, paint }: SelectionRenderInput): string[] {
77
- if (!selection) return lines; // inert with no selection
81
+ if (selection === undefined) return lines; // inert with no selection
78
82
  const { start, end } = selection;
79
83
  return lines.map((line, i) => {
80
84
  if (i < start.line || i > end.line) return line; // outside the range
@@ -1,5 +1,10 @@
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";
5
+ import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
2
6
  import { Effect, Layer } from "effect";
7
+ import { rangeEndpointsArbitrary, wordCursorArbitrary } from "../../test-support/generators.ts";
3
8
  import { makeRange, type VimTextModelShape, VimTextModel } from "./text-model.ts";
4
9
  import { findWordBackStart, findWordEnd, findWordStart, wordForward } from "./word-motion.ts";
5
10
 
@@ -7,20 +12,6 @@ import { findWordBackStart, findWordEnd, findWordStart, wordForward } from "./wo
7
12
  // Range normalization (binding decision 3)
8
13
  // ---------------------------------------------------------------------------
9
14
 
10
- it("makeRange keeps anchor and active and normalizes start/end", () => {
11
- const r = makeRange({ line: 0, col: 1 }, { line: 0, col: 3 });
12
- assert.deepStrictEqual(r.anchor, { line: 0, col: 1 });
13
- assert.deepStrictEqual(r.active, { line: 0, col: 3 });
14
- assert.deepStrictEqual(r.start, { line: 0, col: 1 });
15
- assert.deepStrictEqual(r.end, { line: 0, col: 3 });
16
- });
17
-
18
- it("makeRange orders multi-line ranges by line then column", () => {
19
- const r = makeRange({ line: 2, col: 0 }, { line: 1, col: 5 });
20
- assert.deepStrictEqual(r.start, { line: 1, col: 5 });
21
- assert.deepStrictEqual(r.end, { line: 2, col: 0 });
22
- });
23
-
24
15
  // ---------------------------------------------------------------------------
25
16
  // Word-motion geometry
26
17
  // ---------------------------------------------------------------------------
@@ -83,3 +74,50 @@ it.effect("VimTextModel.layerFrom provides the shape through the service tag", (
83
74
  assert.strictEqual(model.getText(), "hello");
84
75
  }).pipe(Effect.provide(VimTextModel.layerFrom(noopShape) as Layer.Layer<VimTextModel>)),
85
76
  );
77
+
78
+ it.prop(
79
+ "makeRange normalizes start and end whatever the endpoint order",
80
+ { endpoints: rangeEndpointsArbitrary },
81
+ ({ endpoints }) => {
82
+ const range = makeRange(endpoints.anchor, endpoints.active);
83
+ const before = (left: { line: number; col: number }, right: { line: number; col: number }) =>
84
+ left.line < right.line || (left.line === right.line && left.col <= right.col);
85
+ assert(before(range.start, range.end));
86
+ const swapped = makeRange(endpoints.active, endpoints.anchor);
87
+ assert.deepStrictEqual(swapped.start, range.start);
88
+ assert.deepStrictEqual(swapped.end, range.end);
89
+ assert.deepStrictEqual(range.anchor, endpoints.anchor);
90
+ assert.deepStrictEqual(range.active, endpoints.active);
91
+ },
92
+ { arbitrary: { runs: PROPERTY_RUNS } },
93
+ );
94
+
95
+ it.prop(
96
+ "word motion returns an index inside the line",
97
+ { cursor: wordCursorArbitrary },
98
+ ({ cursor }) => {
99
+ const line = cursor.lines[cursor.cursor.line]!;
100
+ for (const index of [
101
+ findWordStart(line, cursor.cursor.col),
102
+ findWordBackStart(line, cursor.cursor.col),
103
+ findWordEnd(line, cursor.cursor.col),
104
+ ]) {
105
+ assert.isAtLeast(index, 0);
106
+ assert.isAtMost(index, line.length);
107
+ }
108
+ },
109
+ { arbitrary: { runs: PROPERTY_RUNS } },
110
+ );
111
+
112
+ it.prop(
113
+ "wordForward never moves backwards",
114
+ { cursor: wordCursorArbitrary },
115
+ ({ cursor }) => {
116
+ const next = wordForward(cursor.lines, cursor.cursor);
117
+ assert(
118
+ next.line > cursor.cursor.line ||
119
+ (next.line === cursor.cursor.line && next.col >= cursor.cursor.col),
120
+ );
121
+ },
122
+ { arbitrary: { runs: PROPERTY_RUNS } },
123
+ );
@@ -67,7 +67,7 @@ export interface VimTextModelShape {
67
67
  * through the session's effect context.
68
68
  */
69
69
  export class VimTextModel extends Context.Service<VimTextModel, VimTextModelShape>()(
70
- "tui/vim/VimTextModel",
70
+ "@ftrdotdev/pi-tui/vim/text-model/VimTextModel",
71
71
  ) {
72
72
  static layerFrom(model: VimTextModelShape): Layer.Layer<VimTextModel> {
73
73
  return Layer.succeed(VimTextModel, VimTextModel.of(model));
@@ -1,3 +1,6 @@
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 { VimRouter } from "./vim-router.ts";
@@ -31,7 +31,7 @@ export type NavigationIntent = {
31
31
  export class VimRouter extends Context.Service<
32
32
  VimRouter,
33
33
  { readonly decodeNavigation: (data: string) => NavigationIntent | undefined }
34
- >()("tui/vim/VimRouter") {
34
+ >()("@ftrdotdev/pi-tui/vim/vim-router/VimRouter") {
35
35
  static make(getVimEnabled: () => boolean): Layer.Layer<VimRouter> {
36
36
  return Layer.succeed(
37
37
  VimRouter,