Skip to content

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

refactor(tui): extract config load/save into a config module

Samir Hossainsb292fbc2991e
diff --git a/docs/architecture.md b/docs/architecture.md
index 661d86f..56f45ea 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -39,6 +39,7 @@ src/
system.rs help, refresh, cache, theme, updates, focus, resize
state.rs AppState: all UI state + in-memory LRU caches
action.rs the Action enum (event/message model)
+ config.rs Config load/save (config.json)
event.rs EventHandler: input events + tick → Action channel
player.rs player detection and command construction
overlay.rs popups, pickers, notifications
diff --git a/docs/modules.md b/docs/modules.md
index affa00a..0dd5c98 100644
--- a/docs/modules.md
+++ b/docs/modules.md
@@ -45,6 +45,8 @@ src/
tui/
action.rs The Action enum: every UI event/message (input, network
results, downloads, playback, tv, system).
+ config.rs Config struct: load/save config.json (provider, theme,
+ auto-update, default player, bdix flag).
state.rs AppState: all UI state, LRU image/preview caches, and the
PlayerKind enum + label()/parse(), tv manager row model.
event.rs EventHandler: crossterm event stream + tick interval,
diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rs
index d20a2a7..853bf28 100644
--- a/src/tui/app/mod.rs
+++ b/src/tui/app/mod.rs
@@ -1,9 +1,7 @@
use tokio::sync::mpsc;
use crate::providers::{
- fourkhdhub::FourKHdHubClient,
- models::{ProviderKind, RequestContext},
- moviebox::client::MovieBoxClient,
+ fourkhdhub::FourKHdHubClient, models::RequestContext, moviebox::client::MovieBoxClient,
};
use crate::tui::{action::Action, state::AppState, theme::Theme};
@@ -39,42 +37,13 @@ impl App {
let (action_sender, action_receiver) = mpsc::unbounded_channel();
let mut state = AppState::default();
- if let Some(config_dir) = dirs::config_dir() {
- let config_path = config_dir.join("moviebox-tui").join("config.json");
- if let Ok(config_str) = std::fs::read_to_string(config_path) {
- if let Ok(config_json) = serde_json::from_str::<serde_json::Value>(&config_str) {
- if let Some(auto_update) =
- config_json.get("auto_update").and_then(|v| v.as_bool())
- {
- state.auto_update = auto_update;
- }
- if let Some(last_check) = config_json
- .get("last_update_check")
- .and_then(|v| v.as_u64())
- {
- state.last_update_check = last_check;
- }
- if config_json.get("active_provider").and_then(|v| v.as_str())
- == Some(ProviderKind::FourKHdHub.cache_key())
- {
- state.active_provider = ProviderKind::FourKHdHub;
- }
- if let Some(theme_val) =
- config_json.get("active_theme").and_then(|v| v.as_str())
- {
- state.active_theme_kind = theme_val.to_string();
- }
- if let Some(bdix) = config_json.get("bdix_enabled").and_then(|v| v.as_bool()) {
- state.bdix_enabled = bdix;
- }
- if let Some(default_player) =
- config_json.get("default_player").and_then(|v| v.as_str())
- {
- state.default_player = Some(default_player.to_string());
- }
- }
- }
- }
+ let config = crate::tui::config::load();
+ state.auto_update = config.auto_update;
+ state.last_update_check = config.last_update_check;
+ state.active_provider = config.active_provider;
+ state.active_theme_kind = config.active_theme;
+ state.bdix_enabled = config.bdix_enabled;
+ state.default_player = config.default_player;
let mut theme = crate::tui::theme::Theme::new();
if !state.active_theme_kind.is_empty() {
@@ -110,28 +79,15 @@ impl App {
}
fn persist_config(&self) {
- if let Some(config_dir) = dirs::config_dir() {
- let app_dir = config_dir.join("moviebox-tui");
- if std::fs::create_dir_all(&app_dir).is_err() {
- return;
- }
- let config = serde_json::json!({
- "auto_update": self.state.auto_update,
- "last_update_check": self.state.last_update_check,
- "active_provider": self.state.active_provider.cache_key(),
- "active_theme": self.state.active_theme_kind,
- "bdix_enabled": self.state.bdix_enabled,
- "default_player": self.state.default_player
- });
- let path = app_dir.join("config.json");
- let temporary = app_dir.join(format!("config.{}.tmp", std::process::id()));
- if std::fs::write(&temporary, config.to_string()).is_ok()
- && std::fs::rename(&temporary, &path).is_err()
- {
- let _ = std::fs::remove_file(&path);
- let _ = std::fs::rename(&temporary, &path);
- }
- }
+ let config = crate::tui::config::Config {
+ auto_update: self.state.auto_update,
+ last_update_check: self.state.last_update_check,
+ active_provider: self.state.active_provider,
+ active_theme: self.state.active_theme_kind.clone(),
+ bdix_enabled: self.state.bdix_enabled,
+ default_player: self.state.default_player.clone(),
+ };
+ crate::tui::config::save(&config);
}
fn save_tv_playlists(&self) {
diff --git a/src/tui/config.rs b/src/tui/config.rs
new file mode 100644
index 0000000..0d51121
--- /dev/null
+++ b/src/tui/config.rs
@@ -0,0 +1,88 @@
+use crate::providers::models::ProviderKind;
+use std::path::PathBuf;
+
+pub struct Config {
+ pub auto_update: bool,
+ pub last_update_check: u64,
+ pub active_provider: ProviderKind,
+ pub active_theme: String,
+ pub bdix_enabled: bool,
+ pub default_player: Option<String>,
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Self {
+ auto_update: true,
+ last_update_check: 0,
+ active_provider: ProviderKind::MovieBox,
+ active_theme: String::new(),
+ bdix_enabled: false,
+ default_player: None,
+ }
+ }
+}
+
+fn path() -> Option<PathBuf> {
+ dirs::config_dir().map(|dir| dir.join("moviebox-tui").join("config.json"))
+}
+
+pub fn load() -> Config {
+ let mut config = Config::default();
+ let Some(path) = path() else {
+ return config;
+ };
+ let Ok(content) = std::fs::read_to_string(path) else {
+ return config;
+ };
+ let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) else {
+ return config;
+ };
+ if let Some(v) = value.get("auto_update").and_then(|v| v.as_bool()) {
+ config.auto_update = v;
+ }
+ if let Some(v) = value.get("last_update_check").and_then(|v| v.as_u64()) {
+ config.last_update_check = v;
+ }
+ if value.get("active_provider").and_then(|v| v.as_str())
+ == Some(ProviderKind::FourKHdHub.cache_key())
+ {
+ config.active_provider = ProviderKind::FourKHdHub;
+ }
+ if let Some(v) = value.get("active_theme").and_then(|v| v.as_str()) {
+ config.active_theme = v.to_string();
+ }
+ if let Some(v) = value.get("bdix_enabled").and_then(|v| v.as_bool()) {
+ config.bdix_enabled = v;
+ }
+ if let Some(v) = value.get("default_player").and_then(|v| v.as_str()) {
+ config.default_player = Some(v.to_string());
+ }
+ config
+}
+
+pub fn save(config: &Config) {
+ let Some(path) = path() else {
+ return;
+ };
+ if let Some(app_dir) = path.parent()
+ && std::fs::create_dir_all(app_dir).is_err()
+ {
+ return;
+ }
+ let json = serde_json::json!({
+ "auto_update": config.auto_update,
+ "last_update_check": config.last_update_check,
+ "active_provider": config.active_provider.cache_key(),
+ "active_theme": config.active_theme,
+ "bdix_enabled": config.bdix_enabled,
+ "default_player": config.default_player
+ });
+ let temporary = path.with_extension(format!("{}.tmp", std::process::id()));
+ if std::fs::write(&temporary, json.to_string()).is_ok()
+ && std::fs::rename(&temporary, &path).is_err()
+ {
+ let _ = std::fs::remove_file(&path);
+ let _ = std::fs::rename(&temporary, &path);
+ }
+}
diff --git a/src/tui/mod.rs b/src/tui/mod.rs
index 16f16e7..0ac0af6 100644
--- a/src/tui/mod.rs
+++ b/src/tui/mod.rs
@@ -1,5 +1,6 @@
pub mod action;
pub mod app;
+pub mod config;
pub mod event;
pub mod overlay;
pub mod player;