Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/afk/src/widget.ts

Raw
import type { Theme } from "@earendil-works/pi-coding-agent";
import {
	type Component,
	rgbColor,
	type TUI,
	truncateToWidth,
} from "@earendil-works/pi-tui";
import { type CatMood, colors, sprite } from "./cat.ts";

export type { CatMood } from "./cat.ts";

export type CatState = { goal: string; mood: CatMood; activityAt: number };
export type AfkWidget = Component & { dispose(): void };
const labels: Record<CatMood, string> = {
	curious: "finding your intention",
	thinking: "examining evidence",
	watchful: "keeping watch",
	angry: "unfinished business",
	hungry: "waiting for evidence",
	bored: "still waiting",
	pleased: "objection addressed",
	napping: "review settled",
	concerned: "review incomplete",
};

export function createAfkWidget(
	tui: TUI,
	theme: Theme,
	state: () => CatState,
): AfkWidget {
	let disposed = false;
	let timer: ReturnType<typeof setTimeout> | undefined;
	const started = Date.now();
	const palette = Object.fromEntries(
		Object.entries(colors).map(([key, rgb]) => [
			key,
			rgbColor(rgb[0], rgb[1], rgb[2]),
		]),
	) as Record<keyof typeof colors, ReturnType<typeof rgbColor>>;
	const schedule = () => {
		if (disposed || timer) return;
		const mood = state().mood;
		timer = setTimeout(
			() => {
				timer = undefined;
				if (disposed) return;
				tui.requestRender();
				schedule();
			},
			mood === "napping" ||
				mood === "concerned" ||
				(mood === "pleased" && Date.now() - state().activityAt > 2500)
				? 1000
				: 125,
		);
		timer.unref?.();
	};
	return {
		render(width) {
			if (disposed) return [];
			schedule();
			const current = state();
			const elapsed = Date.now() - current.activityAt;
			let mood = current.mood;
			if (mood === "watchful" && elapsed > 45000) mood = "bored";
			if (mood === "angry" && elapsed > 2500) mood = "hungry";
			if (mood === "pleased" && elapsed > 2500) mood = "napping";
			const pixels = sprite(mood, (Date.now() - started) / 1000);
			const lines = [
				truncateToWidth(theme.fg("accent", current.goal), Math.max(0, width)),
				truncateToWidth(
					theme.fg(mood === "concerned" ? "warning" : "muted", labels[mood]),
					Math.max(0, width),
				),
			];
			for (let y = 0; y < pixels.length; y += 2) {
				let line = "";
				const upper = pixels[y] ?? [];
				const lower = pixels[y + 1];
				for (let x = 0; x < Math.min(upper.length, width); x++) {
					const top = upper[x];
					const bottom = lower?.[x];
					if (top && bottom) {
						line += theme.style("▀", { fg: palette[top], bg: palette[bottom] });
					} else if (top) {
						line += theme.style("▀", { fg: palette[top] });
					} else if (bottom) {
						line += theme.style("▄", { fg: palette[bottom] });
					} else {
						line += " ";
					}
				}
				lines.push(line);
			}
			// Sprite columns are single-cell blocks/spaces, already bounded by width.
			// Avoid rescanning every per-cell ANSI sequence for terminal width.
			return lines;
		},
		invalidate() {
			if (!disposed) {
				clearTimeout(timer);
				timer = undefined;
				schedule();
				tui.requestRender();
			}
		},
		dispose() {
			disposed = true;
			clearTimeout(timer);
			timer = undefined;
		},
	};
}