repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/terminal-widget.ts

Raw
File outline
import type { UiServerState } from "./server";

export type WidgetState = {
	url: string;
	qr: string;
	state: UiServerState | "starting" | "error";
	sessionLabel: string;
	clients: number;
	rpcState?: string;
	sessionPath?: string;
	lastError?: string;
};

export function osc8Link(url: string, label: string): string {
	return `\u001b]8;;${url}\u001b\\${label}\u001b]8;;\u001b\\`;
}

export function renderUiWidget(state: WidgetState): string[] {
	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",
	];
	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;
}