Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion extensions/theme-cycler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@
* Usage: pi -e extensions/theme-cycler.ts -e extensions/minimal.ts
*/

import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import { truncateToWidth } from "@mariozechner/pi-tui";
import { applyExtensionDefaults } from "./themeMap.ts";
import { applyExtensionDefaults } from "./lib/themeMap.ts";

export default function (pi: ExtensionAPI) {
function persistTheme(themeName: string): void {
try {
const settingsPath = path.join(os.homedir(), ".pi", "agent", "settings.json");
const raw = fs.existsSync(settingsPath) ? fs.readFileSync(settingsPath, "utf-8") : "{}";
const settings = JSON.parse(raw);
settings.theme = themeName;
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n", "utf-8");
} catch {
// best-effort
}
}

let currentCtx: ExtensionContext | undefined;
let swatchTimer: ReturnType<typeof setTimeout> | null = null;

Expand Down Expand Up @@ -94,6 +109,7 @@ export default function (pi: ExtensionAPI) {
index = (index + direction + themes.length) % themes.length;
const theme = themes[index];
const result = ctx.ui.setTheme(theme.name);
persistTheme(theme.name);

if (result.success) {
updateStatus(ctx);
Expand Down Expand Up @@ -135,6 +151,7 @@ export default function (pi: ExtensionAPI) {

if (arg) {
const result = ctx.ui.setTheme(arg);
persistTheme(arg);
if (result.success) {
updateStatus(ctx);
showSwatch(ctx);
Expand All @@ -156,6 +173,7 @@ export default function (pi: ExtensionAPI) {

const selectedName = selected.split(/\s/)[0];
const result = ctx.ui.setTheme(selectedName);
persistTheme(selectedName);
if (result.success) {
updateStatus(ctx);
showSwatch(ctx);
Expand Down
16 changes: 15 additions & 1 deletion extensions/themeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
*/

import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
import { basename } from "path";
import { basename, join } from "path";
import { fileURLToPath } from "url";
import { homedir } from "os";
import * as fs from "fs";

// ── Theme assignments ──────────────────────────────────────────────────────
//
Expand Down Expand Up @@ -71,6 +73,18 @@ export function applyExtensionTheme(fileUrl: string, ctx: ExtensionContext): boo
return true; // Pretend we succeeded, but don't overwrite the primary theme
}

// Always honour the user's saved theme preference over themeMap defaults
try {
const settingsPath = join(homedir(), ".pi", "agent", "settings.json");
if (fs.existsSync(settingsPath)) {
const settings = JSON.parse(fs.readFileSync(settingsPath, "utf-8"));
if (settings.theme) {
return ctx.ui.setTheme(settings.theme).success;
}
}
} catch {}

// No saved preference — fall back to themeMap default
let themeName = THEME_MAP[name];

if (!themeName) {
Expand Down