@restranger/repi-ui 0.0.0-stage → 0.0.1-sha.357e3bb.dirty
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/package.json +52 -5
- package/src/index.ts +8 -0
- package/src/plugin.ts +125 -0
- package/src/render-to-text.ts +372 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@restranger/repi-ui",
|
|
3
|
+
"version": "0.0.1-sha.357e3bb.dirty",
|
|
4
|
+
"description": "Declarative ViewNode renderer and UI service for Pi extensions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"!src/**/__tests__"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [
|
|
17
|
+
"pi-package",
|
|
18
|
+
"pi-coding-agent",
|
|
19
|
+
"ui",
|
|
20
|
+
"tui",
|
|
21
|
+
"view"
|
|
22
|
+
],
|
|
23
|
+
"author": "ReStranger <restranger@disroot.org>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/ReStranger/repi-mono.git",
|
|
28
|
+
"directory": "packages/ui"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/ReStranger/repi-mono/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/ReStranger/repi-mono#readme",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"pi": {
|
|
38
|
+
"extensions": [
|
|
39
|
+
"./src/index.ts"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@restranger/repi-lib": "0.0.1-sha.357e3bb.dirty"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@earendil-works/pi-coding-agent": ">=0.79.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"@earendil-works/pi-coding-agent": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Disposable,
|
|
3
|
+
type PiContext,
|
|
4
|
+
type PiExtensionAPI,
|
|
5
|
+
UI_CONTRACT_ID,
|
|
6
|
+
type UiService,
|
|
7
|
+
announceReady,
|
|
8
|
+
getSessionId,
|
|
9
|
+
publishService,
|
|
10
|
+
} from "@restranger/repi-lib";
|
|
11
|
+
|
|
12
|
+
export interface UiPluginOptions {
|
|
13
|
+
/** Session used when the host does not pass one in the event context. */
|
|
14
|
+
defaultSessionId?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface UiRuntime {
|
|
18
|
+
attach(pi: PiExtensionAPI): void;
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function serviceFromContext(ctx: PiContext): UiService {
|
|
23
|
+
return {
|
|
24
|
+
select: (title, options, opts) => ctx.ui.select(title, options, opts),
|
|
25
|
+
confirm: (title, message, opts) => ctx.ui.confirm(title, message, opts),
|
|
26
|
+
input: (title, placeholder, opts) => ctx.ui.input(title, placeholder, opts),
|
|
27
|
+
notify: (message, type) => ctx.ui.notify(message, type),
|
|
28
|
+
setStatus: (key, text) => ctx.ui.setStatus?.(key, text),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Publishes the host's dialog/notification surface as `re:ui@1`.
|
|
34
|
+
*
|
|
35
|
+
* This plugin has no dependency on permissions or modes. A consumer that wants a dialog resolves
|
|
36
|
+
* the service; a consumer that does not care simply never asks.
|
|
37
|
+
*/
|
|
38
|
+
export function createUiRuntime(opts: UiPluginOptions = {}): UiRuntime {
|
|
39
|
+
const defaultSessionId = opts.defaultSessionId ?? "default";
|
|
40
|
+
const sessions = new Map<string, Disposable>();
|
|
41
|
+
let attached = false;
|
|
42
|
+
let disposed = false;
|
|
43
|
+
|
|
44
|
+
const publish = (pi: PiExtensionAPI, ctx: PiContext): void => {
|
|
45
|
+
const sessionId = getSessionId(ctx, defaultSessionId);
|
|
46
|
+
sessions.get(sessionId)?.dispose();
|
|
47
|
+
const registration = publishService({
|
|
48
|
+
contract: UI_CONTRACT_ID,
|
|
49
|
+
sessionId,
|
|
50
|
+
owner: "repi:ui",
|
|
51
|
+
impl: serviceFromContext(ctx),
|
|
52
|
+
});
|
|
53
|
+
sessions.set(sessionId, registration);
|
|
54
|
+
announceReady(pi.events, {
|
|
55
|
+
contract: UI_CONTRACT_ID,
|
|
56
|
+
version: 1,
|
|
57
|
+
sessionId,
|
|
58
|
+
owner: "repi:ui",
|
|
59
|
+
capabilities: ["select", "confirm", "input", "notify", "status"],
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const attach = (pi: PiExtensionAPI): void => {
|
|
64
|
+
if (disposed) {
|
|
65
|
+
throw new Error("UI runtime is disposed and cannot be attached.");
|
|
66
|
+
}
|
|
67
|
+
if (attached) {
|
|
68
|
+
throw new Error("UI runtime is already attached to a Pi extension API.");
|
|
69
|
+
}
|
|
70
|
+
attached = true;
|
|
71
|
+
|
|
72
|
+
pi.on("session_start", (_event, ctx) => {
|
|
73
|
+
if (!disposed) {
|
|
74
|
+
publish(pi, ctx);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
pi.on("before_agent_start", (_event, ctx) => {
|
|
78
|
+
if (disposed) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
if (!sessions.has(getSessionId(ctx, defaultSessionId))) {
|
|
82
|
+
publish(pi, ctx);
|
|
83
|
+
} else {
|
|
84
|
+
const sessionId = getSessionId(ctx, defaultSessionId);
|
|
85
|
+
announceReady(pi.events, {
|
|
86
|
+
contract: UI_CONTRACT_ID,
|
|
87
|
+
version: 1,
|
|
88
|
+
sessionId,
|
|
89
|
+
owner: "repi:ui",
|
|
90
|
+
capabilities: ["select", "confirm", "input", "notify", "status"],
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
});
|
|
95
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
96
|
+
const sessionId = getSessionId(ctx, defaultSessionId);
|
|
97
|
+
sessions.get(sessionId)?.dispose();
|
|
98
|
+
sessions.delete(sessionId);
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const dispose = (): void => {
|
|
103
|
+
if (disposed) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
disposed = true;
|
|
107
|
+
for (const registration of sessions.values()) {
|
|
108
|
+
registration.dispose();
|
|
109
|
+
}
|
|
110
|
+
sessions.clear();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return { attach, dispose };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Return a Pi extension factory; useful for tests and for composition in a custom host. */
|
|
117
|
+
export function createUiPlugin(opts: UiPluginOptions = {}): (pi: unknown) => void {
|
|
118
|
+
const runtime = createUiRuntime(opts);
|
|
119
|
+
return (pi) => runtime.attach(pi as PiExtensionAPI);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Default Pi entry point (`pi.extensions`). Accepts the host API structurally. */
|
|
123
|
+
export default function uiExtension(pi: unknown): void {
|
|
124
|
+
createUiPlugin()(pi);
|
|
125
|
+
}
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BorderStyle,
|
|
3
|
+
type BoxSide,
|
|
4
|
+
type ListItem,
|
|
5
|
+
type ViewNode,
|
|
6
|
+
displayWidth,
|
|
7
|
+
fitWidth,
|
|
8
|
+
truncate,
|
|
9
|
+
wrapDisplay,
|
|
10
|
+
} from "@restranger/repi-lib";
|
|
11
|
+
|
|
12
|
+
export interface RenderToTextOptions {
|
|
13
|
+
/** Total width budget for the whole tree (`notes/02` §3). Defaults to 80 (or 120 for tui-120). */
|
|
14
|
+
width?: number;
|
|
15
|
+
/** Hard node budget so one plugin cannot lock the renderer (`notes/02` §2 rule 7). */
|
|
16
|
+
maxNodes?: number;
|
|
17
|
+
/** Hard output budget in UTF-8 bytes. Defaults to 64 KiB. */
|
|
18
|
+
maxBytes?: number;
|
|
19
|
+
/** Maximum nesting depth. Defaults to 50. */
|
|
20
|
+
maxDepth?: number;
|
|
21
|
+
/** Surface hint for rendering (`notes/02` §3, §7). */
|
|
22
|
+
surface?: "tui-80" | "tui-120" | "web" | "headless";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DEFAULT_WIDTH = 80;
|
|
26
|
+
const DEFAULT_MAX_NODES = 5000;
|
|
27
|
+
const DEFAULT_MAX_BYTES = 64 * 1024;
|
|
28
|
+
const DEFAULT_MAX_DEPTH = 50;
|
|
29
|
+
|
|
30
|
+
type Node = Exclude<ViewNode, { t: "col" } | { t: "row" }>;
|
|
31
|
+
|
|
32
|
+
interface RenderState {
|
|
33
|
+
width: number;
|
|
34
|
+
readonly maxNodes: number;
|
|
35
|
+
readonly maxBytes: number;
|
|
36
|
+
readonly maxDepth: number;
|
|
37
|
+
nodes: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Cuts to `width` display columns, appending an ellipsis when something was removed. */
|
|
41
|
+
export const truncateText = truncate;
|
|
42
|
+
|
|
43
|
+
/** Word wrap that respects the display width, keeping existing newlines. */
|
|
44
|
+
export const wrapText = wrapDisplay;
|
|
45
|
+
|
|
46
|
+
function formatBadgeText(text: string): string {
|
|
47
|
+
if (text.startsWith("[") && text.endsWith("]")) {
|
|
48
|
+
return text;
|
|
49
|
+
}
|
|
50
|
+
return `[${text}]`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function resolveListBadge(item: ListItem): string {
|
|
54
|
+
if (item.badge) {
|
|
55
|
+
return item.badge;
|
|
56
|
+
}
|
|
57
|
+
if (item.done === true) {
|
|
58
|
+
return "✓";
|
|
59
|
+
}
|
|
60
|
+
if (item.done === false) {
|
|
61
|
+
return "○";
|
|
62
|
+
}
|
|
63
|
+
if (item.status === "done" || item.status === "completed") {
|
|
64
|
+
return "✓";
|
|
65
|
+
}
|
|
66
|
+
if (item.status === "in_progress" || item.status === "active" || item.status === "running") {
|
|
67
|
+
return "●";
|
|
68
|
+
}
|
|
69
|
+
if (item.status === "pending" || item.status === "todo") {
|
|
70
|
+
return "○";
|
|
71
|
+
}
|
|
72
|
+
return "●";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface FrameChars {
|
|
76
|
+
readonly tl: string;
|
|
77
|
+
readonly tr: string;
|
|
78
|
+
readonly bl: string;
|
|
79
|
+
readonly br: string;
|
|
80
|
+
readonly h: string;
|
|
81
|
+
readonly v: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const FRAMES: Record<Exclude<BorderStyle, "none">, FrameChars> = {
|
|
85
|
+
default: { tl: "╭", tr: "╮", bl: "╰", br: "╯", h: "─", v: "│" },
|
|
86
|
+
rounded: { tl: "╭", tr: "╮", bl: "╰", br: "╯", h: "─", v: "│" },
|
|
87
|
+
sharp: { tl: "┌", tr: "┐", bl: "└", br: "┘", h: "─", v: "│" },
|
|
88
|
+
double: { tl: "╔", tr: "╗", bl: "╚", br: "╝", h: "═", v: "║" },
|
|
89
|
+
minimal: { tl: "", tr: "", bl: "", br: "", h: "─", v: "" },
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function padTo(text: string, width: number): string {
|
|
93
|
+
return fitWidth(text, width);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function frameBox(
|
|
97
|
+
innerLines: string[],
|
|
98
|
+
title: string | undefined,
|
|
99
|
+
style: BorderStyle,
|
|
100
|
+
sides: readonly BoxSide[] | undefined,
|
|
101
|
+
totalWidth: number,
|
|
102
|
+
): string[] {
|
|
103
|
+
if (style === "none") {
|
|
104
|
+
return innerLines.map((line) => truncate(line, totalWidth));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const chars = FRAMES[style] ?? FRAMES.default;
|
|
108
|
+
const showLeft = sides === undefined || sides.includes("left");
|
|
109
|
+
const showRight = sides === undefined || sides.includes("right");
|
|
110
|
+
const showTop = sides === undefined || sides.includes("top");
|
|
111
|
+
const showBottom = sides === undefined || sides.includes("bottom");
|
|
112
|
+
|
|
113
|
+
const leftCap = showLeft ? chars.v : "";
|
|
114
|
+
const rightCap = showRight ? chars.v : "";
|
|
115
|
+
// Width available between the vertical borders.
|
|
116
|
+
const innerWidth = Math.max(1, totalWidth - displayWidth(leftCap) - displayWidth(rightCap));
|
|
117
|
+
// Body lines are `│ <content> │`, the left border costs two extra columns of padding.
|
|
118
|
+
const contentWidth = Math.max(1, innerWidth - (showLeft ? 2 : 0));
|
|
119
|
+
const rendered = innerLines.flatMap((line) => wrapDisplay(line, contentWidth));
|
|
120
|
+
|
|
121
|
+
const lines: string[] = [];
|
|
122
|
+
|
|
123
|
+
if (showTop) {
|
|
124
|
+
const titleText = title ? ` ${title} ` : "";
|
|
125
|
+
const trimmedTitle = truncate(titleText, innerWidth);
|
|
126
|
+
const fill = Math.max(0, innerWidth - displayWidth(trimmedTitle));
|
|
127
|
+
const topLeft = showLeft ? chars.tl : "";
|
|
128
|
+
const topRight = showRight ? chars.tr : "";
|
|
129
|
+
lines.push(`${topLeft}${trimmedTitle}${chars.h.repeat(fill)}${topRight}`);
|
|
130
|
+
} else if (title) {
|
|
131
|
+
lines.push(truncate(` ${title}`, totalWidth));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const line of rendered) {
|
|
135
|
+
const body = showLeft ? ` ${padTo(line, contentWidth)} ` : padTo(line, contentWidth);
|
|
136
|
+
lines.push(`${leftCap}${body}${rightCap}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (showBottom) {
|
|
140
|
+
const bottomLeft = showLeft ? chars.bl : "";
|
|
141
|
+
const bottomRight = showRight ? chars.br : "";
|
|
142
|
+
lines.push(`${bottomLeft}${chars.h.repeat(innerWidth)}${bottomRight}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return lines;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function renderKnownNode(node: Node, state: RenderState, depth: number): string[] {
|
|
149
|
+
switch (node.t) {
|
|
150
|
+
case "text": {
|
|
151
|
+
const wrap = node.wrap ?? true;
|
|
152
|
+
return wrap ? wrapText(node.text, state.width) : [truncateText(node.text, state.width)];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
case "markdown": {
|
|
156
|
+
// The plain-text surface keeps markdown readable as-is (`notes/02` §1: T1 fallback first).
|
|
157
|
+
return wrapText(node.md, state.width);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
case "badge":
|
|
161
|
+
return [truncateText(formatBadgeText(node.text), state.width)];
|
|
162
|
+
|
|
163
|
+
case "button":
|
|
164
|
+
return [truncateText(`[ ${node.label} ]`, state.width)];
|
|
165
|
+
|
|
166
|
+
case "progress": {
|
|
167
|
+
const max = node.max ?? 100;
|
|
168
|
+
const value = Math.max(0, Math.min(node.value, max));
|
|
169
|
+
const label = node.label ? ` ${node.label}` : "";
|
|
170
|
+
return [truncateText(`[${value}/${max}]${label}`, state.width)];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
case "spinner":
|
|
174
|
+
return [truncateText(`… ${node.label ?? ""}`.trim(), state.width)];
|
|
175
|
+
|
|
176
|
+
case "kv":
|
|
177
|
+
return node.items.map(([key, value]) => truncateText(`${key}: ${value}`, state.width));
|
|
178
|
+
|
|
179
|
+
case "list":
|
|
180
|
+
return node.items.map((item, index) => {
|
|
181
|
+
let prefix: string;
|
|
182
|
+
if (node.ordered) {
|
|
183
|
+
prefix = `${index + 1}.`;
|
|
184
|
+
} else if (node.checkbox) {
|
|
185
|
+
prefix = item.done ? "[x]" : "[ ]";
|
|
186
|
+
} else {
|
|
187
|
+
prefix = resolveListBadge(item);
|
|
188
|
+
}
|
|
189
|
+
return truncateText(`${prefix} ${item.text}`, state.width);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
case "table": {
|
|
193
|
+
if (node.fallback) {
|
|
194
|
+
return node.fallback.flatMap((child) => renderNode(child, state, depth + 1));
|
|
195
|
+
}
|
|
196
|
+
const columns = Math.max(node.head.length, ...node.rows.map((row) => row.length), 1);
|
|
197
|
+
const widths: number[] = [];
|
|
198
|
+
for (let index = 0; index < columns; index++) {
|
|
199
|
+
widths.push(
|
|
200
|
+
Math.max(displayWidth(node.head[index] ?? ""), ...node.rows.map((row) => displayWidth(row[index] ?? "")), 1),
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
const total = widths.reduce((sum, width) => sum + width + 2, 0);
|
|
204
|
+
const scale = total > state.width ? state.width / total : 1;
|
|
205
|
+
const line = (cells: readonly string[]) =>
|
|
206
|
+
cells
|
|
207
|
+
.map((cell, index) => padTo(cell, Math.max(1, Math.floor((widths[index] ?? 1) * scale))))
|
|
208
|
+
.join(" ")
|
|
209
|
+
.trimEnd();
|
|
210
|
+
return [
|
|
211
|
+
line(node.head),
|
|
212
|
+
line(widths.map((width) => "─".repeat(Math.max(1, Math.floor(width * scale))))),
|
|
213
|
+
...node.rows.map(line),
|
|
214
|
+
].map((row) => truncateText(row, state.width));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
case "diff": {
|
|
218
|
+
if (node.fallback) {
|
|
219
|
+
return node.fallback.flatMap((child) => renderNode(child, state, depth + 1));
|
|
220
|
+
}
|
|
221
|
+
return node.patch.split("\n").map((line) => truncateText(line, state.width));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
case "box": {
|
|
225
|
+
const children = node.children.flatMap((child) => renderNode(child, state, depth + 1));
|
|
226
|
+
const variant = node.variant ?? "callout";
|
|
227
|
+
|
|
228
|
+
if (variant === "callout") {
|
|
229
|
+
const bar = node.border === "sharp" ? "│" : "┃";
|
|
230
|
+
const lines: string[] = [];
|
|
231
|
+
if (node.title) {
|
|
232
|
+
lines.push(`${bar} ${truncateText(`[${node.title}]`, state.width - 2)}`);
|
|
233
|
+
}
|
|
234
|
+
for (const line of children) {
|
|
235
|
+
lines.push(`${bar} ${truncateText(line, state.width - 2)}`);
|
|
236
|
+
}
|
|
237
|
+
return lines;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (variant === "minimal") {
|
|
241
|
+
const body = children.map((line) => truncateText(line, state.width - 2));
|
|
242
|
+
return node.title ? [`${node.title}`, ...body] : body;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return frameBox(children, node.title, node.border ?? "default", node.sides, state.width);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function renderNode(node: ViewNode, state: RenderState, depth = 0): string[] {
|
|
251
|
+
state.nodes += 1;
|
|
252
|
+
if (state.nodes > state.maxNodes) {
|
|
253
|
+
throw new Error(`ViewNode tree exceeds the node budget (${state.maxNodes}); refusing to render.`);
|
|
254
|
+
}
|
|
255
|
+
if (depth > state.maxDepth) {
|
|
256
|
+
throw new Error(`ViewNode tree exceeds the depth budget (${state.maxDepth}); refusing to render.`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (node.t === "col") {
|
|
260
|
+
const gap = Math.max(0, node.gap ?? 0);
|
|
261
|
+
const lines: string[] = [];
|
|
262
|
+
node.children.forEach((child, index) => {
|
|
263
|
+
if (index > 0) {
|
|
264
|
+
for (let i = 0; i < gap; i++) {
|
|
265
|
+
lines.push("");
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
lines.push(...renderNode(child, state, depth + 1));
|
|
269
|
+
});
|
|
270
|
+
return lines;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (node.t === "row") {
|
|
274
|
+
if (node.children.length === 0) {
|
|
275
|
+
return [];
|
|
276
|
+
}
|
|
277
|
+
const gap = Math.max(0, node.gap ?? 1);
|
|
278
|
+
const columnWidth = Math.max(
|
|
279
|
+
1,
|
|
280
|
+
Math.floor((state.width - gap * (node.children.length - 1)) / node.children.length),
|
|
281
|
+
);
|
|
282
|
+
const originalWidth = state.width;
|
|
283
|
+
state.width = columnWidth;
|
|
284
|
+
const children = node.children.map((child) => renderNode(child, state, depth + 1));
|
|
285
|
+
state.width = originalWidth;
|
|
286
|
+
|
|
287
|
+
const height = Math.max(0, ...children.map((lines) => lines.length));
|
|
288
|
+
const widths = children.map((lines) => Math.min(columnWidth, Math.max(0, ...lines.map(displayWidth))));
|
|
289
|
+
const lines: string[] = [];
|
|
290
|
+
for (let index = 0; index < height; index++) {
|
|
291
|
+
const cells = children.map((childLines, childIndex) => padTo(childLines[index] ?? "", widths[childIndex] ?? 0));
|
|
292
|
+
lines.push(truncateText(cells.join(" ".repeat(gap)).trimEnd(), state.width));
|
|
293
|
+
}
|
|
294
|
+
return lines;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return renderKnownNode(node, state, depth);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function renderNodeToLines(
|
|
301
|
+
node: ViewNode | ViewNode[] | string | number | boolean | null | undefined,
|
|
302
|
+
opts?: RenderToTextOptions,
|
|
303
|
+
): string[] {
|
|
304
|
+
if (node === null || node === undefined) {
|
|
305
|
+
return [];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const defaultWidth = opts?.surface === "tui-120" ? 120 : DEFAULT_WIDTH;
|
|
309
|
+
const state: RenderState = {
|
|
310
|
+
width: Math.max(10, opts?.width ?? defaultWidth),
|
|
311
|
+
maxNodes: opts?.maxNodes ?? DEFAULT_MAX_NODES,
|
|
312
|
+
maxBytes: opts?.maxBytes ?? DEFAULT_MAX_BYTES,
|
|
313
|
+
maxDepth: opts?.maxDepth ?? DEFAULT_MAX_DEPTH,
|
|
314
|
+
nodes: 0,
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const render = (value: unknown): string[] => {
|
|
318
|
+
if (value === null || value === undefined) {
|
|
319
|
+
return [];
|
|
320
|
+
}
|
|
321
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
322
|
+
return String(value)
|
|
323
|
+
.split("\n")
|
|
324
|
+
.flatMap((line) => wrapText(line, state.width));
|
|
325
|
+
}
|
|
326
|
+
if (Array.isArray(value)) {
|
|
327
|
+
return value.flatMap((child) => render(child));
|
|
328
|
+
}
|
|
329
|
+
// SAFETY: the union is exhaustive; anything else is a plugin bug and is surfaced, not dropped.
|
|
330
|
+
const asNode = value as ViewNode;
|
|
331
|
+
const known = [
|
|
332
|
+
"col",
|
|
333
|
+
"row",
|
|
334
|
+
"box",
|
|
335
|
+
"text",
|
|
336
|
+
"markdown",
|
|
337
|
+
"badge",
|
|
338
|
+
"kv",
|
|
339
|
+
"list",
|
|
340
|
+
"table",
|
|
341
|
+
"diff",
|
|
342
|
+
"progress",
|
|
343
|
+
"spinner",
|
|
344
|
+
"button",
|
|
345
|
+
];
|
|
346
|
+
if (typeof asNode.t !== "string" || !known.includes(asNode.t)) {
|
|
347
|
+
return [`[unsupported view node: ${String((asNode as { t?: unknown }).t)}]`];
|
|
348
|
+
}
|
|
349
|
+
return renderNode(asNode, state);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const lines = render(node);
|
|
353
|
+
const bytes = Buffer.byteLength(lines.join("\n"), "utf8");
|
|
354
|
+
if (bytes > state.maxBytes) {
|
|
355
|
+
throw new Error(`ViewNode output exceeds the byte budget (${state.maxBytes} bytes); refusing to render.`);
|
|
356
|
+
}
|
|
357
|
+
return lines;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function renderToText(
|
|
361
|
+
node: ViewNode | ViewNode[] | string | number | boolean | null | undefined,
|
|
362
|
+
opts?: RenderToTextOptions,
|
|
363
|
+
): string {
|
|
364
|
+
return renderNodeToLines(node, opts).join("\n");
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function renderToJson(
|
|
368
|
+
tree: ViewNode | readonly ViewNode[],
|
|
369
|
+
_opts?: { surface?: "tui-80" | "tui-120" | "web" | "headless" },
|
|
370
|
+
): string {
|
|
371
|
+
return JSON.stringify(tree, null, 2);
|
|
372
|
+
}
|
package/README.md
DELETED