import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import { Container } from "@mariozechner/pi-tui"; // Parse CSI ? 997 ; N n response (colour scheme notification). function parseColourSchemeNotification(data: string): "light" | "dark" | null { const match = data.match(/^\x1b\[\?997;(\d)n$/); if (!match) return null; const value = match[1]; if (value === "1") return "dark"; if (value === "2") return "light"; return null; } export default function (pi: ExtensionAPI) { let currentTheme: "dark" | "light" | undefined; let initialized = false; pi.on("session_start", (event, ctx) => { if (!ctx.hasUI || initialized) return; initialized = true; // Listen for future colour scheme changes. ctx.ui.onTerminalInput((data) => { const newTheme = parseColourSchemeNotification(data); if (!newTheme) return undefined; if (currentTheme !== newTheme) { currentTheme = newTheme; ctx.ui.setTheme(newTheme); ctx.ui.notify(`Switched to ${newTheme} theme`, "info"); } return undefined; }); // HACK: get access to the terminal object. ctx.ui.setWidget("auto-theme", (tui) => { // Enable unsolicited palette change notifications. tui.terminal.write("\x1b[?2031h"); // Query current colour scheme (CSI ? 996 n). tui.terminal.write("\x1b[?996n"); return new Container(); }); }); }