From c510eec9f84e4582714a677d228a32eb9d2a867f Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Tue, 10 Feb 2026 21:33:49 -0300 Subject: [PATCH] feat(tui): fallback to active theme when configured theme is unavailable --- codex-rs/tui/src/theme_picker.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/codex-rs/tui/src/theme_picker.rs b/codex-rs/tui/src/theme_picker.rs index 3714faed1d..18fbd0461c 100644 --- a/codex-rs/tui/src/theme_picker.rs +++ b/codex-rs/tui/src/theme_picker.rs @@ -194,12 +194,16 @@ pub(crate) fn build_theme_picker_params( let entries = highlight::list_available_themes(codex_home); let codex_home_owned = codex_home.map(|p| p.to_path_buf()); - // Resolve the effective theme name: honor explicit config, fall back to - // the auto-detected default so the picker pre-selects even when no theme - // is configured. - let effective_name = current_name - .map(str::to_string) - .unwrap_or_else(highlight::current_theme_name); + // Resolve the effective theme name: honor explicit config only when it is + // currently available; otherwise fall back to the active runtime theme so + // opening `/theme` does not auto-preview an unrelated first entry. + let effective_name = if let Some(name) = current_name + && entries.iter().any(|entry| entry.name == name) + { + name.to_string() + } else { + highlight::current_theme_name() + }; // Track the index of the current theme so we can pre-select it. let mut initial_idx = None; @@ -407,4 +411,19 @@ mod tests { assert_eq!(subtitle, PREVIEW_FALLBACK_SUBTITLE); } + + #[test] + fn unavailable_configured_theme_falls_back_to_active_theme_selection() { + let active_theme = highlight::current_theme_name(); + let params = build_theme_picker_params(Some("not-a-real-theme"), None, Some(120)); + let selected_idx = params + .initial_selected_idx + .expect("expected selected index for active fallback theme"); + let selected_name = params.items[selected_idx] + .search_value + .as_deref() + .expect("expected search value to contain canonical theme name"); + + assert_eq!(selected_name, active_theme); + } }