Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/src/native-setup-approval.ts

Raw
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
import {
	type Component,
	Key,
	type KeyId,
	matchesKey,
	ScrollView,
	stripTerminalSequences,
	Text,
	type TUI,
} from "@earendil-works/pi-tui";

/** Compact consent with inspectable, scrollable asset details before any writes. */
export async function approveNativeSetup(
	ctx: ExtensionContext,
	title: string,
	details: string,
	signal?: AbortSignal,
	cancelShortcut?: KeyId,
	summary?: string,
	approveLabel = "continue",
): Promise<boolean> {
	signal?.throwIfAborted();
	let cancel: (() => void) | undefined;
	const abort = () => cancel?.();
	signal?.addEventListener("abort", abort, { once: true });
	try {
		return await ctx.ui.custom<boolean>(
			(tui, theme, _keys, done) => {
				cancel = () => done(false);
				const view = createNativeSetupApprovalView(
					tui,
					theme,
					title,
					details,
					done,
					cancelShortcut,
					summary,
					approveLabel,
				);
				if (signal?.aborted) queueMicrotask(cancel);
				return view;
			},
			{ blocked: { kind: "permission", message: title } },
		);
	} finally {
		signal?.removeEventListener("abort", abort);
	}
}

export function createNativeSetupApprovalView(
	tui: TUI,
	theme: Pick<Theme, "fg" | "bold">,
	title: string,
	details: string,
	done: (approved: boolean) => void,
	cancelShortcut?: KeyId,
	summary?: string,
	approveLabel = "continue",
): Component {
	const clean = (value: string) =>
		stripTerminalSequences(value).replace(
			/[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/gu,
			" ",
		);
	const safe = clean(details);
	const concise = summary === undefined ? undefined : clean(summary);
	let expanded = concise === undefined;
	const text = new Text("", 1, 0);
	const update = () =>
		text.setText(
			[
				theme.fg("accent", theme.bold(title)),
				"",
				expanded ? safe : concise,
				"",
				theme.fg(
					"muted",
					`y ${approveLabel} · n/Esc cancel${concise === undefined ? "" : ` · Tab ${expanded ? "less" : "details"}`}${cancelShortcut ? ` · ${cancelShortcut} cancel` : ""}`,
				),
			].join("\n"),
		);
	update();
	const scroll = new ScrollView(text, { primary: true, scrollbar: "auto" });
	return Object.assign(scroll, {
		handleInput(data: string) {
			if (cancelShortcut && matchesKey(data, cancelShortcut)) {
				done(false);
				return;
			}
			if (
				(data === "d" || data === "D" || matchesKey(data, Key.tab)) &&
				concise !== undefined
			) {
				expanded = !expanded;
				update();
				scroll.scrollToStart();
				tui.requestRender();
				return;
			}
			if (data === "y" || data === "Y") {
				done(true);
				return;
			}
			if (data === "n" || data === "N" || matchesKey(data, Key.escape)) {
				done(false);
				return;
			}
			if (data === "j" || matchesKey(data, Key.down)) scroll.scrollBy(1);
			else if (data === "k" || matchesKey(data, Key.up)) scroll.scrollBy(-1);
			else if (data === "g") scroll.scrollToStart();
			else if (data === "G") scroll.scrollToEnd();
			tui.requestRender();
		},
	});
}