import { basename } from "node:path"; import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; function setTerminalTitle(pi: ExtensionAPI, ctx: ExtensionContext): void { const cwd = basename(ctx.cwd); const session = pi.getSessionName(); ctx.ui.setTitle(session ? `π - ${session} - ${cwd}` : `π - ${cwd}`); } export default function terminalTitleExtension(pi: ExtensionAPI): void { let restoreTitle: (() => void) | undefined; function removeSignalListeners(): void { if (!restoreTitle) { return; } process.off("SIGCONT", restoreTitle); process.off("SIGCHLD", restoreTitle); restoreTitle = undefined; } pi.on("session_start", (_event, ctx) => { removeSignalListeners(); if (ctx.mode !== "tui") { return; } restoreTitle = () => setTerminalTitle(pi, ctx); restoreTitle(); process.on("SIGCONT", restoreTitle); // Pi has no external-editor lifecycle event. The editor is a direct // child, so its exit is the earliest reliable point to restore the title. process.on("SIGCHLD", restoreTitle); }); pi.on("session_info_changed", (_event, ctx) => { if (ctx.mode === "tui") { setTerminalTitle(pi, ctx); } }); pi.on("session_shutdown", removeSignalListeners); }