feat: add herdr & pi auto theme
This commit is contained in:
1 parent
658ef3f3ed
commit
5af8007804
6 files changed
+101
-2
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// @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;
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user