diff --git a/packages/coding-agent/src/tui/footer.ts b/packages/coding-agent/src/tui/footer.ts index bc45dc05..05e7bf31 100644 --- a/packages/coding-agent/src/tui/footer.ts +++ b/packages/coding-agent/src/tui/footer.ts @@ -1,7 +1,7 @@ import type { AgentState } from "@mariozechner/pi-agent-core"; import type { AssistantMessage } from "@mariozechner/pi-ai"; import { type Component, visibleWidth } from "@mariozechner/pi-tui"; -import { readFileSync } from "fs"; +import { existsSync, type FSWatcher, readFileSync, watch } from "fs"; import { join } from "path"; import { theme } from "../theme/theme.js"; @@ -11,11 +11,56 @@ import { theme } from "../theme/theme.js"; export class FooterComponent implements Component { private state: AgentState; private cachedBranch: string | null | undefined = undefined; // undefined = not checked yet, null = not in git repo, string = branch name + private gitWatcher: FSWatcher | null = null; + private onBranchChange: (() => void) | null = null; constructor(state: AgentState) { this.state = state; } + /** + * Set up a file watcher on .git/HEAD to detect branch changes. + * Call the provided callback when branch changes. + */ + watchBranch(onBranchChange: () => void): void { + this.onBranchChange = onBranchChange; + this.setupGitWatcher(); + } + + private setupGitWatcher(): void { + // Clean up existing watcher + if (this.gitWatcher) { + this.gitWatcher.close(); + this.gitWatcher = null; + } + + const gitHeadPath = join(process.cwd(), ".git", "HEAD"); + if (!existsSync(gitHeadPath)) { + return; + } + + try { + this.gitWatcher = watch(gitHeadPath, () => { + this.cachedBranch = undefined; // Invalidate cache + if (this.onBranchChange) { + this.onBranchChange(); + } + }); + } catch { + // Silently fail if we can't watch + } + } + + /** + * Clean up the file watcher + */ + dispose(): void { + if (this.gitWatcher) { + this.gitWatcher.close(); + this.gitWatcher = null; + } + } + updateState(state: AgentState): void { this.state = state; } diff --git a/packages/coding-agent/src/tui/tui-renderer.ts b/packages/coding-agent/src/tui/tui-renderer.ts index 6aa9c604..887fc18a 100644 --- a/packages/coding-agent/src/tui/tui-renderer.ts +++ b/packages/coding-agent/src/tui/tui-renderer.ts @@ -464,6 +464,11 @@ export class TuiRenderer { this.updateEditorBorderColor(); this.ui.requestRender(); }); + + // Set up git branch watcher + this.footer.watchBranch(() => { + this.ui.requestRender(); + }); } private subscribeToAgent(): void { @@ -1523,6 +1528,7 @@ export class TuiRenderer { this.loadingAnimation.stop(); this.loadingAnimation = null; } + this.footer.dispose(); if (this.isInitialized) { this.ui.stop(); this.isInitialized = false;