diff --git a/rm-config/src/main_config.rs b/rm-config/src/main_config.rs new file mode 100644 index 0000000..b1d6960 --- /dev/null +++ b/rm-config/src/main_config.rs @@ -0,0 +1,129 @@ +use std::{io::ErrorKind, path::PathBuf, sync::OnceLock}; + +use anyhow::{Context, Result}; +use magnetease::WhichProvider; +use ratatui::style::Color; +use rm_shared::header::Header; +use serde::Deserialize; +use url::Url; + +use crate::utils::{self, ConfigFetchingError}; + +#[derive(Deserialize)] +pub struct MainConfig { + pub general: General, + pub connection: Connection, + #[serde(default)] + pub torrents_tab: TorrentsTab, + #[serde(default)] + pub search_tab: SearchTab, +} + +#[derive(Deserialize)] +pub struct General { + #[serde(default)] + pub auto_hide: bool, + #[serde(default = "default_accent_color")] + pub accent_color: Color, + #[serde(default = "default_beginner_mode")] + pub beginner_mode: bool, + #[serde(default)] + pub headers_hide: bool, +} + +fn default_accent_color() -> Color { + Color::LightMagenta +} + +fn default_beginner_mode() -> bool { + true +} + +#[derive(Deserialize)] +pub struct Connection { + pub username: Option, + pub password: Option, + pub url: Url, + #[serde(default = "default_refresh")] + pub torrents_refresh: u64, + #[serde(default = "default_refresh")] + pub stats_refresh: u64, + #[serde(default = "default_refresh")] + pub free_space_refresh: u64, +} + +fn default_refresh() -> u64 { + 5 +} + +#[derive(Deserialize)] +pub struct TorrentsTab { + #[serde(default = "default_headers")] + pub headers: Vec
, +} + +fn default_headers() -> Vec
{ + vec![ + Header::Name, + Header::SizeWhenDone, + Header::Progress, + Header::Eta, + Header::DownloadRate, + Header::UploadRate, + ] +} + +impl Default for TorrentsTab { + fn default() -> Self { + Self { + headers: default_headers(), + } + } +} + +#[derive(Deserialize)] +pub struct SearchTab { + pub providers: Vec, +} + +fn default_providers() -> Vec { + vec![WhichProvider::Knaben, WhichProvider::Nyaa] +} + +impl Default for SearchTab { + fn default() -> Self { + Self { + providers: default_providers(), + } + } +} + +impl MainConfig { + pub(crate) const FILENAME: &'static str = "config.toml"; + pub const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml"); + + pub(crate) fn init() -> Result { + match utils::fetch_config::(Self::FILENAME) { + Ok(config) => Ok(config), + Err(e) => match e { + ConfigFetchingError::Io(e) if e.kind() == ErrorKind::NotFound => { + utils::put_config::(Self::DEFAULT_CONFIG, Self::FILENAME)?; + println!("Update {:?} and start rustmission again", Self::path()); + std::process::exit(0); + } + ConfigFetchingError::Toml(e) => Err(e).with_context(|| { + format!( + "Failed to parse config located at {:?}", + utils::get_config_path(Self::FILENAME) + ) + }), + _ => anyhow::bail!(e), + }, + } + } + + pub(crate) fn path() -> &'static PathBuf { + static PATH: OnceLock = OnceLock::new(); + PATH.get_or_init(|| utils::get_config_path(Self::FILENAME)) + } +} diff --git a/rm-main/src/cli/mod.rs b/rm-main/src/cli/mod.rs index cdd3e5e..425406d 100644 --- a/rm-main/src/cli/mod.rs +++ b/rm-main/src/cli/mod.rs @@ -18,12 +18,20 @@ pub struct Args { pub enum Commands { AddTorrent { torrent: String }, FetchRss { url: String, filter: Option }, + PrintDefaultConfig {}, + PrintDefaultKeymap {}, } pub async fn handle_command(command: Commands) -> Result<()> { match command { Commands::AddTorrent { torrent } => add_torrent(torrent).await?, Commands::FetchRss { url, filter } => fetch_rss(&url, filter.as_deref()).await?, + Commands::PrintDefaultConfig {} => { + println!("{}", rm_config::main_config::MainConfig::DEFAULT_CONFIG) + } + Commands::PrintDefaultKeymap {} => { + println!("{}", rm_config::keymap::KeymapConfig::DEFAULT_CONFIG) + } } Ok(()) }