Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
fix(theme): make terminal theme autodetection actually run
Samir Hossains9a92f867a9dc
diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rsindex 68c76c0..948867c 100644--- a/src/tui/app/mod.rs+++ b/src/tui/app/mod.rs@@ -79,16 +79,28 @@ impl App {state.download_dir = config.download_dir.map(std::path::PathBuf::from);state.installed_addons = crate::config::load_addons();- let theme_str = std::env::var("MOVIEBOX_THEME").unwrap_or_else(|_| {- if state.active_theme_kind.is_empty() {- "Mocha".to_string()- } else {- state.active_theme_kind.clone()+ let env_theme = std::env::var("MOVIEBOX_THEME")+ .ok()+ .map(|value| value.trim().to_string())+ .filter(|value| !value.is_empty());+ let config_theme = if state.active_theme_kind.is_empty() {+ None+ } else {+ Some(state.active_theme_kind.clone())+ };+ let theme = match env_theme.or(config_theme) {+ Some(explicit) => {+ state.theme_is_auto = false;+ let theme_kind = crate::tui::theme::ThemeKind::parse(&explicit);+ state.active_theme_kind = theme_kind.as_str().to_string();+ crate::tui::theme::Theme::from_kind(theme_kind)}- });- let theme_kind = crate::tui::theme::ThemeKind::parse(&theme_str);- state.active_theme_kind = theme_kind.as_str().to_string();- let theme = crate::tui::theme::Theme::from_kind(theme_kind);+ None => {+ state.theme_is_auto = true;+ state.active_theme_kind.clear();+ crate::tui::theme::Theme::detect()+ }+ };let service = std::sync::Arc::new(crate::service::MovieBoxService::new());if service.fourk_client.is_none() {diff --git a/src/tui/app/run.rs b/src/tui/app/run.rsindex a1a061e..eddd826 100644--- a/src/tui/app/run.rs+++ b/src/tui/app/run.rs@@ -21,6 +21,7 @@ impl App {tokio::task::spawn_blocking(|| {let options = ratatui_image::picker::cap_parser::QueryStdioOptions {timeout: Duration::from_millis(400),+ terminal_background_color_osc: true,..Default::default()};ratatui_image::picker::Picker::from_query_stdio_with_options(options).ok()@@ -30,6 +31,25 @@ impl App {} else {None};+ if self.state.theme_is_auto+ && let Some(background) = picker.as_ref().and_then(|p| {+ p.capabilities()+ .iter()+ .find_map(|capability| match capability {+ ratatui_image::picker::Capability::Background(red, green, blue) => {+ Some((*red, *green, *blue))+ }+ _ => None,+ })+ })+ {+ let luminance = 0.2126 * f32::from(background.0)+ + 0.7152 * f32::from(background.1)+ + 0.0722 * f32::from(background.2);+ let is_light = luminance > 128.0;+ self.theme = crate::tui::theme::Theme::detect_with_light(Some(is_light));+ self.state.active_theme_kind = if is_light { "Latte" } else { "Mocha" }.to_string();+ }if let Some(picker) = picker&& !matches!(picker.protocol_type(),diff --git a/src/tui/app/system.rs b/src/tui/app/system.rsindex 7251bb4..ccf7fe9 100644--- a/src/tui/app/system.rs+++ b/src/tui/app/system.rs@@ -320,9 +320,16 @@ impl App {}Action::SelectTheme(theme_name) => {- let kind = crate::tui::theme::ThemeKind::parse(&theme_name);- self.state.active_theme_kind = kind.as_str().to_string();- self.theme = crate::tui::theme::Theme::from_kind(kind);+ if theme_name.is_empty() && self.state.theme_is_auto {+ // Esc from the picker with no explicit theme configured:+ // restore the auto-detected theme instead of forcing Mocha.+ self.theme = crate::tui::theme::Theme::detect();+ } else {+ let kind = crate::tui::theme::ThemeKind::parse(&theme_name);+ self.state.active_theme_kind = kind.as_str().to_string();+ self.state.theme_is_auto = false;+ self.theme = crate::tui::theme::Theme::from_kind(kind);+ }if !self.state.show_theme_popup {self.persist_config();}diff --git a/src/tui/state.rs b/src/tui/state.rsindex 588bc2c..34c8fd0 100644--- a/src/tui/state.rs+++ b/src/tui/state.rs@@ -93,6 +93,7 @@ pub struct AppState {pub show_theme_popup: bool,pub active_theme_kind: String,+ pub theme_is_auto: bool,pub original_theme_kind: Option<String>,pub theme_list_state: ListState,pub show_browse_popup: bool,@@ -241,6 +242,7 @@ impl Default for AppState {tick_count: 0,poster_image: None,active_theme_kind: String::new(),+ theme_is_auto: true,original_theme_kind: None,show_theme_popup: false,theme_list_state: ListState::default(),diff --git a/src/tui/theme.rs b/src/tui/theme.rsindex aaeb5ca..d8e41c8 100644--- a/src/tui/theme.rs+++ b/src/tui/theme.rs@@ -110,6 +110,45 @@ impl Default for Theme {}}+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ColorSupport {+ NoColor,+ Truecolor,+ Color256,+ Basic,+}++pub(crate) fn classify_terminal(colorterm: &str, term: &str, term_program: &str) -> ColorSupport {+ let colorterm = colorterm.to_lowercase();+ let term = term.to_lowercase();+ let truecolor = colorterm == "truecolor"+ || colorterm == "24bit"+ || term.contains("truecolor")+ || term.contains("kitty")+ || term.contains("ghostty")+ || std::env::var("WT_SESSION").is_ok()+ || term_program == "iTerm.app"+ || term_program == "Hyper"+ || term_program == "Tabby"+ || term_program == "WezTerm"+ || term_program == "WarpTerminal"+ || term_program == "vscode"+ || term_program == "ghostty";+ if truecolor {+ return ColorSupport::Truecolor;+ }+ let basic = term == "dumb" || term == "linux" || term.contains("fbterm");+ let apple_term = term.contains("apple") || term_program == "Apple_Terminal";+ let has_256 = term.contains("256") || term.contains("xterm") || term.contains("screen");+ if basic || (apple_term && !has_256) || term == "xterm" {+ ColorSupport::Basic+ } else if has_256 {+ ColorSupport::Color256+ } else {+ ColorSupport::Basic+ }+}+impl Theme {pub fn mocha() -> Self {Self {@@ -214,57 +253,52 @@ impl Theme {ThemeKind::Mocha => Self::mocha(),}}+}- pub fn new() -> Self {- let colorterm = std::env::var("COLORTERM")- .unwrap_or_default()- .to_lowercase();- let term = std::env::var("TERM").unwrap_or_default().to_lowercase();- let term_program = std::env::var("TERM_PROGRAM").unwrap_or_default();- let has_no_color = std::env::var("NO_COLOR").is_ok();- let is_light = crate::tui::terminal::background_is_light();-- if has_no_color {- return Self::monochrome(is_light);- }+impl Theme {+ /// Detects the best theme for the current terminal.+ ///+ /// Explicit user choices (`MOVIEBOX_THEME` / saved config) are resolved by+ /// the caller and take precedence; this path only runs when no explicit+ /// choice exists.+ pub fn detect() -> Self {+ Self::detect_with_light(None)+ }- if let Ok(theme_env) = std::env::var("MOVIEBOX_THEME") {- return Self::from_kind(ThemeKind::parse(&theme_env));+ /// Like [`Theme::detect`] but overrides the light/dark decision (for+ /// example from an OSC 11 background-color query).+ pub fn detect_with_light(light: Option<bool>) -> Self {+ let resolved_light = light.unwrap_or_else(crate::tui::terminal::background_is_light);+ if std::env::var("NO_COLOR").is_ok() {+ return Self::monochrome(resolved_light);}+ let colorterm = std::env::var("COLORTERM").unwrap_or_default();+ let term = std::env::var("TERM").unwrap_or_default();+ let term_program = std::env::var("TERM_PROGRAM").unwrap_or_default();+ Self::detect_from(+ classify_terminal(&colorterm, &term, &term_program),+ resolved_light,+ )+ }- let truecolor = colorterm == "truecolor"- || colorterm == "24bit"- || term.contains("truecolor")- || term.contains("kitty")- || std::env::var("WT_SESSION").is_ok()- || term_program == "iTerm.app"- || term_program == "Hyper"- || term_program == "Tabby"- || term_program == "WezTerm"- || term_program == "WarpTerminal"- || term_program == "vscode"- || term_program == "ghostty";-- let basic = term == "dumb" || term == "linux" || term.contains("fbterm");- let apple_term = term.contains("apple") || term_program == "Apple_Terminal";- let has_256 = term.contains("256") || term.contains("xterm") || term.contains("screen");-- if truecolor {- if is_light {- Self::latte()- } else {- Self::mocha()+ pub(crate) fn detect_from(support: ColorSupport, is_light: bool) -> Self {+ match support {+ ColorSupport::NoColor => Self::monochrome(is_light),+ ColorSupport::Truecolor => {+ if is_light {+ Self::latte()+ } else {+ Self::mocha()+ }}- } else if basic || (apple_term && !has_256) || term == "xterm" {- Self::fallback(is_light)- } else if has_256 {- if is_light {- Self::latte().quantized_to_256()- } else {- Self::mocha().quantized_to_256()+ ColorSupport::Color256 => {+ if is_light {+ Self::latte().quantized_to_256()+ } else {+ Self::mocha().quantized_to_256()+ }}- } else {- Self::fallback(is_light)+ ColorSupport::Basic => Self::fallback(is_light),}}@@ -838,4 +872,55 @@ mod tests {let mono = Theme::monochrome(false);assert_eq!(mono.text.fg, Some(Color::White));}++ #[test]+ fn classify_maps_common_terminals() {+ use crate::tui::theme::ColorSupport;+ assert_eq!(+ classify_terminal("truecolor", "xterm-256color", ""),+ ColorSupport::Truecolor+ );+ assert_eq!(+ classify_terminal("", "xterm-kitty", ""),+ ColorSupport::Truecolor+ );+ assert_eq!(+ classify_terminal("", "xterm-ghostty", ""),+ ColorSupport::Truecolor+ );+ assert_eq!(+ classify_terminal("", "xterm-256color", "ghostty"),+ ColorSupport::Truecolor+ );+ assert_eq!(+ classify_terminal("", "xterm-256color", ""),+ ColorSupport::Color256+ );+ assert_eq!(+ classify_terminal("", "screen.xterm-256color", ""),+ ColorSupport::Color256+ );+ assert_eq!(classify_terminal("", "vt100", ""), ColorSupport::Basic);+ assert_eq!(classify_terminal("", "dumb", ""), ColorSupport::Basic);+ assert_eq!(+ classify_terminal("", "xterm", "Apple_Terminal"),+ ColorSupport::Basic+ );+ }++ #[test]+ fn detect_from_matches_support_matrix() {+ let quantized = Theme::detect_from(ColorSupport::Color256, false);+ for style in [quantized.border, quantized.accent, quantized.bg] {+ assert!(+ style+ .fg+ .map(|c| !matches!(c, Color::Rgb(..)))+ .unwrap_or(true)+ );+ }+ assert!(!Theme::detect_from(ColorSupport::Truecolor, false).is_light);+ assert!(Theme::detect_from(ColorSupport::Truecolor, true).is_light);+ assert!(!Theme::detect_from(ColorSupport::NoColor, false).is_light);+ }}