@ftrdotdev/pi-tui 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +80 -0
- package/src/autocomplete-contract.test.ts +126 -0
- package/src/autocomplete-contract.ts +91 -0
- package/src/commands/git-execution.test.ts +69 -0
- package/src/commands/git-execution.ts +198 -0
- package/src/commands/git-status.test.ts +379 -0
- package/src/commands/git-status.ts +320 -0
- package/src/components/bordered-box.test.ts +136 -0
- package/src/components/bordered-box.ts +106 -0
- package/src/components/define-component.ts +33 -0
- package/src/components/index.ts +35 -0
- package/src/config.test.ts +469 -0
- package/src/config.ts +307 -0
- package/src/frame/box-frame.test.ts +44 -0
- package/src/frame/box-frame.ts +160 -0
- package/src/icons.ts +203 -0
- package/src/index.ts +359 -0
- package/src/runtime.ts +256 -0
- package/src/session-lifecycle.ts +41 -0
- package/src/state.test.ts +7 -0
- package/src/state.ts +90 -0
- package/src/telemetry.ts +301 -0
- package/src/ui/editor-tint.test.ts +70 -0
- package/src/ui/editor-tint.ts +78 -0
- package/src/ui/editor.ts +403 -0
- package/src/ui/footer.test.ts +350 -0
- package/src/ui/footer.ts +225 -0
- package/src/ui/header.test.ts +260 -0
- package/src/ui/header.ts +165 -0
- package/src/ui/preview.test.ts +639 -0
- package/src/ui/preview.ts +374 -0
- package/src/ui/render-helpers.ts +275 -0
- package/src/ui/settings-command.test.ts +218 -0
- package/src/ui/settings-command.ts +390 -0
- package/src/utils.ts +448 -0
- package/src/vim/editor-close-autocomplete.ts +26 -0
- package/src/vim/editor-host.test.ts +340 -0
- package/src/vim/editor-state.test.ts +385 -0
- package/src/vim/editor-state.ts +314 -0
- package/src/vim/mode-indicator.test.ts +27 -0
- package/src/vim/mode-indicator.ts +44 -0
- package/src/vim/selection-render.test.ts +77 -0
- package/src/vim/selection-render.ts +85 -0
- package/src/vim/text-model.test.ts +85 -0
- package/src/vim/text-model.ts +75 -0
- package/src/vim/vim-router.test.ts +50 -0
- package/src/vim/vim-router.ts +55 -0
- package/src/vim/word-motion.ts +96 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
4
|
+
import { makeBorderedBox } from "./bordered-box.ts";
|
|
5
|
+
|
|
6
|
+
// A body component that renders exactly the given lines (Text pads/wraps).
|
|
7
|
+
const lines = (content: string[]): Component => ({
|
|
8
|
+
render: () => content,
|
|
9
|
+
invalidate: () => {},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Identity theme for structural assertions.
|
|
13
|
+
const plainTheme = {
|
|
14
|
+
fg: (_color: string, text: string) => text,
|
|
15
|
+
bg: (_color: string, text: string) => text,
|
|
16
|
+
bold: (text: string) => text,
|
|
17
|
+
} as unknown as Theme;
|
|
18
|
+
|
|
19
|
+
// Marker theme for color and background assertions.
|
|
20
|
+
const markerTheme = {
|
|
21
|
+
fg: (color: string, text: string) => `<${color}>${text}</${color}>`,
|
|
22
|
+
bg: (color: string, text: string) => `[${color}]${text}[/${color}]`,
|
|
23
|
+
bold: (text: string) => text,
|
|
24
|
+
} as unknown as Theme;
|
|
25
|
+
|
|
26
|
+
it("renders top and bottom borders with the content rail-wrapped between them", () => {
|
|
27
|
+
const out = makeBorderedBox(lines(["hi"]), plainTheme).render(12);
|
|
28
|
+
assert.deepStrictEqual(out, ["╭──────────╮", "│ hi │", "╰──────────╯"]);
|
|
29
|
+
for (const line of out) assert.strictEqual(line.length, 12);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("embeds the label in the top border when there is room", () => {
|
|
33
|
+
const out = makeBorderedBox(lines(["hi"]), plainTheme, { label: "hello" }).render(40);
|
|
34
|
+
assert.strictEqual(out[0]!.includes("─── hello ─────"), true);
|
|
35
|
+
assert.strictEqual(out[0]!.startsWith("╭"), true);
|
|
36
|
+
assert.strictEqual(out[0]!.endsWith("╮"), true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("omits the label when the box is too narrow", () => {
|
|
40
|
+
const out = makeBorderedBox(lines(["hi"]), plainTheme, { label: "hello" }).render(7);
|
|
41
|
+
assert.strictEqual(out[0], "╭─────╮");
|
|
42
|
+
assert.strictEqual(out[0]!.includes("hello"), false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("truncates long body lines to the content width", () => {
|
|
46
|
+
const out = makeBorderedBox(lines(["abcdefghij"]), plainTheme).render(8);
|
|
47
|
+
// truncateToWidth appends an SGR reset after the cut (display width 0).
|
|
48
|
+
assert.deepStrictEqual(out, ["╭──────╮", "│ abcd\x1b[0m │", "╰──────╯"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("applies the background function to every rendered line", () => {
|
|
52
|
+
const bg = (s: string) => `[customMessageBg]${s}[/customMessageBg]`;
|
|
53
|
+
const out = makeBorderedBox(lines(["hi"]), markerTheme, { bg }).render(12);
|
|
54
|
+
assert.strictEqual(out.length, 3);
|
|
55
|
+
for (const line of out) {
|
|
56
|
+
assert.strictEqual(line.startsWith("[customMessageBg]"), true);
|
|
57
|
+
assert.strictEqual(line.endsWith("[/customMessageBg]"), true);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("paints the border with the configured color token", () => {
|
|
62
|
+
const out = makeBorderedBox(lines(["hi"]), markerTheme, { color: "warning" }).render(12);
|
|
63
|
+
assert.strictEqual(out[0]!.includes("<warning>╭"), true);
|
|
64
|
+
assert.strictEqual(out[0]!.includes("<accent>"), false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("pads the content by the configured padding", () => {
|
|
68
|
+
const out = makeBorderedBox(lines(["hi"]), plainTheme, { paddingX: 2 }).render(12);
|
|
69
|
+
assert.deepStrictEqual(out, ["╭──────────╮", "│ hi │", "╰──────────╯"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("renders nothing when the width is too small for a border", () => {
|
|
73
|
+
assert.deepStrictEqual(makeBorderedBox(lines(["hi"]), plainTheme).render(2), []);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("invalidates the body", () => {
|
|
77
|
+
const body = lines(["hi"]);
|
|
78
|
+
const box = makeBorderedBox(body, plainTheme);
|
|
79
|
+
box.invalidate();
|
|
80
|
+
// invalidate is a no-op for the fake body; assert it runs without error
|
|
81
|
+
assert.strictEqual(typeof body.invalidate, "function");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("caches the rendered lines across renders at the same width", () => {
|
|
85
|
+
let bodyRenders = 0;
|
|
86
|
+
const body: Component = {
|
|
87
|
+
render: () => {
|
|
88
|
+
bodyRenders++;
|
|
89
|
+
return ["hi"];
|
|
90
|
+
},
|
|
91
|
+
invalidate: () => {},
|
|
92
|
+
};
|
|
93
|
+
const box = makeBorderedBox(body, plainTheme);
|
|
94
|
+
const first = box.render(12);
|
|
95
|
+
const second = box.render(12);
|
|
96
|
+
assert.strictEqual(bodyRenders, 1);
|
|
97
|
+
assert.strictEqual(second, first);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("renders through a plain wrapper object (TUI calls render as a method of the handed-off object)", () => {
|
|
101
|
+
const box = makeBorderedBox(lines(["hi"]), plainTheme, { label: "Tracker" });
|
|
102
|
+
// Regression for the resume crash: setWidget bridges hand the TUI a wrapper
|
|
103
|
+
// like `{ render: widget.render, ... }`, and pi-tui invokes render as a
|
|
104
|
+
// method *of that wrapper*. Closure components carry no `this`, so the
|
|
105
|
+
// hand-off is safe by construction — detaching the methods must not break.
|
|
106
|
+
// oxlint-disable typescript/unbound-method -- intentional seam regression:
|
|
107
|
+
// closure components must survive detached hand-off.
|
|
108
|
+
const wrapper: Component = { render: box.render, invalidate: box.invalidate };
|
|
109
|
+
// oxlint-enable typescript/unbound-method
|
|
110
|
+
assert.deepStrictEqual(wrapper.render(12), ["╭──────────╮", "│ hi │", "╰──────────╯"]);
|
|
111
|
+
wrapper.invalidate();
|
|
112
|
+
const second = wrapper.render(24);
|
|
113
|
+
assert.deepStrictEqual(second, [
|
|
114
|
+
"╭─── Tracker ──────────╮",
|
|
115
|
+
"│ hi │",
|
|
116
|
+
"╰──────────────────────╯",
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("recomputes when the width changes and invalidate clears the cache", () => {
|
|
121
|
+
let bodyRenders = 0;
|
|
122
|
+
const body: Component = {
|
|
123
|
+
render: () => {
|
|
124
|
+
bodyRenders++;
|
|
125
|
+
return ["hi"];
|
|
126
|
+
},
|
|
127
|
+
invalidate: () => {},
|
|
128
|
+
};
|
|
129
|
+
const box = makeBorderedBox(body, plainTheme);
|
|
130
|
+
box.render(12);
|
|
131
|
+
box.render(20);
|
|
132
|
+
assert.strictEqual(bodyRenders, 2);
|
|
133
|
+
box.invalidate();
|
|
134
|
+
box.render(12);
|
|
135
|
+
assert.strictEqual(bodyRenders, 3);
|
|
136
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
3
|
+
import { composeBorderLine, buildBoxFrame } from "../frame/box-frame.ts";
|
|
4
|
+
import { padRight, visibleWidth } from "../utils.ts";
|
|
5
|
+
import { defineComponent } from "./define-component.ts";
|
|
6
|
+
|
|
7
|
+
export interface BorderedBoxOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Text embedded in the top border (`╭─── label ─────╮`). May be a plain
|
|
10
|
+
* string (optionally pre-themed) or a thunk that derives the label from
|
|
11
|
+
* the current theme at render time — use the thunk when the label should
|
|
12
|
+
* follow theme changes. When the box is too narrow the label is omitted.
|
|
13
|
+
*/
|
|
14
|
+
readonly label?: string | ((theme: Theme) => string);
|
|
15
|
+
/** Border color token. Defaults to "accent". */
|
|
16
|
+
readonly color?: ThemeColor;
|
|
17
|
+
/**
|
|
18
|
+
* Decorative dash run after the label. Defaults to " ─────". Pass a
|
|
19
|
+
* different tail (e.g. a single space for a scroll hint) when the label
|
|
20
|
+
* should not trail with the full dash run.
|
|
21
|
+
*/
|
|
22
|
+
readonly labelSuffix?: string;
|
|
23
|
+
/** Background function applied to every rendered line. */
|
|
24
|
+
readonly bg?: (s: string) => string;
|
|
25
|
+
/** Horizontal padding between the rails and the content. Defaults to 1. */
|
|
26
|
+
readonly paddingX?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Disable the width-keyed render cache. Set false when the body carries
|
|
29
|
+
* its own finer-grained caching keyed on mutable state (so the box never
|
|
30
|
+
* serves stale rails at an unchanged width). Defaults to true.
|
|
31
|
+
*/
|
|
32
|
+
readonly cache?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Frames a child component in the house rounded border (╭─╮│╰─╯), the same
|
|
37
|
+
* style as the header and editor boxes: colored border with an optional label
|
|
38
|
+
* embedded in the top border, and an optional per-line background.
|
|
39
|
+
*
|
|
40
|
+
* The frame geometry comes from the pure {@link buildBoxFrame} model, which
|
|
41
|
+
* pi-free cores (e.g. the inquiry scene) share so every box in the codebase
|
|
42
|
+
* is laid out identically. Border lines are composed with
|
|
43
|
+
* {@link composeBorderLine} and body lines padded with `padRight`, the same
|
|
44
|
+
* primitives the hand-rolled rails in the editor use.
|
|
45
|
+
*
|
|
46
|
+
* Factory (closure) form: the width cache lives in the factory closure, and
|
|
47
|
+
* the returned component is a plain object of closures (`defineComponent`),
|
|
48
|
+
* so `render`/`invalidate` carry no `this`. pi-tui and the extension bridges
|
|
49
|
+
* may invoke them as methods of a wrapper object (e.g. a `setWidget` bridge
|
|
50
|
+
* returns `{ render: widget.render, invalidate: widget.invalidate }`) without
|
|
51
|
+
* losing the instance — the detached-reference crash class cannot occur.
|
|
52
|
+
*/
|
|
53
|
+
export const makeBorderedBox = (
|
|
54
|
+
body: Component,
|
|
55
|
+
theme: Theme,
|
|
56
|
+
options: BorderedBoxOptions = {},
|
|
57
|
+
): Component => {
|
|
58
|
+
const cacheEnabled = options.cache ?? true;
|
|
59
|
+
let cachedWidth: number | undefined;
|
|
60
|
+
let cachedLines: string[] | undefined;
|
|
61
|
+
|
|
62
|
+
const computeLines = (width: number): string[] => {
|
|
63
|
+
const label = typeof options.label === "function" ? options.label(theme) : options.label;
|
|
64
|
+
const frame = buildBoxFrame(width, {
|
|
65
|
+
label,
|
|
66
|
+
labelWidth: label === undefined ? undefined : visibleWidth(label),
|
|
67
|
+
labelSuffix: options.labelSuffix,
|
|
68
|
+
paddingX: options.paddingX ?? 1,
|
|
69
|
+
});
|
|
70
|
+
if (!frame) return [];
|
|
71
|
+
|
|
72
|
+
const paint = (s: string) => theme.fg(options.color ?? "accent", s);
|
|
73
|
+
const bg = options.bg ?? ((s: string) => s);
|
|
74
|
+
const pad = frame.padLeft;
|
|
75
|
+
|
|
76
|
+
const top = bg(composeBorderLine(frame, "top", paint));
|
|
77
|
+
const bottom = bg(composeBorderLine(frame, "bottom", paint));
|
|
78
|
+
const bodyLines = body
|
|
79
|
+
.render(frame.contentWidth)
|
|
80
|
+
.map((line) =>
|
|
81
|
+
bg(
|
|
82
|
+
`${paint(frame.railLeft)}${pad}${padRight(line, frame.contentWidth)}${pad}${paint(frame.railRight)}`,
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
return [top, ...bodyLines, bottom];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return defineComponent({
|
|
89
|
+
render(width: number): string[] {
|
|
90
|
+
if (cacheEnabled && cachedLines !== undefined && cachedWidth === width) {
|
|
91
|
+
return cachedLines;
|
|
92
|
+
}
|
|
93
|
+
const lines = computeLines(width);
|
|
94
|
+
if (cacheEnabled) {
|
|
95
|
+
cachedWidth = width;
|
|
96
|
+
cachedLines = lines;
|
|
97
|
+
}
|
|
98
|
+
return lines;
|
|
99
|
+
},
|
|
100
|
+
invalidate(): void {
|
|
101
|
+
cachedWidth = undefined;
|
|
102
|
+
cachedLines = undefined;
|
|
103
|
+
body.invalidate();
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal surface a component must expose to the pi-tui runtime. `render`
|
|
5
|
+
* and `invalidate` are required by the `Component` interface; the rest are
|
|
6
|
+
* optional lifecycle hooks.
|
|
7
|
+
*/
|
|
8
|
+
export interface ComponentMethods {
|
|
9
|
+
readonly render: (width: number) => string[];
|
|
10
|
+
readonly invalidate: () => void;
|
|
11
|
+
readonly handleInput?: (data: string) => void;
|
|
12
|
+
readonly dispose?: () => void;
|
|
13
|
+
readonly wantsKeyRelease?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds a TUI component from closures.
|
|
18
|
+
*
|
|
19
|
+
* The returned object's methods are the closures handed in — there is no
|
|
20
|
+
* `this` anywhere, so pi-tui (and any bridge such as `setWidget`, `ui.custom`,
|
|
21
|
+
* `setFooter` or `setHeader`) can invoke `render`/`invalidate`/`handleInput`
|
|
22
|
+
* as methods of *any* wrapper object without losing state. This is the only
|
|
23
|
+
* sanctioned way to construct a component that crosses the pi seam: never
|
|
24
|
+
* hand pi a class-method reference (`{ render: widget.render }`), which
|
|
25
|
+
* rebinds `this` to the wrapper and breaks the component.
|
|
26
|
+
*/
|
|
27
|
+
export const defineComponent = (methods: ComponentMethods) => ({ ...methods }) satisfies Component;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A component object as returned by {@link defineComponent}: the closures
|
|
31
|
+
* handed in, with an optional `dispose` lifecycle hook.
|
|
32
|
+
*/
|
|
33
|
+
export type DisposableComponent = ReturnType<typeof defineComponent>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public component surface of the tui package.
|
|
3
|
+
*
|
|
4
|
+
* Extensions and workspace packages import the shared house UI from this
|
|
5
|
+
* entry (`import { makeBorderedBox } from "@ftrdotdev/pi-tui"`). The pi extension
|
|
6
|
+
* itself is registered through the `pi.extensions` field and loads
|
|
7
|
+
* `./src/index.ts` directly; it is intentionally not re-exported here so
|
|
8
|
+
* importing a component never drags in the extension's side effects.
|
|
9
|
+
*
|
|
10
|
+
* This folder holds the composable components and the primitives they share.
|
|
11
|
+
* `defineComponent` lives in its own leaf module (`./define-component.ts`):
|
|
12
|
+
* components import it directly, never this barrel, so the layout is acyclic.
|
|
13
|
+
*
|
|
14
|
+
* The pure frame model is available at `@ftrdotdev/pi-tui/box-frame` for
|
|
15
|
+
* pi-free cores that must not depend on terminal styling.
|
|
16
|
+
*/
|
|
17
|
+
export { makeBorderedBox, type BorderedBoxOptions } from "./bordered-box.ts";
|
|
18
|
+
export { defineComponent, type ComponentMethods } from "./define-component.ts";
|
|
19
|
+
export {
|
|
20
|
+
buildBoxFrame,
|
|
21
|
+
composeBorderLine,
|
|
22
|
+
type BoxFrame,
|
|
23
|
+
type BoxFrameOptions,
|
|
24
|
+
type BoxBorderLine,
|
|
25
|
+
type BoxTopBorder,
|
|
26
|
+
} from "../frame/box-frame.ts";
|
|
27
|
+
export { VimRouter, type NavigationIntent } from "../vim/vim-router.ts";
|
|
28
|
+
// Editor customization: a generic Effect service other extensions can import
|
|
29
|
+
// and `.configure` to inject/override the editor's border tint.
|
|
30
|
+
export {
|
|
31
|
+
EditorTintService,
|
|
32
|
+
EditorTintServiceDefault,
|
|
33
|
+
upsertBorderTintProvider,
|
|
34
|
+
type BorderTintProvider,
|
|
35
|
+
} from "../ui/editor-tint.ts";
|