Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
fix(tui): isolate tv search dispatch and balance landing layout
Samir Hossains34150129f12e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9ea1ee5..7f9792a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -37,9 +37,17 @@- **Settings Modal Balanced Geometry & Dynamic Height**:- Auto-sized `/settings` modal height dynamically to fit the exact row count of the active category and balanced modal width to 60 columns in `src/tui/overlay.rs` and `src/tui/widgets/settings.rs`, removing the oversized right-side void and bottom blank areas while maintaining uniform margins around all borders.- **Modal Backdrop Dimming for Background Deck & Search**:- - Dimmed favorites/continue-watching card items and the search bar provider pill when modal popups (such as the provider picker) are active in `src/tui/screens/home.rs`, ensuring background elements properly recede visually.+ - Dimmed favorites/continue-watching card items, browse preset suggestions, and the search bar provider pill when modal popups (such as the provider picker) are active in `src/tui/screens/home.rs`, ensuring background elements properly recede visually.- **Update Check In-Flight Notification Replacement**:- Replaced the in-flight `"Checking for updates"` notification toast with the final outcome toast (`Up to date` or `Update check failed`) upon completion in `src/tui/app/system.rs`, avoiding duplicate stacked update notifications.+- **Live TV Search Isolation & Balanced Landing Geometry**:+ - Isolated `Action::Search` dispatch in `src/tui/app/requests.rs` to route TV mode queries directly through `apply_tv_search_results` instead of launching movie/series upstream scrapers against `active_provider` (MovieBox).+ - Reduced landing search bar width in Live TV mode to 48 columns (44 on compact viewports) in `src/tui/screens/home.rs`, centering the input cleanly under the header logo without the horizontal void left by the absent provider selector pill.+ - Suppressed the streaming provider badge (`[MovieBox]`) on channel search results in `src/tui/screens/home.rs`, maintaining visual separation between streaming catalogs and live TV channels.+ - Streamlined empty search feedback in `src/tui/screens/home.rs` to display `"No TV channels found matching “<query>”"` with a single `[ Clear Search (c) ]` action button, eliminating redundant playlist reload prompts.+- **Idempotent Mode Switching & Shortcut Isolation**:+ - Replaced `Action::ToggleTvMode` with dedicated `Action::SwitchToTvMode` in `src/tui/action.rs` and `src/tui/app/tv.rs`, aligning behavior symmetrically with `Action::SwitchToStreamingMode`.+ - Hardened `Ctrl+T` and `Ctrl+S` keystroke handling in `src/tui/app/keyboard.rs` to report status without toggling when already in the active mode (`"Already in TV Mode."` / `"Already in Streaming Mode."`), preventing unintended mode flips when pressing `Ctrl+T` while in Live TV mode.- **Media Player Stream Buffering & Readahead Tuning**:- Configured high-throughput buffering in `src/player.rs` for `mpv` and `IINA` (`--cache=yes`, `--cache-pause=yes`, `--cache-pause-wait=8`, `--cache-pause-initial=yes`, `--demuxer-max-bytes=256M`, `--demuxer-max-back-bytes=100M`, `--demuxer-readahead-secs=120`, `--demuxer-lavf-buffersize=1048576`, `--stream-buffer-size=512k`, `--force-seekable=yes`, and `--stream-lavf-o=reconnect=1,reconnect_streamed=1,reconnect_delay_max=5`), eliminating the 1-second re-buffering loop by enforcing an 8-second playback cushion, prefetching up to 2 minutes of stream data, and expanding memory cache size to 256MB.- Configured network caching and adaptive streaming in `src/player.rs` for `VLC` (`--network-caching=10000` and `--adaptive-logic=nearoptimal`), aligning the network buffer floor with the 10.3s MPEG-DASH `minBufferTime` specification and preventing playback stalls when CDN chunk delivery encounters transient packet jitter.diff --git a/src/tui/action.rs b/src/tui/action.rsindex a6f03b7..c3c4845 100644--- a/src/tui/action.rs+++ b/src/tui/action.rs@@ -9,7 +9,7 @@ pub enum Action {SelectLanguage(usize),Resize(u16, u16),ToggleHelp,- ToggleTvMode,+ SwitchToTvMode,SwitchToStreamingMode,SwitchProvider(crate::providers::models::ProviderKind),ShowTvConfig,diff --git a/src/tui/app/keyboard.rs b/src/tui/app/keyboard.rsindex be5d4bc..482fa52 100644--- a/src/tui/app/keyboard.rs+++ b/src/tui/app/keyboard.rs@@ -46,12 +46,14 @@ impl App {}if !self.state.has_active_modal() {if let KeyCode::Char('t') = key.code {- if self.state.tv_enabled {- self.action_sender.send(Action::ToggleTvMode).ok();- self.state.set_status_short("Switched to TV Mode.");- } else {+ if !self.state.tv_enabled {self.state.set_status_short("TV Mode is disabled. Use /settings to enable.");+ } else if self.state.is_tv_mode {+ self.state.set_status_short("Already in TV Mode.");+ } else {+ self.action_sender.send(Action::SwitchToTvMode).ok();+ self.state.set_status_short("Switched to TV Mode.");}return None;}@@ -1588,6 +1590,7 @@ mod tests {let mut app = App::new();app.state.active_screen = crate::tui::state::Screen::Home;app.state.input_mode = InputMode::Normal;+ app.state.is_tv_mode = false;app.state.active_provider = crate::models::ProviderKind::MovieBox;app.state.show_provider_popup = true;app.state.provider_list_state.select(Some(0));diff --git a/src/tui/app/mouse.rs b/src/tui/app/mouse.rsindex d3a2e91..c51e7c2 100644--- a/src/tui/app/mouse.rs+++ b/src/tui/app/mouse.rs@@ -668,7 +668,7 @@ impl App {self.state.provider_list_state.select(Some(current_idx));self.state.input_mode = InputMode::Normal;} else if self.state.mode() == crate::tui::state::AppMode::Tv {- self.action_sender.send(Action::ToggleTvMode).ok();+ self.action_sender.send(Action::SwitchToStreamingMode).ok();}return None;}@@ -870,12 +870,8 @@ impl App {self.state.is_tv_mode,);let pos = ratatui::layout::Position::new(col, row);- if btn1.contains(pos) {- if self.state.is_tv_mode {- self.action_sender.send(Action::TvReloadPlaylists).ok();- } else {- self.cycle_provider();- }+ if !self.state.is_tv_mode && btn1.contains(pos) {+ self.cycle_provider();return None;}if btn2.contains(pos) {@@ -985,7 +981,7 @@ impl App {}BottomBtn::Tv => {if self.state.mode() != crate::tui::state::AppMode::Tv {- self.action_sender.send(Action::ToggleTvMode).ok();+ self.action_sender.send(Action::SwitchToTvMode).ok();}}}@@ -1425,6 +1421,7 @@ mod tests {let mut app = App::new();app.state.active_screen = crate::tui::state::Screen::Home;app.state.input_mode = crate::tui::state::InputMode::Normal;+ app.state.is_tv_mode = false;app.state.active_provider = ProviderKind::FourKHdHub;app.state.search_query.set_content("deewaniyat");app.state.search_results = vec![];@@ -1454,6 +1451,7 @@ mod tests {let mut app = App::new();app.state.active_screen = crate::tui::state::Screen::Home;app.state.input_mode = crate::tui::state::InputMode::Normal;+ app.state.is_tv_mode = false;app.state.active_provider = ProviderKind::MovieBox;app.state.search_query.clear();diff --git a/src/tui/app/requests.rs b/src/tui/app/requests.rsindex 452a80b..2a764a5 100644--- a/src/tui/app/requests.rs+++ b/src/tui/app/requests.rs@@ -198,13 +198,16 @@ impl App {self.load_favorites_virtual_list();return None;}-if self.handle_search_command(&query, &lower_query).is_some() {return None;}if query.trim().starts_with('/') {return None;}+ if self.state.is_tv_mode {+ self.apply_tv_search_results(&query, &lower_query);+ return None;+ }let context = self.prepare_search_request(&query);self.run_search_request(query.clone(), force_refresh, context);}diff --git a/src/tui/app/run.rs b/src/tui/app/run.rsindex ea6aeaf..454ab32 100644--- a/src/tui/app/run.rs+++ b/src/tui/app/run.rs@@ -395,7 +395,7 @@ impl App {self.handle_system(action).await;}- Action::ToggleTvMode+ Action::SwitchToTvMode| Action::ShowTvConfig| Action::TvPlaylistAdd(..)| Action::TvPlaylistRemove(..)diff --git a/src/tui/app/tv.rs b/src/tui/app/tv.rsindex 5212505..adbbe32 100644--- a/src/tui/app/tv.rs+++ b/src/tui/app/tv.rs@@ -63,15 +63,10 @@ impl App {pub(super) async fn handle_tv(&mut self, action: Action) -> Option<()> {match action {- Action::ToggleTvMode => {- self.reset_mode_state();- let will_be_tv = !self.state.is_tv_mode;- if will_be_tv {+ Action::SwitchToTvMode => {+ if !self.state.is_tv_mode {+ self.reset_mode_state();self.state.set_mode(crate::tui::state::AppMode::Tv);- } else {- self.state.set_mode(crate::tui::state::AppMode::Streaming);- }- if self.state.is_tv_mode {self.state.tv_config_popup = false;self.state.tv_channels.clear();self.announce_mode();@@ -80,18 +75,8 @@ impl App {if self.state.tv_playlists.is_empty() {self.action_sender.send(Action::ShowTvConfig).ok();}- } else {- self.state.tv_config_popup = false;- self.state.search_query.clear();- self.state.search_results.clear();- if self.state.active_provider == crate::providers::models::ProviderKind::Addons- {- self.state.active_provider =- crate::providers::models::ProviderKind::MovieBox;- }- self.announce_mode();+ self.persist_config();}- self.persist_config();}Action::ShowTvConfig => {diff --git a/src/tui/screens/home.rs b/src/tui/screens/home.rsindex 42e7a8a..1bb0bb7 100644--- a/src/tui/screens/home.rs+++ b/src/tui/screens/home.rs@@ -126,12 +126,19 @@ pub fn landing_split()}-pub fn search_deck_width(area: Rect, _state: &AppState, landing: bool) -> u16 {+pub fn search_deck_width(area: Rect, state: &AppState, landing: bool) -> u16 {let tier = HomeLayoutTier::for_width(area.width);if landing {- let target_width = match tier {- HomeLayoutTier::Compact => 54,- HomeLayoutTier::Normal | HomeLayoutTier::Wide => 64,+ let target_width = if state.is_tv_mode {+ match tier {+ HomeLayoutTier::Compact => 44,+ HomeLayoutTier::Normal | HomeLayoutTier::Wide => 48,+ }+ } else {+ match tier {+ HomeLayoutTier::Compact => 54,+ HomeLayoutTier::Normal | HomeLayoutTier::Wide => 64,+ }};target_width.min(area.width.saturating_sub(4)).max(24)} else {@@ -225,6 +232,8 @@ fn render_search_state("No watch history found".to_string()} else if state.search_query.trim().eq_ignore_ascii_case("/favorites") {"No favorites saved yet".to_string()+ } else if state.is_tv_mode && !state.search_query.trim().is_empty() {+ format!("No TV channels found matching “{query}”")} else if !state.search_query.trim().is_empty() {format!("No results for “{query}” on {provider_label}")} else {@@ -243,17 +252,13 @@ fn render_search_state(let pills = if is_history_or_fav {vec![]} else if state.is_tv_mode {- let (btn1_label, btn2_label) = if is_compact_btn {- ("[ Reload (", "[ Clear (")+ let btn_label = if is_compact_btn {+ "[ Clear ("} else {- ("[ Reload Playlists (", "[ Clear Search (")+ "[ Clear Search ("};vec![- Span::styled(btn1_label, theme.subtext1),- Span::styled("r", theme.shortcut),- Span::styled(") ]", theme.subtext1),- Span::raw(sep),- Span::styled(btn2_label, theme.subtext1),+ Span::styled(btn_label, theme.subtext1),Span::styled("c", theme.shortcut),Span::styled(") ]", theme.subtext1),]@@ -381,39 +386,46 @@ pub(crate) fn no_results_button_hitboxes(is_tv_mode: bool,) -> (Rect, Rect) {let is_compact_btn = area.width < 56;- let (btn1_w, btn2_w, sep_w) = if is_compact_btn {- let b1 = if is_tv_mode {- 14- } else {- 18 + ctrl_p.len() as u16+ if is_tv_mode {+ let btn_w = if is_compact_btn { 13 } else { 20 };+ let start_x = area.x + area.width.saturating_sub(btn_w) / 2;+ let card_y = area.y + area.height.saturating_sub(3) / 3;+ let btn = Rect {+ x: start_x,+ y: card_y + 2,+ width: btn_w,+ height: 1,};- (b1, 13, 2)+ (Rect::default(), btn)} else {- let b1 = if is_tv_mode {- 24+ let (btn1_w, btn2_w, sep_w) = if is_compact_btn {+ (18 + ctrl_p.len() as u16, 13, 2)} else {- (14 + next_provider_label.len() + ctrl_p.len()) as u16+ (+ (14 + next_provider_label.len() + ctrl_p.len()) as u16,+ 20,+ 8,+ )};- (b1, 20, 8)- };- let total_w = btn1_w + sep_w + btn2_w;- let start_x = area.x + area.width.saturating_sub(total_w) / 2;- let card_y = area.y + area.height.saturating_sub(3) / 3;- let btn_y = card_y + 2;-- let btn1 = Rect {- x: start_x,- y: btn_y,- width: btn1_w,- height: 1,- };- let btn2 = Rect {- x: start_x + btn1_w + sep_w,- y: btn_y,- width: btn2_w,- height: 1,- };- (btn1, btn2)+ let total_w = btn1_w + sep_w + btn2_w;+ let start_x = area.x + area.width.saturating_sub(total_w) / 2;+ let card_y = area.y + area.height.saturating_sub(3) / 3;+ let btn_y = card_y + 2;++ let btn1 = Rect {+ x: start_x,+ y: btn_y,+ width: btn1_w,+ height: 1,+ };+ let btn2 = Rect {+ x: start_x + btn1_w + sep_w,+ y: btn_y,+ width: btn2_w,+ height: 1,+ };+ (btn1, btn2)+ }}pub(crate) fn render_landing_deck(frame: &mut Frame, area: Rect, state: &AppState, theme: &Theme) {@@ -797,12 +809,28 @@ pub(crate) fn render_discover_landing(let tag_len = crate::tui::text::width(&display_desc);let pad_len = (inner_area.width as usize).saturating_sub(margins_len + title_w + tag_len);+ let pointer_style = if modal_active {+ theme.muted+ } else {+ theme.accent+ };+ let title_style = if modal_active {+ theme.muted+ } else {+ theme.text.add_modifier(Modifier::BOLD)+ };+ let desc_style = if modal_active {+ theme.muted+ } else {+ theme.text_dim+ };+let line = Line::from(vec![Span::raw(" "),- Span::styled(pointer, theme.accent),- Span::styled(title, theme.text.add_modifier(Modifier::BOLD)),+ Span::styled(pointer, pointer_style),+ Span::styled(title, title_style),Span::raw(" ".repeat(pad_len)),- Span::styled(display_desc, theme.text_dim),+ Span::styled(display_desc, desc_style),Span::raw(" "),]);@@ -1811,12 +1839,14 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)));}- row3_spans.push(crate::tui::widgets::badge::provider_badge_span(- res.provider,- theme,- state.basic_terminal,- modal_active,- ));+ if !state.is_tv_mode {+ row3_spans.push(crate::tui::widgets::badge::provider_badge_span(+ res.provider,+ theme,+ state.basic_terminal,+ modal_active,+ ));+ }if let Some(meta) = matching_meta {if is_selected {@@ -3019,8 +3049,19 @@ mod tests {assert_eq!(search_deck_width(compact_area, &state, true), 54);assert_eq!(search_deck_width(normal_area, &state, true), 64);assert_eq!(search_deck_width(wide_area, &state, true), 64);- }+ let mut tv_state = AppState {+ is_tv_mode: true,+ ..Default::default()+ };+ assert_eq!(search_deck_width(compact_area, &tv_state, true), 44);+ assert_eq!(search_deck_width(normal_area, &tv_state, true), 48);+ assert_eq!(search_deck_width(wide_area, &tv_state, true), 48);+ tv_state.search_query = "Deepto TV".into();+ assert_eq!(search_deck_width(compact_area, &tv_state, true), 44);+ assert_eq!(search_deck_width(normal_area, &tv_state, true), 48);+ assert_eq!(search_deck_width(wide_area, &tv_state, true), 48);+ }#[test]fn test_search_content_ghost_placeholder() {let state_rich = AppState {diff --git a/tests/tui_acceptance.rs b/tests/tui_acceptance.rsindex c629b2c..b250e3e 100644--- a/tests/tui_acceptance.rs+++ b/tests/tui_acceptance.rs@@ -368,9 +368,13 @@ async fn test_full_user_journey_mode_switching_and_theme_selection() {let mut app = App::new();app.state_mut().is_tv_mode = false;- app.handle_action(Action::ToggleTvMode).await;+ app.handle_action(Action::SwitchToTvMode).await;+ assert!(app.state().is_tv_mode);+ app.handle_action(Action::SwitchToTvMode).await;assert!(app.state().is_tv_mode);+ app.handle_action(Action::SwitchToStreamingMode).await;+ assert!(!app.state().is_tv_mode);app.handle_action(Action::SwitchToStreamingMode).await;assert!(!app.state().is_tv_mode);app.handle_action(Action::SelectTheme("TokyoNight".to_string()))@@ -379,6 +383,141 @@ async fn test_full_user_journey_mode_switching_and_theme_selection() {app.handle_action(Action::SwitchToStreamingMode).await;}+#[tokio::test]+async fn test_ctrl_t_and_ctrl_s_mode_switch_keys() {+ let mut app = App::new();+ app.state_mut().tv_enabled = true;+ app.state_mut().streaming_enabled = true;+ app.state_mut().is_tv_mode = false;++ let ctrl_t = crossterm::event::KeyEvent::new(+ crossterm::event::KeyCode::Char('t'),+ crossterm::event::KeyModifiers::CONTROL,+ );+ let ctrl_s = crossterm::event::KeyEvent::new(+ crossterm::event::KeyCode::Char('s'),+ crossterm::event::KeyModifiers::CONTROL,+ );++ app.handle_action(Action::Key(ctrl_t)).await;+ app.handle_action(Action::SwitchToTvMode).await;+ assert!(app.state().is_tv_mode);++ app.handle_action(Action::Key(ctrl_t)).await;+ assert!(app.state().is_tv_mode);+ assert_eq!(app.state().status_message, "Already in TV Mode.");++ app.handle_action(Action::Key(ctrl_s)).await;+ app.handle_action(Action::SwitchToStreamingMode).await;+ assert!(!app.state().is_tv_mode);++ app.handle_action(Action::Key(ctrl_s)).await;+ assert!(!app.state().is_tv_mode);+ assert_eq!(app.state().status_message, "Already in Streaming Mode.");+}++#[tokio::test]+async fn test_tv_mode_search_isolated_to_channels() {+ let mut app = App::new();+ app.state_mut().tv_enabled = true;+ app.state_mut().is_tv_mode = true;+ app.state_mut().tv_channels = vec![+ moviebox_tui::providers::tv::Channel {+ id: "ch1".to_string(),+ name: "Deepto TV".to_string(),+ stream_url: "https://example.com/deepto.m3u8".to_string(),+ group: "Bangla".to_string(),+ logo: "https://example.com/deepto.png".to_string(),+ },+ moviebox_tui::providers::tv::Channel {+ id: "ch2".to_string(),+ name: "Somoy TV".to_string(),+ stream_url: "https://example.com/somoy.m3u8".to_string(),+ group: "Bangla".to_string(),+ logo: "https://example.com/somoy.png".to_string(),+ },+ ];++ app.handle_action(Action::Search {+ query: "deepto".to_string(),+ force_refresh: false,+ })+ .await;++ assert_eq!(app.state().search_results.len(), 1);+ assert_eq!(app.state().search_results[0].title, "Deepto TV");+ assert_eq!(+ app.state().search_results[0].id,+ "https://example.com/deepto.m3u8"+ );++ app.handle_action(Action::Search {+ query: "nonexistent".to_string(),+ force_refresh: false,+ })+ .await;++ assert!(app.state().search_results.is_empty());+ assert_eq!(app.state().status_message, "No matches for 'nonexistent'.");+}+#[tokio::test]+async fn test_tv_mode_no_provider_badge_and_single_clear_button() {+ let mut app = App::new();+ app.state_mut().tv_enabled = true;+ app.state_mut().is_tv_mode = true;+ app.state_mut().active_screen = Screen::Home;+ app.state_mut().tv_channels = vec![moviebox_tui::providers::tv::Channel {+ id: "ch1".to_string(),+ name: "Deepto TV".to_string(),+ stream_url: "https://example.com/deepto.m3u8".to_string(),+ group: "Bangla".to_string(),+ logo: "https://example.com/deepto.png".to_string(),+ }];++ app.state_mut().search_query.set_content("deepto");+ app.handle_action(Action::Search {+ query: "deepto".to_string(),+ force_refresh: false,+ })+ .await;++ let backend = ratatui::backend::TestBackend::new(100, 30);+ let mut terminal = ratatui::Terminal::new(backend).unwrap();+ terminal.draw(|frame| app.draw(frame)).unwrap();++ let rendered: String = terminal+ .backend()+ .buffer()+ .content()+ .iter()+ .map(|c| c.symbol())+ .collect();++ assert!(rendered.contains("Deepto TV"));+ assert!(!rendered.contains("[MovieBox]"));++ app.state_mut().search_query.set_content("nonexistent");+ app.handle_action(Action::Search {+ query: "nonexistent".to_string(),+ force_refresh: false,+ })+ .await;++ terminal.draw(|frame| app.draw(frame)).unwrap();+ let rendered_empty: String = terminal+ .backend()+ .buffer()+ .content()+ .iter()+ .map(|c| c.symbol())+ .collect();++ assert!(rendered_empty.contains("No TV channels found matching “nonexistent”"));+ assert!(rendered_empty.contains("Clear Search"));+ assert!(!rendered_empty.contains("Reload Playlists"));+ assert!(!rendered_empty.contains("MovieBox"));+}+#[tokio::test]async fn test_esc_key_cancels_slash_command_and_clears_search_bar() {let mut app = App::new();@@ -1000,6 +1139,40 @@ async fn test_home_deck_tab_switching() {);}+#[tokio::test]+async fn test_browse_suggestions_dimmed_when_modal_active() {+ let mut app = App::new();+ app.state_mut().active_screen = Screen::Home;+ app.state_mut().input_mode = InputMode::Normal;+ app.state_mut().history.recent.clear();+ app.state_mut().favorites.clear();+ app.state_mut().is_tv_mode = false;++ let backend = ratatui::backend::TestBackend::new(90, 30);+ let mut terminal = ratatui::Terminal::new(backend).unwrap();++ terminal.draw(|frame| app.draw(frame)).unwrap();+ let buffer_normal = terminal.backend().buffer().clone();++ app.state_mut().show_provider_popup = true;+ terminal.draw(|frame| app.draw(frame)).unwrap();+ let buffer_modal = terminal.backend().buffer().clone();++ let cell_normal = buffer_normal+ .content()+ .iter()+ .find(|c| c.symbol() == "T")+ .expect("Trending Now should be rendered in normal mode");++ let cell_modal = buffer_modal+ .content()+ .iter()+ .find(|c| c.symbol() == "T")+ .expect("Trending Now should be rendered when modal is active");++ assert_ne!(cell_normal.style().fg, cell_modal.style().fg);+}+#[tokio::test]async fn test_home_deck_continue_watching_resume() {let mut app = App::new();