Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
refactor: centralize duration parsing, history file path, and theme constants
Samir Hossainsc80afc8842c6
diff --git a/src/config.rs b/src/config.rsindex 23278a5..8b18109 100644--- a/src/config.rs+++ b/src/config.rs@@ -80,6 +80,10 @@ pub fn tv_path() -> Option<PathBuf> {config_dir().map(|dir| dir.join("tv_config.json"))}+pub fn history_path() -> Option<PathBuf> {+ data_dir().map(|dir| dir.join("history.json"))+}+pub fn load() -> Config {config_path().and_then(|p| std::fs::read_to_string(p).ok())diff --git a/src/history.rs b/src/history.rsindex aec49c4..0c66806 100644--- a/src/history.rs+++ b/src/history.rs@@ -163,7 +163,7 @@ impl HistoryManager {}fn history_file_path() -> Option<PathBuf> {- crate::config::data_dir().map(|dir| dir.join("history.json"))+ crate::config::history_path()}pub fn playback_state_dir() -> Option<PathBuf> {diff --git a/src/tui/app/playback.rs b/src/tui/app/playback.rsindex d6b844c..4da2e0c 100644--- a/src/tui/app/playback.rs+++ b/src/tui/app/playback.rs@@ -1,66 +1,10 @@use super::App;use crate::providers::models::ProviderKind;+use crate::tui::text::parse_duration_seconds;use crate::tui::{action::Action, overlay::NotificationKind, state::Screen};pub(crate) use crate::service::extract_cover_url;-fn parse_duration_seconds(d: &str) -> Option<u64> {- let s = d.trim();- if s.is_empty() || s.eq_ignore_ascii_case("n/a") {- return None;- }- if s.contains(':') {- let parts: Vec<&str> = s.split(':').collect();- if parts.len() == 2 {- let m: u64 = parts[0].trim().parse().ok()?;- let s: u64 = parts[1].trim().parse().ok()?;- return Some(m * 60 + s);- } else if parts.len() == 3 {- let h: u64 = parts[0].trim().parse().ok()?;- let m: u64 = parts[1].trim().parse().ok()?;- let s: u64 = parts[2].trim().parse().ok()?;- return Some(h * 3600 + m * 60 + s);- }- }- let mut total = 0u64;- let mut current_num = String::new();- let mut found_any = false;- for c in s.chars() {- if c.is_ascii_digit() {- current_num.push(c);- } else if c == 'h' || c == 'H' {- if let Ok(n) = current_num.parse::<u64>() {- total += n * 3600;- found_any = true;- }- current_num.clear();- } else if c == 'm' || c == 'M' {- if let Ok(n) = current_num.parse::<u64>() {- total += n * 60;- found_any = true;- }- current_num.clear();- } else if c == 's' || c == 'S' {- if let Ok(n) = current_num.parse::<u64>() {- total += n;- found_any = true;- }- current_num.clear();- }- }- if !current_num.is_empty() && !found_any {- if let Ok(n) = current_num.parse::<u64>() {- total += n * 60;- found_any = true;- }- }- if found_any && total > 0 {- Some(total)- } else {- None- }-}-impl App {fn preferred_playback_player(&self,diff --git a/src/tui/text.rs b/src/tui/text.rsindex ffccd57..bebac5d 100644--- a/src/tui/text.rs+++ b/src/tui/text.rs@@ -242,3 +242,76 @@ pub fn format_file_size(bytes: f64) -> String {format!("{:.0}MB", mb)}}++pub fn parse_duration_seconds(d: &str) -> Option<u64> {+ let s = d.trim();+ if s.is_empty() || s.eq_ignore_ascii_case("n/a") {+ return None;+ }+ if s.contains(':') {+ let parts: Vec<&str> = s.split(':').collect();+ if parts.len() == 2 {+ let m: u64 = parts[0].trim().parse().ok()?;+ let s: u64 = parts[1].trim().parse().ok()?;+ return Some(m * 60 + s);+ } else if parts.len() == 3 {+ let h: u64 = parts[0].trim().parse().ok()?;+ let m: u64 = parts[1].trim().parse().ok()?;+ let s: u64 = parts[2].trim().parse().ok()?;+ return Some(h * 3600 + m * 60 + s);+ }+ }+ let mut total = 0u64;+ let mut current_num = String::new();+ let mut found_any = false;+ for c in s.chars() {+ if c.is_ascii_digit() {+ current_num.push(c);+ } else if c == 'h' || c == 'H' {+ if let Ok(n) = current_num.parse::<u64>() {+ total += n * 3600;+ found_any = true;+ }+ current_num.clear();+ } else if c == 'm' || c == 'M' {+ if let Ok(n) = current_num.parse::<u64>() {+ total += n * 60;+ found_any = true;+ }+ current_num.clear();+ } else if c == 's' || c == 'S' {+ if let Ok(n) = current_num.parse::<u64>() {+ total += n;+ found_any = true;+ }+ current_num.clear();+ }+ }+ if !current_num.is_empty() && !found_any {+ if let Ok(n) = current_num.parse::<u64>() {+ total += n * 60;+ found_any = true;+ }+ }+ if found_any && total > 0 {+ Some(total)+ } else {+ None+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_format_and_parse_duration() {+ assert_eq!(format_duration(3665), "1:01:05");+ assert_eq!(format_duration(125), "2:05");+ assert_eq!(parse_duration_seconds("1:01:05"), Some(3665));+ assert_eq!(parse_duration_seconds("2h 15m"), Some(8100));+ assert_eq!(parse_duration_seconds("45m"), Some(2700));+ assert_eq!(parse_duration_seconds("N/A"), None);+ assert_eq!(parse_duration_seconds(""), None);+ }+}diff --git a/src/tui/theme.rs b/src/tui/theme.rsindex 775237c..762b126 100644--- a/src/tui/theme.rs+++ b/src/tui/theme.rs@@ -26,6 +26,18 @@ pub enum ThemeKind {}impl ThemeKind {+ pub const ALL: [Self; 9] = [+ Self::Mocha,+ Self::Latte,+ Self::Macchiato,+ Self::Frappe,+ Self::Nord,+ Self::TokyoNight,+ Self::Dracula,+ Self::Gruvbox,+ Self::RosePine,+ ];+pub fn parse(s: &str) -> Self {match s.to_ascii_lowercase().replace(['-', '_'], "").as_str() {"latte" | "light" => ThemeKind::Latte,