repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Render ui widget as bounded box

339007ca086e60751026b696a17f7bf80428b7bd

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

+84 / −18 text lines

Diff layout

extensions/ui/__tests__/runtime.test.ts

Modified

+1 / −1 text lines

7777 expect(runtime.widgetLines().join("\n")).toContain("ui: test");
7979 await runtime.redrawQr();
80 expect(runtime.widgetLines().join("\n")).toContain("mobile qr");
80 expect(runtime.widgetLines().join("\n")).toContain("mobile QR: /ui qr");
8282 await runtime.stop("test");
8383 expect(runtime.status().active).toBe(false);

extensions/ui/__tests__/terminal-widget.test.ts

Modified

+34 / −4 text lines

11import { describe, expect, it } from "vitest";
22import { osc8Link, renderUiWidget } from "../terminal-widget";
4const OSC8_RE = /\u001b]8;;.*?\u001b\\|\u001b]8;;\u001b\\/g;
5
6function visibleLength(line: string): number {
7 return line.replace(OSC8_RE, "").length;
8}
9
410describe("ui terminal widget", () => {
511 it("renders OSC 8 links", () => {
612 expect(osc8Link("http://127.0.0.1:123/open", "open ui")).toBe(
814 );
915 });
11 it("renders actionable widget lines", () => {
17 it("renders a closed bounded widget box", () => {
1218 const lines = renderUiWidget({
1319 url: "http://127.0.0.1:123/?token=abc",
14 qr: "QR",
20 qr: Array.from({ length: 30 }, (_, index) => `QR ${index}`).join("\n"),
1521 state: "connected",
1622 sessionLabel: "ui: pi-ext",
1723 clients: 1,
1824 });
2026 const text = lines.join("\n");
27 expect(lines).toHaveLength(8);
28 expect(lines[0]).toMatch(/^┏━+┓$/);
29 expect(lines.at(-1)).toMatch(/^┗━+┛$/);
30 expect(lines.slice(1, -1).every((line) => /^┃.*┃$/.test(line))).toBe(true);
31 expect(new Set(lines.map((line) => visibleLength(line))).size).toBe(1);
2132 expect(text).toContain("pi ui");
2233 expect(text).toContain("connected");
2334 expect(text).toContain("ui: pi-ext");
24 expect(text).toContain("press ESC to cancel UI mode");
35 expect(text).toContain("press ESC to stop");
2536 expect(text).toContain("/ui off");
26 expect(text).toContain("QR");
37 expect(text).toContain("/ui qr");
38 expect(text).not.toContain("widget truncated");
39 expect(text).not.toContain("QR 29");
40 });
42 it("wraps long content inside the box without truncating", () => {
43 const longLabel = "ui: " + "x".repeat(90);
44 const lines = renderUiWidget({
45 url: "http://127.0.0.1:123/?token=abc",
46 qr: "QR",
47 state: "connected",
48 sessionLabel: longLabel,
49 clients: 1,
50 });
52 const text = lines.join("\n");
53 expect(text).toContain("session: ui: ");
54 expect(text.match(/x/g)).toHaveLength(90);
55 expect(text).not.toContain("…");
56 expect(new Set(lines.map((line) => visibleLength(line))).size).toBe(1);
2757 });
2858});

extensions/ui/terminal-widget.ts

Modified

+49 / −13 text lines

1import path from "node:path";
12import type { UiServerState } from "./server";
34export type WidgetState = {
1112 lastError?: string;
1213};
15const WIDTH = 64;
16const CONTENT_WIDTH = WIDTH - 4;
17const OSC8_RE = /\u001b]8;;.*?\u001b\\|\u001b]8;;\u001b\\/g;
1419export function osc8Link(url: string, label: string): string {
1520 return `\u001b]8;;${url}\u001b\\${label}\u001b]8;;\u001b\\`;
1823export function renderUiWidget(state: WidgetState): string[] {
19 const lines = [
20 "┏━ pi ui ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
21 `┃ ${osc8Link(state.url, "open browser ui")}`,
22 `┃ session: ${state.sessionLabel}`,
23 `┃ state: ${state.state} · clients: ${state.clients}`,
24 `┃ rpc: ${state.rpcState ?? "not-started"}${state.sessionPath ? ` · ${state.sessionPath}` : ""}`,
25 "┃ press ESC to cancel UI mode · run /ui off to restore terminal UI",
24 const session = state.sessionPath
25 ? path.basename(state.sessionPath)
26 : "pending";
27 const rows = [
28 "pi ui",
29 osc8Link(state.url, "open browser ui"),
30 `session: ${state.sessionLabel}`,
31 `state: ${state.state} · clients: ${state.clients} · rpc: ${state.rpcState ?? "not-started"}`,
32 `file: ${session}`,
33 "press ESC to stop · /ui off · mobile QR: /ui qr",
2634 ];
27 if (state.lastError) lines.push(`┃ error: ${state.lastError}`);
28 lines.push("┣━ mobile qr ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
29 for (const line of state.qr.split(/\r?\n/).filter(Boolean))
30 lines.push(`┃ ${line}`);
31 lines.push("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
32 return lines;
35 if (state.lastError) rows.push(`error: ${state.lastError}`);
37 return [top(), ...rows.flatMap(row), bottom()];
38}
40function top(): string {
41 return `┏${"━".repeat(WIDTH - 2)}┓`;
42}
44function bottom(): string {
45 return `┗${"━".repeat(WIDTH - 2)}┛`;
46}
48function row(content: string): string[] {
49 return wrap(content).map(
50 (part) =>
51 `┃ ${part}${" ".repeat(Math.max(0, CONTENT_WIDTH - visibleLength(part)))} ┃`,
52 );
53}
55function wrap(content: string): string[] {
56 if (visibleLength(content) <= CONTENT_WIDTH) return [content];
57 const parts: string[] = [];
58 let rest = content;
59 while (visibleLength(rest) > CONTENT_WIDTH) {
60 parts.push(rest.slice(0, CONTENT_WIDTH));
61 rest = rest.slice(CONTENT_WIDTH);
62 }
63 if (rest) parts.push(rest);
64 return parts;
65}
67function visibleLength(content: string): number {
68 return content.replace(OSC8_RE, "").length;