This repository has no description
1.0 kB
41 lines
1import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
2import { readFileSync, watch, type FSWatcher } from "node:fs";
3
4const THEME_FILE = "/var/theme/current_theme";
5
6function readTheme(): "dark" | "light" | null {
7 try {
8 const value = readFileSync(THEME_FILE, "utf-8").trim();
9 if (value === "dark" || value === "light") return value;
10 return null;
11 } catch {
12 return null;
13 }
14}
15
16export default function (pi: ExtensionAPI) {
17 let watcher: FSWatcher | null = null;
18
19 pi.on("session_start", async (_event, ctx) => {
20 // Set initial theme
21 const scheme = readTheme();
22 if (scheme) ctx.ui.setTheme(scheme);
23
24 // Watch for changes
25 try {
26 watcher = watch(THEME_FILE, () => {
27 const newScheme = readTheme();
28 if (newScheme) ctx.ui.setTheme(newScheme);
29 });
30 } catch {
31 // file doesn't exist yet, ignore
32 }
33 });
34
35 pi.on("resources_discover", async () => {
36 if (watcher) {
37 watcher.close();
38 watcher = null;
39 }
40 });
41}