Make GOOSE_CLI_THEME persist to config file and stick for future session (#2099)

This commit is contained in:
sana-db
2025-04-16 21:41:29 -07:00
committed by GitHub
parent a040393fed
commit 66bfcc0e55
+29 -12
View File
@@ -25,26 +25,43 @@ impl Theme {
Theme::Ansi => "base16",
}
}
fn from_config_str(val: &str) -> Self {
if val.eq_ignore_ascii_case("light") {
Theme::Light
} else if val.eq_ignore_ascii_case("ansi") {
Theme::Ansi
} else {
Theme::Dark
}
}
fn as_config_string(&self) -> String {
match self {
Theme::Light => "light".to_string(),
Theme::Dark => "dark".to_string(),
Theme::Ansi => "ansi".to_string(),
}
}
}
thread_local! {
static CURRENT_THEME: RefCell<Theme> = RefCell::new(
std::env::var("GOOSE_CLI_THEME")
.ok()
.map(|val| {
if val.eq_ignore_ascii_case("light") {
Theme::Light
} else if val.eq_ignore_ascii_case("ansi") {
Theme::Ansi
} else {
Theme::Dark
}
})
.unwrap_or(Theme::Dark)
std::env::var("GOOSE_CLI_THEME").ok()
.map(|val| Theme::from_config_str(&val))
.unwrap_or_else(||
Config::global().get_param::<String>("GOOSE_CLI_THEME").ok()
.map(|val| Theme::from_config_str(&val))
.unwrap_or(Theme::Dark)
)
);
}
pub fn set_theme(theme: Theme) {
let config = Config::global();
config
.set_param("GOOSE_CLI_THEME", Value::String(theme.as_config_string()))
.expect("Failed to set theme");
CURRENT_THEME.with(|t| *t.borrow_mut() = theme);
}