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();