forked from earendil-works/pi
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmac-system-theme.ts
More file actions
47 lines (40 loc) · 1.17 KB
/
Copy pathmac-system-theme.ts
File metadata and controls
47 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Syncs pi theme with macOS system appearance (dark/light mode).
*
* Usage:
* pi -e examples/extensions/mac-system-theme.ts
*/
import { exec } from "node:child_process";
import { promisify } from "node:util";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
const execAsync = promisify(exec);
async function isDarkMode(): Promise<boolean> {
try {
const { stdout } = await execAsync(
"osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'",
);
return stdout.trim() === "true";
} catch {
return false;
}
}
export default function (pi: ExtensionAPI) {
let intervalId: ReturnType<typeof setInterval> | null = null;
pi.on("session_start", async (_event, ctx) => {
let currentTheme = (await isDarkMode()) ? "dark" : "light";
ctx.ui.setTheme(currentTheme);
intervalId = setInterval(async () => {
const newTheme = (await isDarkMode()) ? "dark" : "light";
if (newTheme !== currentTheme) {
currentTheme = newTheme;
ctx.ui.setTheme(currentTheme);
}
}, 2000);
});
pi.on("session_shutdown", () => {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
});
}