@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.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +63 -0
  3. package/package.json +80 -0
  4. package/src/autocomplete-contract.test.ts +126 -0
  5. package/src/autocomplete-contract.ts +91 -0
  6. package/src/commands/git-execution.test.ts +69 -0
  7. package/src/commands/git-execution.ts +198 -0
  8. package/src/commands/git-status.test.ts +379 -0
  9. package/src/commands/git-status.ts +320 -0
  10. package/src/components/bordered-box.test.ts +136 -0
  11. package/src/components/bordered-box.ts +106 -0
  12. package/src/components/define-component.ts +33 -0
  13. package/src/components/index.ts +35 -0
  14. package/src/config.test.ts +469 -0
  15. package/src/config.ts +307 -0
  16. package/src/frame/box-frame.test.ts +44 -0
  17. package/src/frame/box-frame.ts +160 -0
  18. package/src/icons.ts +203 -0
  19. package/src/index.ts +359 -0
  20. package/src/runtime.ts +256 -0
  21. package/src/session-lifecycle.ts +41 -0
  22. package/src/state.test.ts +7 -0
  23. package/src/state.ts +90 -0
  24. package/src/telemetry.ts +301 -0
  25. package/src/ui/editor-tint.test.ts +70 -0
  26. package/src/ui/editor-tint.ts +78 -0
  27. package/src/ui/editor.ts +403 -0
  28. package/src/ui/footer.test.ts +350 -0
  29. package/src/ui/footer.ts +225 -0
  30. package/src/ui/header.test.ts +260 -0
  31. package/src/ui/header.ts +165 -0
  32. package/src/ui/preview.test.ts +639 -0
  33. package/src/ui/preview.ts +374 -0
  34. package/src/ui/render-helpers.ts +275 -0
  35. package/src/ui/settings-command.test.ts +218 -0
  36. package/src/ui/settings-command.ts +390 -0
  37. package/src/utils.ts +448 -0
  38. package/src/vim/editor-close-autocomplete.ts +26 -0
  39. package/src/vim/editor-host.test.ts +340 -0
  40. package/src/vim/editor-state.test.ts +385 -0
  41. package/src/vim/editor-state.ts +314 -0
  42. package/src/vim/mode-indicator.test.ts +27 -0
  43. package/src/vim/mode-indicator.ts +44 -0
  44. package/src/vim/selection-render.test.ts +77 -0
  45. package/src/vim/selection-render.ts +85 -0
  46. package/src/vim/text-model.test.ts +85 -0
  47. package/src/vim/text-model.ts +75 -0
  48. package/src/vim/vim-router.test.ts +50 -0
  49. package/src/vim/vim-router.ts +55 -0
  50. package/src/vim/word-motion.ts +96 -0
@@ -0,0 +1,340 @@
1
+ import { afterEach, assert, it } from "@effect/vitest";
2
+ import { vi } from "vitest";
3
+ import { Effect } from "effect";
4
+ import {
5
+ CustomEditor,
6
+ type KeybindingsManager,
7
+ type ThemeColor,
8
+ } from "@earendil-works/pi-coding-agent";
9
+ import {
10
+ KeybindingsManager as TuiKeybindingsManager,
11
+ setKeybindings,
12
+ TUI_KEYBINDINGS,
13
+ type EditorTheme,
14
+ type TUI,
15
+ } from "@earendil-works/pi-tui";
16
+ import { TuiEditor, type TuiEditorOptions } from "../ui/editor.ts";
17
+ import { vimModeGlyph, vimModeTint } from "./mode-indicator.ts";
18
+ import {
19
+ EditorTintService,
20
+ EditorTintServiceDefault,
21
+ type EditorTintServiceHandle,
22
+ resetEditorTintProviders,
23
+ upsertBorderTintProvider,
24
+ } from "../ui/editor-tint.ts";
25
+ import { closeAutocomplete } from "./editor-close-autocomplete.ts";
26
+
27
+ const fakeEditorTheme: EditorTheme = {
28
+ borderColor: (s: string) => s,
29
+ selectList: {
30
+ selectedPrefix: (s: string) => s,
31
+ selectedText: (s: string) => s,
32
+ description: (s: string) => s,
33
+ scrollInfo: (s: string) => s,
34
+ noMatch: (s: string) => s,
35
+ },
36
+ };
37
+
38
+ const makeEditor = (options: TuiEditorOptions = {}): TuiEditor => {
39
+ const tui = {
40
+ terminal: { rows: 24, write() {} },
41
+ requestRender() {},
42
+ } as unknown as TUI;
43
+ // pi hands the editor its richer KeybindingsManager in production; a base
44
+ // pi-tui manager (with the same `matches` surface the tests exercise)
45
+ // suffices here.
46
+ const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
47
+ setKeybindings(kb);
48
+ return new TuiEditor(tui, fakeEditorTheme, kb, options);
49
+ };
50
+
51
+ /** Spy on the CustomEditor super-handler so we can observe delegation. */
52
+ const spyOnSuper = (): ReturnType<typeof vi.spyOn> =>
53
+ vi.spyOn(CustomEditor.prototype, "handleInput").mockImplementation(() => {});
54
+
55
+ const argsOf = (spy: ReturnType<typeof vi.spyOn>): string[] =>
56
+ spy.mock.calls.map((call: unknown[]) => call[0] as string);
57
+
58
+ afterEach(() => vi.restoreAllMocks());
59
+
60
+ // ---------------------------------------------------------------------------
61
+ // Gating (vim off ⇒ plain always-insert editor)
62
+ // ---------------------------------------------------------------------------
63
+
64
+ it("passes all input through unchanged when vim is off", () => {
65
+ const editor = makeEditor({ getVimEnabled: () => false });
66
+ const spy = spyOnSuper();
67
+ editor.handleInput("a");
68
+ editor.handleInput("\x1b");
69
+ editor.handleInput("\x1b[B");
70
+ assert.strictEqual(spy.mock.calls.length, 3);
71
+ assert.deepStrictEqual(argsOf(spy), ["a", "\x1b", "\x1b[B"]);
72
+ });
73
+
74
+ // ---------------------------------------------------------------------------
75
+ // Insert mode
76
+ // ---------------------------------------------------------------------------
77
+
78
+ it("inserts printable keys through super when vim is on", () => {
79
+ const editor = makeEditor({ getVimEnabled: () => true });
80
+ const spy = spyOnSuper();
81
+ editor.handleInput("h");
82
+ assert.strictEqual(spy.mock.calls.length, 1);
83
+ assert.deepStrictEqual(argsOf(spy), ["h"]);
84
+ assert.strictEqual((editor as unknown as { vimState: { mode: string } }).vimState.mode, "insert");
85
+ });
86
+
87
+ it("escape in insert enters normal and does not reach super", () => {
88
+ const editor = makeEditor({ getVimEnabled: () => true });
89
+ const spy = spyOnSuper();
90
+ editor.handleInput("\x1b");
91
+ assert.strictEqual(spy.mock.calls.length, 0);
92
+ assert.strictEqual((editor as unknown as { vimState: { mode: string } }).vimState.mode, "normal");
93
+ });
94
+
95
+ it("escape in insert closes an open autocomplete popup", () => {
96
+ const editor = makeEditor({ getVimEnabled: () => true });
97
+ // Simulate an open popup through the guarded private surface.
98
+ (editor as unknown as { autocompleteState: unknown }).autocompleteState = "manual";
99
+ assert.strictEqual(editor.isShowingAutocomplete(), true);
100
+ const spy = spyOnSuper();
101
+ editor.handleInput("\x1b");
102
+ assert.strictEqual(editor.isShowingAutocomplete(), false);
103
+ assert.strictEqual(spy.mock.calls.length, 0);
104
+ });
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // Normal mode
108
+ // ---------------------------------------------------------------------------
109
+
110
+ it("consumes vim motions and commands in normal mode without reaching super", () => {
111
+ const editor = makeEditor({ getVimEnabled: () => true });
112
+ editor.handleInput("\x1b"); // enter normal
113
+ const spy = spyOnSuper();
114
+ editor.handleInput("l");
115
+ editor.handleInput("w");
116
+ editor.handleInput("i");
117
+ assert.strictEqual(spy.mock.calls.length, 0);
118
+ });
119
+
120
+ it("escapes and control chords in normal mode are delegated to super", () => {
121
+ const editor = makeEditor({ getVimEnabled: () => true });
122
+ editor.handleInput("\x1b"); // enter normal
123
+ const spy = spyOnSuper();
124
+ editor.handleInput("\x1b"); // escape → app.interrupt (passed through)
125
+ editor.handleInput("\x1b[P"); // ctrl+p
126
+ editor.handleInput("\t"); // tab
127
+ assert.strictEqual(spy.mock.calls.length, 3);
128
+ });
129
+
130
+ it("is a plain always-insert editor when the getter is omitted", () => {
131
+ const editor = makeEditor();
132
+ const spy = spyOnSuper();
133
+ editor.handleInput("a");
134
+ assert.strictEqual(spy.mock.calls.length, 1);
135
+ assert.strictEqual((editor as unknown as { vimState: { mode: string } }).vimState.mode, "insert");
136
+ });
137
+
138
+ // ---------------------------------------------------------------------------
139
+ // closeAutocomplete (guarded pi internals)
140
+ // ---------------------------------------------------------------------------
141
+
142
+ interface PopupLike {
143
+ isShowingAutocomplete(): boolean;
144
+ cancelAutocomplete?(): void;
145
+ clearAutocompleteUi?(): void;
146
+ }
147
+
148
+ it("closes an open popup via the guarded internals", () => {
149
+ let closed = 0;
150
+ const editor: PopupLike = {
151
+ isShowingAutocomplete: () => true,
152
+ cancelAutocomplete: () => {
153
+ closed++;
154
+ },
155
+ };
156
+ closeAutocomplete(editor);
157
+ assert.strictEqual(closed, 1);
158
+ });
159
+
160
+ it("is a no-op when no popup is showing", () => {
161
+ let closed = 0;
162
+ const editor: PopupLike = {
163
+ isShowingAutocomplete: () => false,
164
+ cancelAutocomplete: () => {
165
+ closed++;
166
+ },
167
+ };
168
+ closeAutocomplete(editor);
169
+ assert.strictEqual(closed, 0);
170
+ });
171
+
172
+ it("degrades softly when the cancel method has been reshaped", () => {
173
+ let cleared = 0;
174
+ const editor: PopupLike = {
175
+ isShowingAutocomplete: () => true,
176
+ clearAutocompleteUi: () => {
177
+ cleared++;
178
+ },
179
+ };
180
+ closeAutocomplete(editor);
181
+ assert.strictEqual(cleared, 1);
182
+ });
183
+
184
+ // ---------------------------------------------------------------------------
185
+ // Editor border color (generic tint source + vim registration)
186
+ // ---------------------------------------------------------------------------
187
+
188
+ const makeBorderEditor = (
189
+ tint: (() => ThemeColor | undefined) | undefined,
190
+ extra: TuiEditorOptions = {},
191
+ ): TuiEditor => {
192
+ const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
193
+ const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
194
+ setKeybindings(kb);
195
+ // `borderColor` is identity in the fake theme, so a tint shows up as a
196
+ // literal `<color>:` prefix on the border tokens we assert on.
197
+ return new TuiEditor(tui, fakeEditorTheme, kb, {
198
+ tintPaint: (color, s) => `${color}:${s}`,
199
+ getBorderTint: tint,
200
+ ...extra,
201
+ });
202
+ };
203
+
204
+ const topBorderOf = (editor: TuiEditor): string => editor.render(40)[0] ?? "";
205
+
206
+ it("keeps pi's border color when no tint source is provided", () => {
207
+ const editor = makeBorderEditor(undefined);
208
+ assert.ok(topBorderOf(editor).includes("╭"));
209
+ assert.ok(!topBorderOf(editor).includes("accent:"));
210
+ });
211
+
212
+ it("keeps pi's border color when the tint source returns undefined", () => {
213
+ const editor = makeBorderEditor(() => undefined);
214
+ assert.ok(!topBorderOf(editor).includes("accent:"));
215
+ });
216
+
217
+ it("tints the border when a tint source returns a color", () => {
218
+ const editor = makeBorderEditor(() => "accent");
219
+ assert.ok(topBorderOf(editor).includes("accent:"));
220
+ });
221
+
222
+ it("tracks a changing tint source across renders", () => {
223
+ let tint: ThemeColor | undefined;
224
+ const editor = makeBorderEditor(() => tint);
225
+ assert.ok(!topBorderOf(editor).includes("accent:"));
226
+ tint = "accent";
227
+ assert.ok(topBorderOf(editor).includes("accent:"));
228
+ tint = undefined;
229
+ assert.ok(!topBorderOf(editor).includes("accent:"));
230
+ });
231
+
232
+ // ---------------------------------------------------------------------------
233
+ // vim border-tint registration lives in the editor, not the state machine
234
+ // ---------------------------------------------------------------------------
235
+
236
+ const resolveTint = (): EditorTintServiceHandle =>
237
+ Effect.runSync(
238
+ Effect.service(EditorTintService).pipe(Effect.provide(EditorTintServiceDefault)),
239
+ );
240
+
241
+ /**
242
+ * Mirror installEditor's wiring on a bare TuiEditor: ask the shared service
243
+ * for the tint and register vim's provider reading the live modal state.
244
+ */
245
+ const makeVimTintEditor = (getVimEnabled: () => boolean): TuiEditor => {
246
+ resetEditorTintProviders();
247
+ const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
248
+ const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
249
+ setKeybindings(kb);
250
+ const editor = new TuiEditor(tui, fakeEditorTheme, kb, {
251
+ getVimEnabled,
252
+ tintPaint: (color, s) => `${color}:${s}`,
253
+ getBorderTint: () => resolveTint().getTint(),
254
+ });
255
+ resolveTint().configure((current) =>
256
+ upsertBorderTintProvider(current, { id: "vim", getTint: () => vimModeTint(editor.vimState) }),
257
+ );
258
+ return editor;
259
+ };
260
+
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 editor = makeVimTintEditor(() => true);
283
+ editor.handleInput("\x1b"); // normal → vim tints syntaxOperator
284
+ assert.ok(topBorderOf(editor).includes("syntaxOperator:"));
285
+ // A later .configure from a different extension takes precedence.
286
+ resolveTint().configure((cur) =>
287
+ upsertBorderTintProvider(cur, { id: "my-extension", getTint: () => "error" }),
288
+ );
289
+ assert.ok(topBorderOf(editor).includes("error:"));
290
+ assert.ok(!topBorderOf(editor).includes("syntaxOperator:"));
291
+ });
292
+
293
+ // ---------------------------------------------------------------------------
294
+ // vim mode indicator (injectable top-border glyph)
295
+ // ---------------------------------------------------------------------------
296
+
297
+ /**
298
+ * Mirror installEditor's wiring on a bare TuiEditor: the editor asks the
299
+ * injected getter; installEditor supplies vim's live glyph reading the modal
300
+ * state.
301
+ */
302
+ const makeVimGlyphEditor = (getVimEnabled: () => boolean): TuiEditor => {
303
+ const tui = { terminal: { rows: 24, write() {} }, requestRender() {} } as unknown as TUI;
304
+ const kb = new TuiKeybindingsManager(TUI_KEYBINDINGS, {}) as unknown as KeybindingsManager;
305
+ setKeybindings(kb);
306
+ const editor = new TuiEditor(tui, fakeEditorTheme, kb, {
307
+ getVimEnabled,
308
+ getModeIndicator: () => vimModeGlyph(editor.vimState),
309
+ });
310
+ return editor;
311
+ };
312
+
313
+ it("keeps the border glyph-free when no indicator is injected", () => {
314
+ const editor = makeEditor({ getVimEnabled: () => true });
315
+ assert.ok(!topBorderOf(editor).includes("N"));
316
+ assert.ok(!topBorderOf(editor).includes("I"));
317
+ assert.ok(!topBorderOf(editor).includes("V"));
318
+ });
319
+
320
+ it("shows the insert letter by default and tracks mode transitions", () => {
321
+ const editor = makeVimGlyphEditor(() => true);
322
+ assert.ok(topBorderOf(editor).includes("I"));
323
+ editor.handleInput("\x1b"); // → normal
324
+ assert.ok(topBorderOf(editor).includes("N"));
325
+ assert.ok(!topBorderOf(editor).includes("I"));
326
+ editor.handleInput("v"); // → visual
327
+ assert.ok(topBorderOf(editor).includes("V"));
328
+ assert.ok(!topBorderOf(editor).includes("N"));
329
+ editor.handleInput("\x1b"); // → normal
330
+ editor.handleInput("i"); // → insert
331
+ assert.ok(topBorderOf(editor).includes("I"));
332
+ });
333
+
334
+ it("stays glyph-free when vim is off", () => {
335
+ const editor = makeVimGlyphEditor(() => false);
336
+ editor.handleInput("\x1b"); // gated; cannot leave insert
337
+ assert.ok(!topBorderOf(editor).includes("N"));
338
+ assert.ok(!topBorderOf(editor).includes("V"));
339
+ assert.ok(!topBorderOf(editor).includes("I"));
340
+ });