Download packages/coding-agent/examples/extensions/mac-system-theme.ts from SaylorTwift/pi: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/SaylorTwift/pi/resolve/main/packages/coding-agent/examples/extensions/mac-system-theme.ts
- Command line
-
hf download hf://SaylorTwift/pi/packages/coding-agent/examples/extensions/mac-system-theme.ts
-
curl -L -o mac-system-theme.ts https://huggingface.co/SaylorTwift/pi/resolve/main/packages/coding-agent/examples/extensions/mac-system-theme.ts
1.2 kB
| /** | |
| * 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; | |
| } | |
| }); | |
| } | |