2020import type { ExtensionAPI , ExtensionContext } from "@mariozechner/pi-coding-agent" ;
2121import { truncateToWidth } from "@mariozechner/pi-tui" ;
2222import { applyExtensionDefaults } from "./themeMap.ts" ;
23+ import * as fs from "fs" ;
24+ import * as path from "path" ;
25+ import * as os from "os" ;
2326
2427export default function ( pi : ExtensionAPI ) {
2528 let currentCtx : ExtensionContext | undefined ;
@@ -79,6 +82,19 @@ export default function (pi: ExtensionAPI) {
7982 return themes . findIndex ( ( t ) => t . name === current ) ;
8083 }
8184
85+ /** Persist the selected theme name to ~/.pi/agent/settings.json */
86+ function persistTheme ( themeName : string ) : void {
87+ try {
88+ const settingsPath = path . join ( os . homedir ( ) , ".pi" , "agent" , "settings.json" ) ;
89+ const raw = fs . existsSync ( settingsPath ) ? fs . readFileSync ( settingsPath , "utf-8" ) : "{}" ;
90+ const settings = JSON . parse ( raw ) ;
91+ settings . theme = themeName ;
92+ fs . writeFileSync ( settingsPath , JSON . stringify ( settings , null , 2 ) + "\n" , "utf-8" ) ;
93+ } catch {
94+ // best-effort
95+ }
96+ }
97+
8298 function cycleTheme ( ctx : ExtensionContext , direction : 1 | - 1 ) {
8399 if ( ! ctx . hasUI ) return ;
84100
@@ -96,6 +112,7 @@ export default function (pi: ExtensionAPI) {
96112 const result = ctx . ui . setTheme ( theme . name ) ;
97113
98114 if ( result . success ) {
115+ persistTheme ( theme . name ) ;
99116 updateStatus ( ctx ) ;
100117 showSwatch ( ctx ) ;
101118 ctx . ui . notify ( `${ theme . name } (${ index + 1 } /${ themes . length } )` , "info" ) ;
@@ -136,6 +153,7 @@ export default function (pi: ExtensionAPI) {
136153 if ( arg ) {
137154 const result = ctx . ui . setTheme ( arg ) ;
138155 if ( result . success ) {
156+ persistTheme ( arg ) ;
139157 updateStatus ( ctx ) ;
140158 showSwatch ( ctx ) ;
141159 ctx . ui . notify ( `Theme: ${ arg } ` , "info" ) ;
@@ -157,6 +175,7 @@ export default function (pi: ExtensionAPI) {
157175 const selectedName = selected . split ( / \s / ) [ 0 ] ;
158176 const result = ctx . ui . setTheme ( selectedName ) ;
159177 if ( result . success ) {
178+ persistTheme ( selectedName ) ;
160179 updateStatus ( ctx ) ;
161180 showSwatch ( ctx ) ;
162181 ctx . ui . notify ( `Theme: ${ selectedName } ` , "info" ) ;
0 commit comments