Skip to content

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

refactor: improve terminal compatibility, refine search result selection, and optimize API request handling

Samir Hossainsade55d543806
diff --git a/Cargo.toml b/Cargo.toml
index a47a39c..163c12c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,3 +30,4 @@ url = "2.5.8"
[lib]
name = "moviebox_tui"
path = "src/lib.rs"
+
diff --git a/src/tui/app.rs b/src/tui/app.rs
index ae46303..4f7ccc1 100644
--- a/src/tui/app.rs
+++ b/src/tui/app.rs
@@ -539,17 +539,29 @@ impl App {
});
}
- self.state
- .search_list_state
- .select(if count > 0 { Some(0) } else { None });
self.state.status_message = format!("Found {} results.", count); self.state.status_timer = 150;
self.state
.add_log(format!("Search completed: {} items loaded.", count));
- if let Some(res) = self.state.search_results.first() {
- self.action_sender
- .send(Action::FetchPreview(res.id.clone()))
- .ok();
+ let query_lower = query.to_lowercase().trim().to_string();
+ let exact_match_idx = self.state.search_results.iter().position(|r| r.title.to_lowercase() == query_lower);
+
+ if let Some(idx) = exact_match_idx {
+ self.state.search_list_state.select(Some(idx));
+ self.action_sender.send(Action::Submit).ok();
+ } else if count == 1 {
+ self.state.search_list_state.select(Some(0));
+ self.action_sender.send(Action::Submit).ok();
+ } else {
+ self.state
+ .search_list_state
+ .select(if count > 0 { Some(0) } else { None });
+
+ if let Some(res) = self.state.search_results.first() {
+ self.action_sender
+ .send(Action::FetchPreview(res.id.clone()))
+ .ok();
+ }
}
}
Action::SearchFailure(err) => {
diff --git a/src/tui/screens/details.rs b/src/tui/screens/details.rs
index f5ceebe..ce0e85e 100644
--- a/src/tui/screens/details.rs
+++ b/src/tui/screens/details.rs
@@ -64,25 +64,31 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
let genres = details_json
.get("genre")
- .and_then(|g| g.as_array())
- .map(|a| {
- a.iter()
- .filter_map(|v| v.as_str())
- .collect::<Vec<_>>()
- .join(", ")
+ .and_then(|g| {
+ if let Some(a) = g.as_array() {
+ let joined = a.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>().join(", ");
+ if joined.is_empty() { None } else { Some(joined) }
+ } else if let Some(s) = g.as_str() {
+ if s.is_empty() { None } else { Some(s.to_string()) }
+ } else {
+ None
+ }
})
.unwrap_or_else(|| "N/A".to_string());
let duration = details_json
.get("duration")
.and_then(|d| d.as_str())
+ .filter(|s| !s.is_empty())
.unwrap_or("N/A");
let country = details_json
.get("countryName")
.and_then(|c| c.as_str())
+ .filter(|s| !s.is_empty())
.unwrap_or("N/A");
let content_rating = details_json
.get("contentRating")
.and_then(|c| c.as_str())
+ .filter(|s| !s.is_empty())
.unwrap_or("N/A");
let imdb_rating = details_json
.get("imdbRatingValue")
@@ -91,6 +97,7 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
.map(|rf| rf.to_string())
.or_else(|| r.as_str().map(|s| s.to_string()))
})
+ .filter(|s| !s.is_empty())
.unwrap_or_else(|| "N/A".to_string());
let details_block = Block::default()
@@ -143,8 +150,13 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
frame.render_widget(ratatui_image::Image::new(proto), poster_area);
}
} else {
- let spinner_frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
- let current_spinner = spinner_frames[(state.tick_count as usize) % spinner_frames.len()];
+ let current_spinner = if state.basic_terminal {
+ let frames = ['-', '\\', '|', '/'];
+ frames[(state.tick_count as usize) % frames.len()]
+ } else {
+ let frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
+ frames[(state.tick_count as usize) % frames.len()]
+ };
let placeholder = Paragraph::new(format!("\n\n\n\n\n {} Loading Art...", current_spinner))
.style(theme.text_dim)
@@ -239,82 +251,26 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
false
};
- let (is_series, bottom_chunks) = if type_val == 2 && !state.available_seasons.is_empty() {
- if has_languages {
- if !state.language_chosen {
- (
- true,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Min(0),
- Constraint::Length(20),
- Constraint::Min(0),
- ])
- .split(bottom_area),
- )
- } else {
- (
- true,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Length(18),
- Constraint::Length(15),
- Constraint::Length(15),
- Constraint::Min(1),
- ])
- .split(bottom_area),
- )
- }
- } else {
- (
- true,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Length(15),
- Constraint::Length(15),
- Constraint::Min(1),
- ])
- .split(bottom_area),
- )
- }
+ let is_series = type_val == 2 && !state.available_seasons.is_empty();
+ let bottom_chunks = if has_languages && !state.language_chosen {
+ Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints([Constraint::Min(0), Constraint::Length(20), Constraint::Min(0)])
+ .split(bottom_area)
} else {
+ let mut c = Vec::new();
if has_languages {
- if !state.language_chosen {
- (
- false,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Min(0),
- Constraint::Length(20),
- Constraint::Min(0),
- ])
- .split(bottom_area),
- )
- } else {
- (
- false,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([
- Constraint::Length(18),
- Constraint::Min(1),
- ])
- .split(bottom_area),
- )
- }
- } else {
- (
- false,
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([Constraint::Percentage(100)])
- .split(bottom_area),
- )
+ c.push(Constraint::Length(18));
+ }
+ if is_series {
+ c.push(Constraint::Length(15));
+ c.push(Constraint::Length(15));
}
+ c.push(Constraint::Min(1));
+ Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints(c)
+ .split(bottom_area)
};
if has_languages {
diff --git a/src/tui/screens/home.rs b/src/tui/screens/home.rs
index 487b627..aad7ab5 100644
--- a/src/tui/screens/home.rs
+++ b/src/tui/screens/home.rs
@@ -61,7 +61,7 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
}
};
-
+ let mut search_bar_area = Rect::default();
if state.search_results.is_empty()
&& !state.is_loading
@@ -75,15 +75,14 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
let is_wide = area.width >= 100;
let logo_height = if is_narrow { 2 } else if is_wide { 6 } else { 4 };
let logo_text = if is_narrow {
- r"█▀▄▀█ █▀█ █ █ █ █▀▀ █▀▄ █▀█ ▀▄▀
-█ ▀ █ █▄█ ▀▄▀ █ ██▄ █▄▀ █▄█ █ █"
+ "█▀▄▀█ █▀█ █ █ █ █▀▀ █▀▄ █▀█ ▀▄▀\n█ ▀ █ █▄█ ▀▄▀ █ ██▄ █▄▀ █▄█ █ █"
} else if is_wide {
- r" __ __ ____ __ __ ___ _____ ____ ____ __ __
-| \/ | / __ \ \ \ / / |_ _| | ____| | __ ) / __ \ \ \/ /
-| \ / || | | | \ \ / / | | | _| | _ \ | | | | \ /
-| |\/| || | | | \ V / | | | |___ | |_) | | | | | / \
-| | | || |__| | \ / | | | ___| | _ < | |__| | / /\ \
-|_| |_| \____/ V |___| |_____| |_| \_\ \____/ /_/ \_\ "
+ r"███╗ ███╗ ██████╗ ██╗ ██╗ ██╗ ███████╗ ██████╗ ██████╗ ██╗ ██╗
+████╗ ████║ ██╔═══██╗ ██║ ██║ ██║ ██╔════╝ ██╔══██╗ ██╔═══██╗ ╚██╗██╔╝
+██╔████╔██║ ██║ ██║ ██║ ██║ ██║ █████╗ ██████╔╝ ██║ ██║ ╚███╔╝
+██║╚██╔╝██║ ██║ ██║ ╚██╗ ██╔╝ ██║ ██╔══╝ ██╔══██╗ ██║ ██║ ██╔██╗
+██║ ╚═╝ ██║ ╚██████╔╝ ╚████╔╝ ██║ ███████╗ ██████╔╝ ╚██████╔╝ ██╔╝ ██╗
+╚═╝ ╚═╝ ╚═════╝ ╚═══╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝"
} else {
r" __ __ ___ __ __ ___ ___ ___ ___ __ __
| \/ |/ _ \ \ \ / /|_ _|| __|| _ ) / _ \ \ \/ /
@@ -160,6 +159,8 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
Constraint::Percentage(25),
])
.split(vertical_chunks[4]);
+
+ search_bar_area = search_chunks[1];
let search_bar = Paragraph::new(search_content.clone())
.alignment(Alignment::Center)
@@ -168,7 +169,7 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
InputMode::Normal => theme.text,
});
- frame.render_widget(search_bar, search_chunks[1]);
+ frame.render_widget(search_bar, search_bar_area);
let legend = Paragraph::new("/ Search ↑↓ Browse ? Help")
.alignment(Alignment::Center)
@@ -190,12 +191,14 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
])
.split(area);
+ search_bar_area = chunks[0];
+
let search_bar = Paragraph::new(search_content.clone())
.style(match state.input_mode {
InputMode::Editing => theme.title,
InputMode::Normal => theme.text,
});
- frame.render_widget(search_bar, chunks[0]);
+ frame.render_widget(search_bar, search_bar_area);
let list_block = Block::default()
.borders(Borders::ALL)
@@ -205,8 +208,13 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
.border_style(theme.border);
if state.is_loading {
- let spinner_frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
- let spinner = spinner_frames[(state.tick_count as usize) % spinner_frames.len()];
+ let spinner = if state.basic_terminal {
+ let frames = ['-', '\\', '|', '/'];
+ frames[(state.tick_count as usize) % frames.len()]
+ } else {
+ let frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
+ frames[(state.tick_count as usize) % frames.len()]
+ };
let inner_area = list_block.inner(chunks[1]);
frame.render_widget(list_block, chunks[1]);
@@ -277,31 +285,8 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)
}
}
- if state.input_mode == InputMode::Editing && !state.search_suggestions.is_empty() {
- let search_area = if state.search_results.is_empty() && !state.is_loading && !state.status_message.to_lowercase().contains("fail") {
- let is_narrow = area.width < 60;
- let is_wide = area.width >= 100;
- let logo_height = if is_narrow { 2 } else if is_wide { 6 } else { 4 };
- let vertical_chunks = Layout::default()
- .direction(Direction::Vertical)
- .constraints([
- Constraint::Percentage(15),
- Constraint::Length(logo_height),
- Constraint::Length(1),
- Constraint::Percentage(15),
- Constraint::Length(1),
- Constraint::Min(0),
- Constraint::Length(1),
- Constraint::Length(1),
- ])
- .split(area);
- Layout::default()
- .direction(Direction::Horizontal)
- .constraints([Constraint::Percentage(25), Constraint::Percentage(50), Constraint::Percentage(25)])
- .split(vertical_chunks[4])[1]
- } else {
- Layout::default().direction(Direction::Vertical).constraints([Constraint::Length(3), Constraint::Min(4)]).split(area)[0]
- };
+ if state.input_mode == InputMode::Editing && !state.search_suggestions.is_empty() && search_bar_area.width > 0 {
+ let search_area = search_bar_area;
let dropdown_height = std::cmp::min(state.search_suggestions.len() as u16 + 2, 10);
diff --git a/src/tui/state.rs b/src/tui/state.rs
index d12fe62..9844b5f 100644
--- a/src/tui/state.rs
+++ b/src/tui/state.rs
@@ -84,6 +84,7 @@ pub struct AppState {
pub subtitle_list: Vec<(String, String)>,
pub subtitle_list_state: ListState,
pub pending_play_link: Option<String>,
+ pub basic_terminal: bool,
}
impl Default for AppState {
@@ -98,6 +99,7 @@ impl Default for AppState {
suggest_index: None,
search_results: Vec::new(),
search_list_state: TableState::default(),
+ basic_terminal: std::env::var("TERM_PROGRAM").unwrap_or_default() == "Apple_Terminal",
selected_details: None,
selected_resources: None,
resource_list_state: TableState::default(),
diff --git a/src/v3/crypto.rs b/src/v3/crypto.rs
index 1ac08c9..c711324 100644
--- a/src/v3/crypto.rs
+++ b/src/v3/crypto.rs
@@ -159,9 +159,6 @@ pub fn build_signed_headers(
headers.insert("X-Client-Status", "0".parse().expect("Valid ASCII"));
headers.insert("X-Forwarded-For", spoofed_ip.parse().expect("Valid ASCII"));
- headers.insert("X-Real-IP", spoofed_ip.parse().expect("Valid ASCII"));
- headers.insert("Client-IP", spoofed_ip.parse().expect("Valid ASCII"));
- headers.insert("CF-Connecting-IP", spoofed_ip.parse().expect("Valid ASCII"));
if let Some(token) = auth_token {
headers.insert(
diff --git a/src/v3/mod.rs b/src/v3/mod.rs
index 814efe8..24b0479 100644
--- a/src/v3/mod.rs
+++ b/src/v3/mod.rs
@@ -123,16 +123,28 @@ impl MovieBoxClient {
}
let mut all_list = Vec::new();
+ let mut last_err = None;
for h in handles {
- if let Ok(Ok(res)) = h.await
- && let Some(list) = res.get("list").and_then(|l| l.as_array())
- {
- all_list.extend(list.clone());
+ if let Ok(res_result) = h.await {
+ match res_result {
+ Ok(res) => {
+ if let Some(list) = res.get("list").and_then(|l| l.as_array()) {
+ all_list.extend(list.clone());
+ }
+ }
+ Err(e) => {
+ last_err = Some(e);
+ }
+ }
}
}
if all_list.is_empty() {
- Err(ScraperError::ApiStatus(404))
+ if let Some(e) = last_err {
+ Err(e)
+ } else {
+ Err(ScraperError::ApiStatus(404))
+ }
} else {
let mut combined = serde_json::Map::new();
combined.insert("list".to_string(), serde_json::Value::Array(all_list));