Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

fix(search): gate pagination on small lists and prevent layout shifts

Samir Hossains46db21377017
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2bdf15..0220a55 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,10 @@
## [Unreleased]
### Fixed
+- **Search Navigation & Pagination Engine**:
+ - Gated background search pagination in `src/tui/app/navigation.rs` so arrow navigation on result sets smaller than the visible viewport never triggers background page fetches.
+ - Added `search_exhausted` tracking in `src/tui/state.rs` and `src/tui/app/requests.rs`, stopping pagination crawls when provider search returns fewer than 15 items or zero query-matching results.
+ - Preserved stable card slot positions during search pagination in `src/tui/app/requests.rs` by appending subsequent page items to the list rather than re-sorting the entire array out from under the cursor.
- **Media Playback & Buffering Engine**:
- Reduced `mpv` and `IINA` cache-pause wait penalty from 8 seconds to 2 seconds (`--cache-pause-wait=2`) and disabled startup cache pause (`--cache-pause-initial=no`) in `src/player.rs`, preventing multi-second playback stalls on minor network dips.
- Replaced aggressive VLC buffering flags (`--network-caching=10000`, `--adaptive-logic=nearoptimal`) with `--network-caching=3000` and `--adaptive-logic=predictive` in `src/player.rs` to stop demuxer stream restarts and 10-second stall loops.
diff --git a/src/tui/app/navigation.rs b/src/tui/app/navigation.rs
index 1784365..cb87777 100644
--- a/src/tui/app/navigation.rs
+++ b/src/tui/app/navigation.rs
@@ -120,6 +120,7 @@ impl App {
|| self.state.search_results.is_empty()
|| self.state.active_browse_preset.is_some()
|| self.state.search_query.trim().starts_with('/')
+ || (!self.state.is_homepage_mode && self.state.search_exhausted)
{
return;
}
@@ -128,7 +129,13 @@ impl App {
let offset = self.state.result_scroll;
let visible = self.state.effective_visible_items().max(6);
- if selected + 8 >= total || offset + visible + 4 >= total {
+ let should_fetch = if self.state.is_homepage_mode {
+ selected + 8 >= total || offset + visible + 4 >= total
+ } else {
+ total >= visible && (selected + 4 >= total || offset + visible >= total)
+ };
+
+ if should_fetch {
let next_page = self.state.current_page + 1;
if self.state.is_homepage_mode {
self.action_sender
@@ -1021,4 +1028,63 @@ mod tests {
app.handle_action(Action::GoBack).await;
assert!(app.state.notifications.is_empty());
}
+
+ #[tokio::test]
+ async fn test_search_pagination_does_not_trigger_on_tiny_result_set() {
+ let mut app = App::new();
+ app.state.search_query.set_content("deewaniyat");
+ app.state.is_homepage_mode = false;
+ app.state.search_results.push(crate::models::SearchResult {
+ id: "1".to_string(),
+ title: "Deewaniyat".to_string(),
+ stype: 2,
+ release_year: "2024".to_string(),
+ cover_url: None,
+ season: 0,
+ episode: 1,
+ provider: ProviderKind::MovieBox,
+ });
+ app.state.search_results.push(crate::models::SearchResult {
+ id: "2".to_string(),
+ title: "Ek Deewane Ki Deewaniyat".to_string(),
+ stype: 1,
+ release_year: "2025".to_string(),
+ cover_url: None,
+ season: 0,
+ episode: 1,
+ provider: ProviderKind::MovieBox,
+ });
+ app.state.search_list_state.select(Some(0));
+
+ app.trigger_next_page_if_needed();
+
+ assert!(app.request_tasks.search.is_none());
+ assert!(!app.state.is_loading);
+ }
+
+ #[tokio::test]
+ async fn test_search_pagination_respects_search_exhausted() {
+ let mut app = App::new();
+ app.state.search_query.set_content("test");
+ app.state.is_homepage_mode = false;
+ app.state.search_exhausted = true;
+ for i in 0..20 {
+ app.state.search_results.push(crate::models::SearchResult {
+ id: i.to_string(),
+ title: format!("Test {i}"),
+ stype: 1,
+ release_year: "2025".to_string(),
+ cover_url: None,
+ season: 0,
+ episode: 1,
+ provider: ProviderKind::MovieBox,
+ });
+ }
+ app.state.search_list_state.select(Some(19));
+
+ app.trigger_next_page_if_needed();
+
+ assert!(app.request_tasks.search.is_none());
+ assert!(!app.state.is_loading);
+ }
}
diff --git a/src/tui/app/requests.rs b/src/tui/app/requests.rs
index f4b3e36..a783b6a 100644
--- a/src/tui/app/requests.rs
+++ b/src/tui/app/requests.rs
@@ -302,9 +302,8 @@ impl App {
self.state.search_error = None;
self.state.is_loading = false;
self.state.has_search_settled = true;
- if page <= 1 {
- self.state.search_results.clear();
- }
+ let raw_count = items.len();
+ let mut new_results = Vec::new();
for item in items {
let id = item.id.value.clone();
@@ -348,23 +347,28 @@ impl App {
|| raw_lower.contains("[english]");
if is_dub
- && self
+ && (self
.state
.search_results
.iter()
.any(|r| r.title == clean_title && r.stype == stype)
+ || new_results
+ .iter()
+ .any(|r: &SearchResult| r.title == clean_title && r.stype == stype))
{
continue;
}
if self.state.search_results.iter().any(|r| {
r.title == clean_title && r.release_year == release_year && r.stype == stype
+ }) || new_results.iter().any(|r: &SearchResult| {
+ r.title == clean_title && r.release_year == release_year && r.stype == stype
}) {
continue;
}
if !id.is_empty() {
- self.state.search_results.push(SearchResult {
+ new_results.push(SearchResult {
id,
title: clean_title.to_string(),
stype,
@@ -377,6 +381,30 @@ impl App {
}
}
+ if new_results.is_empty() || raw_count < 15 {
+ self.state.search_exhausted = true;
+ }
+
+ let sort_results = |results: &mut Vec<SearchResult>, q: &str| {
+ let query_lower = q.to_lowercase();
+ results.sort_by(|a, b| {
+ let a_title = a.title.to_lowercase();
+ let b_title = b.title.to_lowercase();
+
+ let a_exact = a_title == query_lower;
+ let b_exact = b_title == query_lower;
+
+ let a_starts = a_title.starts_with(&query_lower);
+ let b_starts = b_title.starts_with(&query_lower);
+
+ b_exact
+ .cmp(&a_exact)
+ .then_with(|| b_starts.cmp(&a_starts))
+ .then_with(|| b.stype.cmp(&a.stype))
+ .then_with(|| b.release_year.cmp(&a.release_year))
+ });
+ };
+
let previous_selected_id = if page > 1 {
self.state
.search_list_state
@@ -386,24 +414,13 @@ impl App {
None
};
- let query_lower = query.to_lowercase();
- self.state.search_results.sort_by(|a, b| {
- let a_title = a.title.to_lowercase();
- let b_title = b.title.to_lowercase();
-
- let a_exact = a_title == query_lower;
- let b_exact = b_title == query_lower;
-
- let a_starts = a_title.starts_with(&query_lower);
- let b_starts = b_title.starts_with(&query_lower);
-
- b_exact
- .cmp(&a_exact)
- .then_with(|| b_starts.cmp(&a_starts))
- .then_with(|| b.stype.cmp(&a.stype))
- .then_with(|| b.release_year.cmp(&a.release_year))
- });
-
+ if page <= 1 {
+ self.state.search_results = new_results;
+ sort_results(&mut self.state.search_results, &query);
+ } else {
+ sort_results(&mut new_results, &query);
+ self.state.search_results.extend(new_results);
+ }
if let Some(prev_id) = previous_selected_id {
if let Some(new_idx) = self
.state
@@ -2083,4 +2100,67 @@ mod tests {
assert_eq!(details.stars.as_deref(), Some("Actor One, Actor Two"));
assert_eq!(details.dubs.len(), 2);
}
+
+ #[tokio::test]
+ async fn test_search_success_preserves_page_1_order_and_sets_exhausted() {
+ let mut app = App::new();
+ app.state.search_query.set_content("deewaniyat");
+ let context = app.request_context();
+ let request_id = app.state.active_search_request;
+ let page1_items = vec![
+ crate::providers::models::CatalogItem {
+ id: crate::providers::models::ProviderMediaId {
+ value: "1".into(),
+ provider: ProviderKind::MovieBox,
+ },
+ title: "Deewaniyat".into(),
+ media_type: crate::models::MediaType::Series,
+ year: Some("2024".into()),
+ poster_url: None,
+ season_count: None,
+ },
+ crate::providers::models::CatalogItem {
+ id: crate::providers::models::ProviderMediaId {
+ value: "2".into(),
+ provider: ProviderKind::MovieBox,
+ },
+ title: "Ek Deewane Ki Deewaniyat".into(),
+ media_type: crate::models::MediaType::Movie,
+ year: Some("2025".into()),
+ poster_url: None,
+ season_count: None,
+ },
+ ];
+
+ app.handle_requests(Action::SearchSuccess {
+ context,
+ request_id,
+ query: "deewaniyat".into(),
+ page: 1,
+ items: page1_items,
+ })
+ .await;
+
+ assert_eq!(app.state.search_results.len(), 2);
+ assert_eq!(app.state.search_results[0].title, "Deewaniyat");
+ assert_eq!(
+ app.state.search_results[1].title,
+ "Ek Deewane Ki Deewaniyat"
+ );
+ // Page 1 only had 2 items (< 15), so it should already be marked exhausted
+ assert!(app.state.search_exhausted);
+
+ // If a page 2 arrives with 0 matching items, search_exhausted remains true
+ app.handle_requests(Action::SearchSuccess {
+ context,
+ request_id,
+ query: "deewaniyat".into(),
+ page: 2,
+ items: vec![],
+ })
+ .await;
+
+ assert_eq!(app.state.search_results.len(), 2);
+ assert!(app.state.search_exhausted);
+ }
}
diff --git a/src/tui/app/search.rs b/src/tui/app/search.rs
index 2a2376f..fb00616 100644
--- a/src/tui/app/search.rs
+++ b/src/tui/app/search.rs
@@ -207,6 +207,7 @@ impl App {
self.state.active_addon_catalog = None;
self.state.browse_metrics.clear();
self.state.current_page = 1;
+ self.state.search_exhausted = false;
self.state.active_screen = Screen::Home;
self.state.active_subject_id = None;
self.state.selected_details = None;
diff --git a/src/tui/state.rs b/src/tui/state.rs
index a3a89ce..68116b8 100644
--- a/src/tui/state.rs
+++ b/src/tui/state.rs
@@ -167,6 +167,7 @@ pub struct AppState {
pub homepage_cache: lru::LruCache<(String, usize), HomepageCacheData>,
pub search_results: Vec<SearchResult>,
pub search_error: Option<String>,
+ pub search_exhausted: bool,
pub is_homepage_mode: bool,
pub current_tab_id: String,
pub current_page: usize,
@@ -344,6 +345,7 @@ impl Default for AppState {
homepage_cache: lru::LruCache::new(cache_capacity(32)),
search_results: Vec::new(),
search_error: None,
+ search_exhausted: false,
is_homepage_mode: false,
current_tab_id: String::new(),
current_page: 1,
@@ -831,6 +833,7 @@ impl AppState {
self.favorites_focus = false;
self.favorites_landing_state.select(None);
self.has_search_settled = false;
+ self.search_exhausted = false;
}
pub fn clear_details_state(&mut self) {