From cb3bb56671d79eef68e819dac2f0c472c9e144a9 Mon Sep 17 00:00:00 2001 From: micielski <73398428+micielski@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:14:09 +0200 Subject: [PATCH] refactor: make config static (#86) --- rm-config/src/lib.rs | 11 +++++-- rm-main/src/app.rs | 28 ++++++----------- rm-main/src/cli.rs | 15 +++++---- rm-main/src/main.rs | 11 +++---- rm-main/src/transmission/fetchers.rs | 10 +++--- rm-main/src/transmission/utils.rs | 10 +++--- rm-main/src/ui/components/tabs.rs | 5 +-- rm-main/src/ui/global_popups/help.rs | 31 +++++++------------ rm-main/src/ui/tabs/search/bottom_bar.rs | 7 ++--- rm-main/src/ui/tabs/search/mod.rs | 17 +++++----- rm-main/src/ui/tabs/search/popups/mod.rs | 5 +-- .../src/ui/tabs/search/popups/providers.rs | 19 +++++------- rm-main/src/ui/tabs/torrents/input_manager.rs | 12 +++---- rm-main/src/ui/tabs/torrents/mod.rs | 16 +++++----- rm-main/src/ui/tabs/torrents/popups/files.rs | 27 +++++----------- rm-main/src/ui/tabs/torrents/popups/stats.rs | 17 +++++----- rm-main/src/ui/tabs/torrents/table_manager.rs | 21 ++++++------- rm-main/src/ui/tabs/torrents/task_manager.rs | 4 +-- .../src/ui/tabs/torrents/tasks/add_magnet.rs | 6 +--- rm-main/src/ui/tabs/torrents/tasks/default.rs | 17 +++++----- .../ui/tabs/torrents/tasks/delete_torrent.rs | 2 +- rm-main/src/ui/tabs/torrents/tasks/filter.rs | 2 +- .../ui/tabs/torrents/tasks/move_torrent.rs | 2 +- 23 files changed, 123 insertions(+), 172 deletions(-) diff --git a/rm-config/src/lib.rs b/rm-config/src/lib.rs index 73eaff6..2853d32 100644 --- a/rm-config/src/lib.rs +++ b/rm-config/src/lib.rs @@ -2,12 +2,19 @@ pub mod keymap; pub mod main_config; mod utils; -use std::path::PathBuf; +use std::{path::PathBuf, sync::LazyLock}; use anyhow::Result; use keymap::KeymapConfig; use main_config::MainConfig; +pub static CONFIG: LazyLock = LazyLock::new(|| { + Config::init().unwrap_or_else(|e| { + eprintln!("{:?}", e); + std::process::exit(1); + }) +}); + pub struct Config { pub general: main_config::General, pub connection: main_config::Connection, @@ -23,7 +30,7 @@ pub struct Directories { } impl Config { - pub fn init() -> Result { + fn init() -> Result { let main_config = MainConfig::init()?; let keybindings = KeymapConfig::init()?; diff --git a/rm-main/src/app.rs b/rm-main/src/app.rs index 7a20a69..d44d032 100644 --- a/rm-main/src/app.rs +++ b/rm-main/src/app.rs @@ -1,7 +1,7 @@ use crossterm::event::Event; use crossterm::event::KeyCode; use crossterm::event::KeyModifiers; -use rm_config::Config; +use rm_config::CONFIG; use rm_shared::action::Action; use rm_shared::action::UpdateAction; use std::sync::Arc; @@ -19,7 +19,6 @@ use transmission_rpc::{types::SessionGet, TransClient}; #[derive(Clone)] pub struct Ctx { - pub config: Arc, pub session_info: Arc, action_tx: UnboundedSender, update_tx: UnboundedSender, @@ -29,7 +28,6 @@ pub struct Ctx { impl Ctx { async fn new( client: &mut TransClient, - config: Config, action_tx: UnboundedSender, update_tx: UnboundedSender, trans_tx: UnboundedSender, @@ -39,7 +37,6 @@ impl Ctx { Ok(res) => { let session_info = Arc::new(res.arguments); Ok(Self { - config: Arc::new(config), action_tx, trans_tx, update_tx, @@ -47,7 +44,7 @@ impl Ctx { }) } Err(e) => { - let config_path = config.directories.main_path; + let config_path = CONFIG.directories.main_path; Err(Error::msg(format!( "{e}\nIs the connection info in {:?} correct?", config_path @@ -79,21 +76,14 @@ pub struct App { } impl App { - pub async fn new(config: Config) -> Result { + pub async fn new() -> Result { let (action_tx, action_rx) = mpsc::unbounded_channel(); let (update_tx, update_rx) = mpsc::unbounded_channel(); - let mut client = transmission::utils::client_from_config(&config); + let mut client = transmission::utils::new_client(); let (trans_tx, trans_rx) = mpsc::unbounded_channel(); - let ctx = Ctx::new( - &mut client, - config, - action_tx.clone(), - update_tx.clone(), - trans_tx, - ) - .await?; + let ctx = Ctx::new(&mut client, action_tx.clone(), update_tx.clone(), trans_tx).await?; tokio::spawn(transmission::action_handler(client, trans_rx, update_tx)); @@ -217,12 +207,12 @@ pub fn event_to_action(ctx: &Ctx, mode: Mode, current_tab: CurrentTab, event: Ev Event::Key(key) => { let keymaps = match current_tab { CurrentTab::Torrents => [ - &ctx.config.keybindings.general_keymap, - &ctx.config.keybindings.torrent_keymap, + &CONFIG.keybindings.general_keymap, + &CONFIG.keybindings.torrent_keymap, ], CurrentTab::Search => [ - &ctx.config.keybindings.general_keymap, - &ctx.config.keybindings.search_keymap, + &CONFIG.keybindings.general_keymap, + &CONFIG.keybindings.search_keymap, ], }; diff --git a/rm-main/src/cli.rs b/rm-main/src/cli.rs index 0aacce7..7f17a54 100644 --- a/rm-main/src/cli.rs +++ b/rm-main/src/cli.rs @@ -4,7 +4,6 @@ use anyhow::{bail, Result}; use base64::Engine; use clap::{Parser, Subcommand}; use regex::Regex; -use rm_config::Config; use transmission_rpc::types::TorrentAddArgs; use crate::transmission; @@ -22,16 +21,16 @@ pub enum Commands { FetchRss { url: String, filter: Option }, } -pub async fn handle_command(config: &Config, command: Commands) -> Result<()> { +pub async fn handle_command(command: Commands) -> Result<()> { match command { - Commands::AddTorrent { torrent } => add_torrent(config, torrent).await?, - Commands::FetchRss { url, filter } => fetch_rss(config, &url, filter.as_deref()).await?, + Commands::AddTorrent { torrent } => add_torrent(torrent).await?, + Commands::FetchRss { url, filter } => fetch_rss(&url, filter.as_deref()).await?, } Ok(()) } -async fn add_torrent(config: &Config, torrent: String) -> Result<()> { - let mut transclient = transmission::utils::client_from_config(config); +async fn add_torrent(torrent: String) -> Result<()> { + let mut transclient = transmission::utils::new_client(); let args = { if torrent.starts_with("magnet:") || torrent.starts_with("http:") @@ -69,8 +68,8 @@ async fn add_torrent(config: &Config, torrent: String) -> Result<()> { Ok(()) } -async fn fetch_rss(config: &Config, url: &str, filter: Option<&str>) -> Result<()> { - let mut transclient = transmission::utils::client_from_config(config); +async fn fetch_rss(url: &str, filter: Option<&str>) -> Result<()> { + let mut transclient = transmission::utils::new_client(); let content = reqwest::get(url).await?.bytes().await?; let channel = rss::Channel::read_from(&content[..])?; let re: Option = { diff --git a/rm-main/src/main.rs b/rm-main/src/main.rs index 433c5e7..e4b60a7 100644 --- a/rm-main/src/main.rs +++ b/rm-main/src/main.rs @@ -5,7 +5,6 @@ pub mod tui; mod ui; use app::App; -use rm_config::Config; use anyhow::Result; use clap::Parser; @@ -14,19 +13,17 @@ use clap::Parser; async fn main() -> Result<()> { let args = cli::Args::parse(); - let config = Config::init()?; - if let Some(command) = args.command { - cli::handle_command(&config, command).await?; + cli::handle_command(command).await?; } else { - run_tui(config).await?; + run_tui().await?; } Ok(()) } -async fn run_tui(config: Config) -> Result<()> { - let mut app = App::new(config).await?; +async fn run_tui() -> Result<()> { + let mut app = App::new().await?; app.run().await?; Ok(()) } diff --git a/rm-main/src/transmission/fetchers.rs b/rm-main/src/transmission/fetchers.rs index dab47cc..6840123 100644 --- a/rm-main/src/transmission/fetchers.rs +++ b/rm-main/src/transmission/fetchers.rs @@ -1,5 +1,6 @@ use std::{sync::Arc, time::Duration}; +use rm_config::CONFIG; use tokio::sync::oneshot; use transmission_rpc::types::TorrentGetField; @@ -22,7 +23,7 @@ pub async fn stats(ctx: app::Ctx) { } }; - tokio::time::sleep(Duration::from_secs(ctx.config.connection.stats_refresh)).await; + tokio::time::sleep(Duration::from_secs(CONFIG.connection.stats_refresh)).await; } } @@ -57,10 +58,7 @@ pub async fn free_space(ctx: app::Ctx) { } } - tokio::time::sleep(Duration::from_secs( - ctx.config.connection.free_space_refresh, - )) - .await; + tokio::time::sleep(Duration::from_secs(CONFIG.connection.free_space_refresh)).await; } } @@ -98,6 +96,6 @@ pub async fn torrents(ctx: app::Ctx) { } }; - tokio::time::sleep(Duration::from_secs(ctx.config.connection.torrents_refresh)).await; + tokio::time::sleep(Duration::from_secs(CONFIG.connection.torrents_refresh)).await; } } diff --git a/rm-main/src/transmission/utils.rs b/rm-main/src/transmission/utils.rs index f8d9b0f..e424d5d 100644 --- a/rm-main/src/transmission/utils.rs +++ b/rm-main/src/transmission/utils.rs @@ -1,14 +1,14 @@ -use rm_config::Config; +use rm_config::CONFIG; use transmission_rpc::{types::BasicAuth, TransClient}; -pub fn client_from_config(config: &Config) -> TransClient { - let user = config +pub fn new_client() -> TransClient { + let user = CONFIG .connection .username .as_ref() .unwrap_or(&"".to_string()) .clone(); - let password = config + let password = CONFIG .connection .password .as_ref() @@ -17,5 +17,5 @@ pub fn client_from_config(config: &Config) -> TransClient { let auth = BasicAuth { user, password }; - TransClient::with_auth(config.connection.url.clone(), auth) + TransClient::with_auth(CONFIG.connection.url.clone(), auth) } diff --git a/rm-main/src/ui/components/tabs.rs b/rm-main/src/ui/components/tabs.rs index aa5268a..c92d8c8 100644 --- a/rm-main/src/ui/components/tabs.rs +++ b/rm-main/src/ui/components/tabs.rs @@ -1,4 +1,5 @@ use crate::app; +use rm_config::CONFIG; use rm_shared::action::Action; use super::{Component, ComponentAction}; @@ -19,7 +20,7 @@ pub struct TabComponent { impl TabComponent { pub fn new(ctx: app::Ctx) -> Self { let tabs_list = { - if ctx.config.general.beginner_mode { + if CONFIG.general.beginner_mode { ["1. Torrents", "2. Search"] } else { ["Torrents", "Search"] @@ -52,7 +53,7 @@ impl Component for TabComponent { .flex(Flex::Center) .split(rect)[0]; - let tabs_highlight_style = Style::default().fg(self.ctx.config.general.accent_color); + let tabs_highlight_style = Style::default().fg(CONFIG.general.accent_color); let tabs = Tabs::new(self.tabs_list) .style(Style::default().white()) .highlight_style(tabs_highlight_style) diff --git a/rm-main/src/ui/global_popups/help.rs b/rm-main/src/ui/global_popups/help.rs index 85e71df..32e8ef7 100644 --- a/rm-main/src/ui/global_popups/help.rs +++ b/rm-main/src/ui/global_popups/help.rs @@ -15,7 +15,10 @@ use crate::{ components::{Component, ComponentAction}, }, }; -use rm_config::keymap::{actions::UserAction, Keybinding}; +use rm_config::{ + keymap::{actions::UserAction, Keybinding}, + CONFIG, +}; use rm_shared::action::Action; macro_rules! add_line { @@ -165,17 +168,13 @@ impl Component for HelpPopup { let popup_rect = centered_rect.inner(Margin::new(1, 1)); let text_rect = popup_rect.inner(Margin::new(3, 2)); - let title_style = Style::new().fg(self.ctx.config.general.accent_color); + let title_style = Style::new().fg(CONFIG.general.accent_color); let block = Block::bordered() .border_set(symbols::border::ROUNDED) .title( - Title::from( - " [ CLOSE ] " - .fg(self.ctx.config.general.accent_color) - .bold(), - ) - .alignment(Alignment::Right) - .position(Position::Bottom), + Title::from(" [ CLOSE ] ".fg(CONFIG.general.accent_color).bold()) + .alignment(Alignment::Right) + .position(Position::Bottom), ) .title(" Help ") .title_style(title_style); @@ -186,7 +185,7 @@ impl Component for HelpPopup { )]) .centered()]; - Self::write_keybindings(&self.ctx.config.keybindings.general.keybindings, &mut lines); + Self::write_keybindings(&CONFIG.keybindings.general.keybindings, &mut lines); lines.push( Line::from(vec![Span::styled( @@ -196,10 +195,7 @@ impl Component for HelpPopup { .centered(), ); - Self::write_keybindings( - &self.ctx.config.keybindings.torrents_tab.keybindings, - &mut lines, - ); + Self::write_keybindings(&CONFIG.keybindings.torrents_tab.keybindings, &mut lines); lines.push( Line::from(vec![Span::styled( @@ -209,10 +205,7 @@ impl Component for HelpPopup { .centered(), ); - Self::write_keybindings( - &self.ctx.config.keybindings.search_tab.keybindings, - &mut lines, - ); + Self::write_keybindings(&CONFIG.keybindings.search_tab.keybindings, &mut lines); let help_text = Text::from(lines); @@ -254,7 +247,7 @@ impl Component for HelpPopup { if let Some(scroll) = &mut self.scroll { let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight) - .thumb_style(Style::default().fg(self.ctx.config.general.accent_color)); + .thumb_style(Style::default().fg(CONFIG.general.accent_color)); f.render_stateful_widget( scrollbar, diff --git a/rm-main/src/ui/tabs/search/bottom_bar.rs b/rm-main/src/ui/tabs/search/bottom_bar.rs index 6a9b70a..2b74954 100644 --- a/rm-main/src/ui/tabs/search/bottom_bar.rs +++ b/rm-main/src/ui/tabs/search/bottom_bar.rs @@ -5,6 +5,7 @@ use ratatui::{ widgets::Paragraph, Frame, }; +use rm_config::CONFIG; use rm_shared::action::{Action, UpdateAction}; use throbber_widgets_tui::ThrobberState; @@ -102,9 +103,7 @@ impl Component for SearchState { fn render(&mut self, f: &mut Frame, rect: Rect) { let append_key_info = |line: &mut Line| { - let providers_key = self - .ctx - .config + let providers_key = CONFIG .keybindings .get_keys_for_action(Action::ShowProvidersInfo); if let Some(key) = providers_key { @@ -112,7 +111,7 @@ impl Component for SearchState { line.push_span(Span::styled( key, Style::default() - .fg(self.ctx.config.general.accent_color) + .fg(CONFIG.general.accent_color) .underlined(), )); line.push_span(Span::raw(" for details.")) diff --git a/rm-main/src/ui/tabs/search/mod.rs b/rm-main/src/ui/tabs/search/mod.rs index 48ba645..4f0e43f 100644 --- a/rm-main/src/ui/tabs/search/mod.rs +++ b/rm-main/src/ui/tabs/search/mod.rs @@ -14,6 +14,7 @@ use ratatui::{ widgets::{Cell, Paragraph, Row, Table}, }; use reqwest::Client; +use rm_config::CONFIG; use tokio::sync::mpsc::{self, UnboundedSender}; use tui_input::Input; @@ -60,8 +61,7 @@ impl SearchTab { } for configured_provider in &mut configured_providers { - if ctx - .config + if CONFIG .search_tab .providers .contains(&configured_provider.provider) @@ -361,7 +361,7 @@ impl Component for SearchTab { if self.focus == SearchTabFocus::Search { Style::default() .underlined() - .fg(self.ctx.config.general.accent_color) + .fg(CONFIG.general.accent_color) } else { Style::default().underlined().gray() } @@ -392,15 +392,14 @@ impl Component for SearchTab { Constraint::Length(8), // Size ]; - let table_higlight_style = Style::default().on_black().bold().fg(self - .ctx - .config - .general - .accent_color); + let table_higlight_style = Style::default() + .on_black() + .bold() + .fg(CONFIG.general.accent_color); let table = { let table = Table::new(items, widths).highlight_style(table_higlight_style); - if !self.ctx.config.general.headers_hide { + if !CONFIG.general.headers_hide { table.header(header) } else { table diff --git a/rm-main/src/ui/tabs/search/popups/mod.rs b/rm-main/src/ui/tabs/search/popups/mod.rs index 2521844..443ff05 100644 --- a/rm-main/src/ui/tabs/search/popups/mod.rs +++ b/rm-main/src/ui/tabs/search/popups/mod.rs @@ -38,10 +38,7 @@ impl PopupManager { } pub fn show_providers_info_popup(&mut self, providers: Vec) { - self.show_popup(CurrentPopup::Providers(ProvidersPopup::new( - self.ctx.clone(), - providers, - ))); + self.show_popup(CurrentPopup::Providers(ProvidersPopup::new(providers))); } pub fn close_popup(&mut self) { diff --git a/rm-main/src/ui/tabs/search/popups/providers.rs b/rm-main/src/ui/tabs/search/popups/providers.rs index 5219a30..3602c64 100644 --- a/rm-main/src/ui/tabs/search/popups/providers.rs +++ b/rm-main/src/ui/tabs/search/popups/providers.rs @@ -10,19 +10,16 @@ use ratatui::{ }, Frame, }; +use rm_config::CONFIG; use rm_shared::action::Action; -use crate::{ - app, - ui::{ - centered_rect, - components::{Component, ComponentAction}, - tabs::search::{ConfiguredProvider, ProviderState}, - }, +use crate::ui::{ + centered_rect, + components::{Component, ComponentAction}, + tabs::search::{ConfiguredProvider, ProviderState}, }; pub struct ProvidersPopup { - ctx: app::Ctx, providers: Vec, } @@ -70,8 +67,8 @@ impl From<&ConfiguredProvider> for Row<'_> { } impl ProvidersPopup { - pub const fn new(ctx: app::Ctx, providers: Vec) -> Self { - Self { ctx, providers } + pub const fn new(providers: Vec) -> Self { + Self { providers } } pub fn update_providers(&mut self, providers: Vec) { @@ -93,7 +90,7 @@ impl Component for ProvidersPopup { let block_rect = popup_rect.inner(Margin::new(1, 1)); let table_rect = block_rect.inner(Margin::new(1, 1)); - let title_style = Style::default().fg(self.ctx.config.general.accent_color); + let title_style = Style::default().fg(CONFIG.general.accent_color); let block = Block::bordered() .border_type(BorderType::Rounded) .title(Title::from(" Providers ".set_style(title_style))) diff --git a/rm-main/src/ui/tabs/torrents/input_manager.rs b/rm-main/src/ui/tabs/torrents/input_manager.rs index 498ef7b..3e75f5b 100644 --- a/rm-main/src/ui/tabs/torrents/input_manager.rs +++ b/rm-main/src/ui/tabs/torrents/input_manager.rs @@ -2,28 +2,26 @@ use ratatui::{ prelude::*, widgets::{Clear, Paragraph}, }; +use rm_config::CONFIG; use tui_input::{Input, InputRequest}; -use crate::{app, ui::components::Component}; +use crate::ui::components::Component; pub struct InputManager { input: Input, prompt: String, - ctx: app::Ctx, } impl InputManager { - pub fn new(ctx: app::Ctx, prompt: String) -> Self { + pub fn new(prompt: String) -> Self { Self { - ctx, prompt, input: Input::default(), } } - pub fn new_with_value(ctx: app::Ctx, prompt: String, value: String) -> Self { + pub fn new_with_value(prompt: String, value: String) -> Self { Self { - ctx, prompt, input: Input::default().with_value(value), } @@ -45,7 +43,7 @@ impl Component for InputManager { let spans = vec![ Span::styled( self.prompt.as_str(), - Style::default().fg(self.ctx.config.general.accent_color), + Style::default().fg(CONFIG.general.accent_color), ), Span::raw(self.text()), ]; diff --git a/rm-main/src/ui/tabs/torrents/mod.rs b/rm-main/src/ui/tabs/torrents/mod.rs index 5f74cc4..60ee6c1 100644 --- a/rm-main/src/ui/tabs/torrents/mod.rs +++ b/rm-main/src/ui/tabs/torrents/mod.rs @@ -11,6 +11,7 @@ use crate::ui::tabs::torrents::popups::stats::StatisticsPopup; use ratatui::prelude::*; use ratatui::widgets::{Row, Table}; +use rm_config::CONFIG; use rm_shared::status_task::StatusTask; use rustmission_torrent::RustmissionTorrent; use transmission_rpc::types::TorrentStatus; @@ -35,7 +36,7 @@ pub struct TorrentsTab { impl TorrentsTab { pub fn new(ctx: app::Ctx) -> Self { - let table_manager = TableManager::new(ctx.clone()); + let table_manager = TableManager::new(); let bottom_stats = BottomStats::new(); tokio::spawn(transmission::fetchers::stats(ctx.clone())); @@ -160,16 +161,15 @@ impl TorrentsTab { fn render_table(&mut self, f: &mut Frame, rect: Rect) { self.table_manager.torrents_displaying_no = rect.height; - let highlight_table_style = Style::default().on_black().bold().fg(self - .ctx - .config - .general - .accent_color); + let highlight_table_style = Style::default() + .on_black() + .bold() + .fg(CONFIG.general.accent_color); let table_widget = { let table = Table::new(self.table_manager.rows(), &self.table_manager.widths) .highlight_style(highlight_table_style); - if !self.ctx.config.general.headers_hide { + if CONFIG.general.headers_hide { table.header(Row::new(self.table_manager.headers().iter().cloned())) } else { table @@ -193,7 +193,7 @@ impl TorrentsTab { fn show_statistics_popup(&mut self) { if let Some(stats) = &self.bottom_stats.stats { - let popup = StatisticsPopup::new(self.ctx.clone(), stats.clone()); + let popup = StatisticsPopup::new(stats.clone()); self.popup_manager.show_popup(CurrentPopup::Stats(popup)); self.ctx.send_action(Action::Render) } diff --git a/rm-main/src/ui/tabs/torrents/popups/files.rs b/rm-main/src/ui/tabs/torrents/popups/files.rs index 8f336dc..520d429 100644 --- a/rm-main/src/ui/tabs/torrents/popups/files.rs +++ b/rm-main/src/ui/tabs/torrents/popups/files.rs @@ -8,6 +8,7 @@ use ratatui::{ Block, BorderType, Clear, Paragraph, }, }; +use rm_config::CONFIG; use tokio::{sync::oneshot, task::JoinHandle}; use transmission_rpc::types::{Id, Torrent, TorrentSetArgs}; use tui_tree_widget::{Tree, TreeItem, TreeState}; @@ -247,7 +248,7 @@ impl Component for FilesPopup { let info_text_rect = block_rect.inner(Margin::new(3, 2)); - let highlight_style = Style::default().fg(self.ctx.config.general.accent_color); + let highlight_style = Style::default().fg(CONFIG.general.accent_color); let bold_highlight_style = highlight_style.on_black().bold(); let block = Block::bordered() @@ -282,36 +283,22 @@ impl Component for FilesPopup { let download_dir = torrent.download_dir.as_ref().expect("Requested"); let keybinding_tip = { - if self.ctx.config.general.beginner_mode { + if CONFIG.general.beginner_mode { let mut keys = vec![]; - if let Some(key) = self - .ctx - .config - .keybindings - .get_keys_for_action(Action::Select) - { + if let Some(key) = CONFIG.keybindings.get_keys_for_action(Action::Select) { keys.push(Span::raw(" ")); keys.push(Span::styled( key, - Style::new() - .fg(self.ctx.config.general.accent_color) - .underlined(), + Style::new().fg(CONFIG.general.accent_color).underlined(), )); keys.push(Span::raw(" - toggle | ")); } - if let Some(key) = self - .ctx - .config - .keybindings - .get_keys_for_action(Action::XdgOpen) - { + if let Some(key) = CONFIG.keybindings.get_keys_for_action(Action::XdgOpen) { keys.push(Span::styled( key, - Style::new() - .fg(self.ctx.config.general.accent_color) - .underlined(), + Style::new().fg(CONFIG.general.accent_color).underlined(), )); keys.push(Span::raw(" - xdg_open ")); } diff --git a/rm-main/src/ui/tabs/torrents/popups/stats.rs b/rm-main/src/ui/tabs/torrents/popups/stats.rs index 96855aa..f301c74 100644 --- a/rm-main/src/ui/tabs/torrents/popups/stats.rs +++ b/rm-main/src/ui/tabs/torrents/popups/stats.rs @@ -8,25 +8,22 @@ use ratatui::{ Block, BorderType, Clear, Paragraph, }, }; +use rm_config::CONFIG; use transmission_rpc::types::SessionStats; -use crate::{ - app, - ui::{ - centered_rect, - components::{Component, ComponentAction}, - }, +use crate::ui::{ + centered_rect, + components::{Component, ComponentAction}, }; use rm_shared::{action::Action, utils::bytes_to_human_format}; pub struct StatisticsPopup { stats: Arc, - ctx: app::Ctx, } impl StatisticsPopup { - pub const fn new(ctx: app::Ctx, stats: Arc) -> Self { - Self { ctx, stats } + pub const fn new(stats: Arc) -> Self { + Self { stats } } } @@ -45,7 +42,7 @@ impl Component for StatisticsPopup { let block_rect = popup_rect.inner(Margin::new(1, 1)); let text_rect = block_rect.inner(Margin::new(3, 2)); - let title_style = Style::default().fg(self.ctx.config.general.accent_color); + let title_style = Style::default().fg(CONFIG.general.accent_color); let block = Block::bordered() .border_type(BorderType::Rounded) .title(Title::from(" Statistics ".set_style(title_style))) diff --git a/rm-main/src/ui/tabs/torrents/table_manager.rs b/rm-main/src/ui/tabs/torrents/table_manager.rs index 2a1d8a1..72d6a73 100644 --- a/rm-main/src/ui/tabs/torrents/table_manager.rs +++ b/rm-main/src/ui/tabs/torrents/table_manager.rs @@ -1,14 +1,14 @@ use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use ratatui::{prelude::*, widgets::Row}; +use rm_config::CONFIG; use rm_shared::header::Header; use std::collections::HashMap; -use crate::{app, ui::components::table::GenericTable}; +use crate::ui::components::table::GenericTable; use super::rustmission_torrent::RustmissionTorrent; pub struct TableManager { - ctx: app::Ctx, pub table: GenericTable, pub widths: Vec, pub filter: Option, @@ -23,16 +23,15 @@ pub struct Filter { } impl TableManager { - pub fn new(ctx: app::Ctx) -> Self { + pub fn new() -> Self { let table = GenericTable::new(vec![]); - let widths = Self::default_widths(&ctx.config.torrents_tab.headers); + let widths = Self::default_widths(&CONFIG.torrents_tab.headers); let mut headers = vec![]; - for header in &ctx.config.torrents_tab.headers { + for header in &CONFIG.torrents_tab.headers { headers.push(header.header_name()); } Self { - ctx, table, widths, filter: None, @@ -51,8 +50,8 @@ impl TableManager { pub fn rows(&self) -> Vec> { if let Some(filter) = &self.filter { - let highlight_style = Style::default().fg(self.ctx.config.general.accent_color); - let headers = &self.ctx.config.torrents_tab.headers; + let highlight_style = Style::default().fg(CONFIG.general.accent_color); + let headers = &CONFIG.torrents_tab.headers; let mut rows = vec![]; for (i, which_torrent) in filter.indexes.iter().enumerate() { let row = self.table.items[*which_torrent as usize].to_row_with_higlighted_indices( @@ -69,7 +68,7 @@ impl TableManager { self.table .items .iter() - .map(|t| t.to_row(&self.ctx.config.torrents_tab.headers)) + .map(|t| t.to_row(&CONFIG.torrents_tab.headers)) .collect() } } @@ -130,9 +129,9 @@ impl TableManager { } fn header_widths(&self, rows: &[RustmissionTorrent]) -> Vec { - let headers = &self.ctx.config.torrents_tab.headers; + let headers = &CONFIG.torrents_tab.headers; - if !self.ctx.config.general.auto_hide { + if !CONFIG.general.auto_hide { return Self::default_widths(headers); } diff --git a/rm-main/src/ui/tabs/torrents/task_manager.rs b/rm-main/src/ui/tabs/torrents/task_manager.rs index c98ba5e..4e4d24f 100644 --- a/rm-main/src/ui/tabs/torrents/task_manager.rs +++ b/rm-main/src/ui/tabs/torrents/task_manager.rs @@ -32,7 +32,7 @@ pub struct TaskManager { impl TaskManager { pub fn new(ctx: app::Ctx) -> Self { Self { - current_task: CurrentTask::Default(DefaultBar::new(ctx.clone())), + current_task: CurrentTask::Default(DefaultBar::new()), ctx, } } @@ -185,7 +185,7 @@ impl TaskManager { return; } - self.current_task = CurrentTask::Default(DefaultBar::new(self.ctx.clone())); + self.current_task = CurrentTask::Default(DefaultBar::new()); self.ctx .send_update_action(UpdateAction::SwitchToNormalMode); } diff --git a/rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs b/rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs index 616c96f..d2d07f0 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs @@ -30,12 +30,8 @@ enum Stage { impl AddMagnetBar { pub fn new(ctx: app::Ctx) -> Self { Self { - input_magnet_mgr: InputManager::new( - ctx.clone(), - "Add (Magnet URL / Torrent path): ".to_string(), - ), + input_magnet_mgr: InputManager::new("Add (Magnet URL / Torrent path): ".to_string()), input_location_mgr: InputManager::new_with_value( - ctx.clone(), "Directory: ".to_string(), ctx.session_info.download_dir.clone(), ), diff --git a/rm-main/src/ui/tabs/torrents/tasks/default.rs b/rm-main/src/ui/tabs/torrents/tasks/default.rs index f508350..03013d4 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/default.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/default.rs @@ -1,23 +1,20 @@ -use crate::{app, ui::components::Component}; +use crate::ui::components::Component; use ratatui::prelude::*; +use rm_config::CONFIG; -pub struct DefaultBar { - ctx: app::Ctx, -} +pub struct DefaultBar {} impl DefaultBar { - pub const fn new(ctx: app::Ctx) -> Self { - Self { ctx } + pub const fn new() -> Self { + Self {} } } impl Component for DefaultBar { fn render(&mut self, f: &mut ratatui::Frame<'_>, rect: Rect) { - if self.ctx.config.general.beginner_mode { - if let Some(keys) = self - .ctx - .config + if CONFIG.general.beginner_mode { + if let Some(keys) = CONFIG .keybindings .get_keys_for_action(rm_shared::action::Action::ShowHelp) { diff --git a/rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs b/rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs index 5584959..3544820 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs @@ -43,7 +43,7 @@ impl DeleteBar { Self { torrents_to_delete: to_delete, - input_mgr: InputManager::new(ctx.clone(), prompt), + input_mgr: InputManager::new(prompt), ctx, mode, } diff --git a/rm-main/src/ui/tabs/torrents/tasks/filter.rs b/rm-main/src/ui/tabs/torrents/tasks/filter.rs index b25f214..c0e1848 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/filter.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/filter.rs @@ -26,7 +26,7 @@ impl FilterBar { } }; - let input = InputManager::new_with_value(ctx.clone(), "Search: ".to_string(), filter); + let input = InputManager::new_with_value("Search: ".to_string(), filter); Self { ctx, input } } } diff --git a/rm-main/src/ui/tabs/torrents/tasks/move_torrent.rs b/rm-main/src/ui/tabs/torrents/tasks/move_torrent.rs index 4995422..e7febce 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/move_torrent.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/move_torrent.rs @@ -28,7 +28,7 @@ impl MoveBar { Self { torrents_to_move, - input_mgr: InputManager::new_with_value(ctx.clone(), prompt, existing_location), + input_mgr: InputManager::new_with_value(prompt, existing_location), ctx, } }