66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
// @ts-nocheck
|
|
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 checkTheme(): Promise<{ dark: boolean; error?: string }> {
|
|
try {
|
|
if (process.platform === "darwin") {
|
|
const { stdout } = await execAsync(
|
|
"osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'",
|
|
);
|
|
return { dark: stdout.trim() === "true" };
|
|
} else if (process.platform === "linux") {
|
|
const { stdout } = await execAsync(
|
|
"dbus-send --session --print-reply --dest=org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read string:'org.freedesktop.appearance' string:'color-scheme'",
|
|
);
|
|
// 1 is Dark, 2 is Light, 0 is Default
|
|
return { dark: stdout.includes("uint32 1") };
|
|
}
|
|
return { dark: true, error: "Unsupported OS" };
|
|
} catch (e: any) {
|
|
return { dark: true, error: e.message.split("\n")[0] }; // First line of error
|
|
}
|
|
}
|
|
|
|
export default function (pi: ExtensionAPI) {
|
|
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
|
|
const updateTheme = async (ctx: any, currentTheme: string) => {
|
|
const res = await checkTheme();
|
|
const newTheme = res.dark ? "catppuccin-tui-mocha" : "catppuccin-tui-latte";
|
|
|
|
if (res.error) {
|
|
ctx.ui.setStatus(
|
|
"auto-theme",
|
|
ctx.ui.theme.fg("error", `theme err: ${res.error}`),
|
|
);
|
|
} else {
|
|
ctx.ui.setStatus("auto-theme", ctx.ui.theme.fg("muted", "theme auto"));
|
|
}
|
|
|
|
if (newTheme !== currentTheme) {
|
|
ctx.ui.setTheme(newTheme);
|
|
}
|
|
return newTheme;
|
|
};
|
|
|
|
pi.on("session_start", async (_event, ctx) => {
|
|
let currentTheme = "";
|
|
currentTheme = await updateTheme(ctx, currentTheme);
|
|
|
|
intervalId = setInterval(async () => {
|
|
currentTheme = await updateTheme(ctx, currentTheme);
|
|
}, 2000);
|
|
});
|
|
|
|
pi.on("session_shutdown", () => {
|
|
if (intervalId) {
|
|
clearInterval(intervalId);
|
|
intervalId = null;
|
|
}
|
|
});
|
|
}
|