Render ui widget as bounded box
339007ca086e60751026b696a17f7bf80428b7bd
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
- Parent 1: 30c3d2cbc6af
bugabingas pi extensions
339007ca086e60751026b696a17f7bf80428b7bd
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
+84 / −18 text lines
+1 / −1 text lines
+34 / −4 text lines
it("renders actionable widget lines", () => { it("renders a closed bounded widget box", () => { expect(text).toContain("press ESC to cancel UI mode"); expect(text).toContain("press ESC to stop"); expect(text).toContain("QR"); expect(text).toContain("/ui qr"); expect(text).not.toContain("widget truncated"); expect(text).not.toContain("QR 29"); }); it("wraps long content inside the box without truncating", () => { const longLabel = "ui: " + "x".repeat(90); const lines = renderUiWidget({ url: "http://127.0.0.1:123/?token=abc", qr: "QR", state: "connected", sessionLabel: longLabel, clients: 1, }); const text = lines.join("\n"); expect(text).toContain("session: ui: "); expect(text.match(/x/g)).toHaveLength(90); expect(text).not.toContain("…"); expect(new Set(lines.map((line) => visibleLength(line))).size).toBe(1);+49 / −13 text lines
import path from "node:path"; const lines = [ "┏━ pi ui ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━", `┃ ${osc8Link(state.url, "open browser ui")}`, `┃ session: ${state.sessionLabel}`, `┃ state: ${state.state} · clients: ${state.clients}`, `┃ rpc: ${state.rpcState ?? "not-started"}${state.sessionPath ? ` · ${state.sessionPath}` : ""}`, "┃ press ESC to cancel UI mode · run /ui off to restore terminal UI", const session = state.sessionPath ? path.basename(state.sessionPath) : "pending"; const rows = [ "pi ui", osc8Link(state.url, "open browser ui"), `session: ${state.sessionLabel}`, `state: ${state.state} · clients: ${state.clients} · rpc: ${state.rpcState ?? "not-started"}`, `file: ${session}`, "press ESC to stop · /ui off · mobile QR: /ui qr", if (state.lastError) lines.push(`┃ error: ${state.lastError}`); lines.push("┣━ mobile qr ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"); for (const line of state.qr.split(/\r?\n/).filter(Boolean)) lines.push(`┃ ${line}`); lines.push("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"); return lines; if (state.lastError) rows.push(`error: ${state.lastError}`); return [top(), ...rows.flatMap(row), bottom()];}function top(): string { return `┏${"━".repeat(WIDTH - 2)}┓`;}function bottom(): string { return `┗${"━".repeat(WIDTH - 2)}┛`;}function row(content: string): string[] { return wrap(content).map( (part) => `┃ ${part}${" ".repeat(Math.max(0, CONTENT_WIDTH - visibleLength(part)))} ┃`, );}function wrap(content: string): string[] { if (visibleLength(content) <= CONTENT_WIDTH) return [content]; const parts: string[] = []; let rest = content; while (visibleLength(rest) > CONTENT_WIDTH) { parts.push(rest.slice(0, CONTENT_WIDTH)); rest = rest.slice(CONTENT_WIDTH); } if (rest) parts.push(rest); return parts;}function visibleLength(content: string): number { return content.replace(OSC8_RE, "").length;