From e4f0a63fe4f352330a16ef78ddd7c00ff97402a6 Mon Sep 17 00:00:00 2001 From: micielski <73398428+micielski@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:29:09 +0000 Subject: [PATCH] refactor: various code cleanups (#24) * refactor: move getting rows to table manager * chore: move rm-color to rm-config * chore: remove rm-color crate * fix: add missing deps for color tests --- Cargo.lock | 11 +-- Cargo.toml | 2 +- rm-color/Cargo.toml | 11 --- rm-config/Cargo.toml | 5 +- rm-color/src/lib.rs => rm-config/src/color.rs | 0 rm-config/src/lib.rs | 6 +- rm-main/src/ui/tabs/torrents/mod.rs | 18 +---- rm-main/src/ui/tabs/torrents/table_manager.rs | 78 +++++++++++-------- 8 files changed, 58 insertions(+), 73 deletions(-) delete mode 100644 rm-color/Cargo.toml rename rm-color/src/lib.rs => rm-config/src/color.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 85ad60b..f986c02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,23 +1150,14 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rm-color" -version = "0.1.0" -dependencies = [ - "ratatui", - "serde", - "serde_json", -] - [[package]] name = "rm-config" version = "0.3.0" dependencies = [ "anyhow", "ratatui", - "rm-color", "serde", + "serde_json", "toml", "url", "xdg", diff --git a/Cargo.toml b/Cargo.toml index ed2da95..1de496d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ resolver = "2" members = [ "rm-main", - "rm-config", "rm-color", + "rm-config", ] # Config for 'cargo dist' diff --git a/rm-color/Cargo.toml b/rm-color/Cargo.toml deleted file mode 100644 index 3872a40..0000000 --- a/rm-color/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "rm-color" -version = "0.1.0" -edition = "2021" - -[dependencies] -ratatui = { version = "0.26.3", default-features = false } -serde = { version="1.0", features = ["derive"]} - -[dev-dependencies] -serde_json = "1.0.117" diff --git a/rm-config/Cargo.toml b/rm-config/Cargo.toml index c057d70..62ea201 100644 --- a/rm-config/Cargo.toml +++ b/rm-config/Cargo.toml @@ -14,4 +14,7 @@ serde = { version = "1", features = ["derive"] } anyhow = "1" url = "2.5" ratatui = "0.26" -rm-color = { version = "0.1.0", path = "../rm-color" } + +[dev-dependencies] +serde_json = "1" + diff --git a/rm-color/src/lib.rs b/rm-config/src/color.rs similarity index 100% rename from rm-color/src/lib.rs rename to rm-config/src/color.rs diff --git a/rm-config/src/lib.rs b/rm-config/src/lib.rs index 8de59d7..bd566dd 100644 --- a/rm-config/src/lib.rs +++ b/rm-config/src/lib.rs @@ -1,3 +1,5 @@ +mod color; + use std::{ fs::File, io::{Read, Write}, @@ -6,7 +8,7 @@ use std::{ }; use anyhow::{bail, Context, Result}; -use rm_color::Color; +use color::Color; use serde::{Deserialize, Serialize}; use toml::Table; use xdg::BaseDirectories; @@ -21,7 +23,7 @@ pub struct Config { pub struct General { #[serde(default)] pub auto_hide: bool, - #[serde(default, with = "rm_color")] + #[serde(default, with = "color")] pub accent_color: Color, #[serde(default = "default_beginner_mode")] pub beginner_mode: bool, diff --git a/rm-main/src/ui/tabs/torrents/mod.rs b/rm-main/src/ui/tabs/torrents/mod.rs index 105aff2..173b0f6 100644 --- a/rm-main/src/ui/tabs/torrents/mod.rs +++ b/rm-main/src/ui/tabs/torrents/mod.rs @@ -23,7 +23,6 @@ use crate::{app, transmission}; use self::bottom_stats::BottomStats; use self::popups::files::FilesPopup; use self::popups::{CurrentPopup, PopupManager}; -use self::rustmission_torrent::RustmissionTorrent; use self::table_manager::TableManager; use self::task_manager::TaskManager; @@ -41,7 +40,7 @@ impl TorrentsTab { let table_manager = Arc::new(Mutex::new(TableManager::new(ctx.clone(), table))); let stats = Arc::new(Mutex::new(None)); let free_space = Arc::new(Mutex::new(None)); - let bottom_stats = BottomStats::new(stats, free_space, table_manager.clone()); + let bottom_stats = BottomStats::new(stats, free_space, Arc::clone(&table_manager)); tokio::spawn(transmission::fetchers::stats( ctx.clone(), @@ -113,20 +112,7 @@ impl TorrentsTab { let table_manager_lock = &mut *self.table_manager.lock().unwrap(); table_manager_lock.torrents_displaying_no = rect.height; - let torrent_rows: Vec<_> = if let Some(filter) = &*table_manager_lock.filter.lock().unwrap() - { - let torrent_rows = - table_manager_lock.filtered_torrents_rows(&table_manager_lock.table.items, filter); - table_manager_lock.table.overwrite_len(torrent_rows.len()); - torrent_rows - } else { - table_manager_lock - .table - .items - .iter() - .map(RustmissionTorrent::to_row) - .collect() - }; + let torrent_rows = table_manager_lock.rows(); let highlight_table_style = Style::default().on_black().bold().fg(self .ctx diff --git a/rm-main/src/ui/tabs/torrents/table_manager.rs b/rm-main/src/ui/tabs/torrents/table_manager.rs index 3e2ad2c..e3b596d 100644 --- a/rm-main/src/ui/tabs/torrents/table_manager.rs +++ b/rm-main/src/ui/tabs/torrents/table_manager.rs @@ -1,5 +1,5 @@ use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; -use ratatui::prelude::*; +use ratatui::{prelude::*, widgets::Row}; use std::sync::{Arc, Mutex}; use crate::{app, ui::components::table::GenericTable}; @@ -35,39 +35,22 @@ impl TableManager { } } - pub const fn header(&self) -> &Vec { - &self.header - } - - const fn default_widths() -> [Constraint; 6] { - [ - Constraint::Max(70), // Name - Constraint::Length(10), // Size - Constraint::Length(10), // Progress - Constraint::Length(10), // ETA - Constraint::Length(10), // Download - Constraint::Length(10), // Upload - ] - } - - pub fn filtered_torrents_rows<'a>( - &self, - torrents: &'a [RustmissionTorrent], - filter: &str, - ) -> Vec> { - let matcher = SkimMatcherV2::default(); - let mut rows = vec![]; - - let highlight_style = - Style::default().fg(self.ctx.config.general.accent_color.as_ratatui()); - - for torrent in torrents { - if let Some((_, indices)) = matcher.fuzzy_indices(&torrent.torrent_name, filter) { - rows.push(torrent.to_row_with_higlighted_indices(indices, highlight_style)) - } + pub fn rows(&self) -> Vec> { + if let Some(filter) = &*self.filter.lock().unwrap() { + let rows = self.filtered_torrents_rows(&self.table.items, filter); + self.table.overwrite_len(rows.len()); + rows + } else { + self.table + .items + .iter() + .map(RustmissionTorrent::to_row) + .collect() } + } - rows + pub const fn header(&self) -> &Vec { + &self.header } pub fn current_torrent(&mut self) -> Option<&mut RustmissionTorrent> { @@ -97,6 +80,37 @@ impl TableManager { self.widths = self.header_widths(&self.table.items); } + fn filtered_torrents_rows<'a>( + &self, + torrents: &'a [RustmissionTorrent], + filter: &str, + ) -> Vec> { + let matcher = SkimMatcherV2::default(); + let mut rows = vec![]; + + let highlight_style = + Style::default().fg(self.ctx.config.general.accent_color.as_ratatui()); + + for torrent in torrents { + if let Some((_, indices)) = matcher.fuzzy_indices(&torrent.torrent_name, filter) { + rows.push(torrent.to_row_with_higlighted_indices(indices, highlight_style)) + } + } + + rows + } + + const fn default_widths() -> [Constraint; 6] { + [ + Constraint::Max(70), // Name + Constraint::Length(10), // Size + Constraint::Length(10), // Progress + Constraint::Length(10), // ETA + Constraint::Length(10), // Download + Constraint::Length(10), // Upload + ] + } + fn header_widths(&self, rows: &[RustmissionTorrent]) -> [Constraint; 6] { if !self.ctx.config.general.auto_hide { return Self::default_widths();