import { type ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { matchesKey } from "@earendil-works/pi-tui"; import { createHeaderComponent, type HeaderComponent } from "./header.js"; import { getCompatibleAnimations, randomItem, registerLogoDemoCommand, STATIC_ANIMATION } from "./logo-demo.js"; import { logos } from "./logos/logos.js"; const ENABLE_LOGO_DEMO_COMMAND = false; export default function registerHeaderExtension(pi: ExtensionAPI) { let activeHeader: HeaderComponent | undefined; const stopLogoDemo = ENABLE_LOGO_DEMO_COMMAND ? registerLogoDemoCommand(pi, () => activeHeader) : () => undefined; pi.on("session_shutdown", async () => { stopLogoDemo(); activeHeader?.dispose(); activeHeader = undefined; }); pi.on("session_start", async (event, ctx) => { if (!ctx.hasUI) return; activeHeader?.dispose(); activeHeader = undefined; const logo = randomItem(logos); const shouldAnimate = event.reason === "startup" || event.reason === "new"; const animation = shouldAnimate ? randomItem(getCompatibleAnimations(logo)) : STATIC_ANIMATION; let header: HeaderComponent | undefined; if (!animation) return; ctx.ui.setHeader((tui, theme) => { header = createHeaderComponent({ tui, theme, logoLines: logo.lines, animation, }); activeHeader = header; return header; }); ctx.ui.onTerminalInput((data) => { if (matchesKey(data, "enter")) { header?.stopAnimation(); } return undefined; }); }); }