From cc095a16ca8057a5d5b68dc94e9468b7e5ec6bff Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Fri, 5 Jul 2024 11:42:49 +0200 Subject: [PATCH] fix headers when searching --- rm-config/src/main_config.rs | 2 +- .../ui/tabs/torrents/rustmission_torrent.rs | 56 ++++++++++--------- rm-main/src/ui/tabs/torrents/table_manager.rs | 6 +- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/rm-config/src/main_config.rs b/rm-config/src/main_config.rs index b2e46a8..5cf5c48 100644 --- a/rm-config/src/main_config.rs +++ b/rm-config/src/main_config.rs @@ -52,7 +52,7 @@ fn default_refresh() -> u64 { 5 } -#[derive(Serialize, Deserialize, Hash, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Hash, PartialEq, Eq, Clone, Copy)] pub enum Header { Name, SizeWhenDone, diff --git a/rm-main/src/ui/tabs/torrents/rustmission_torrent.rs b/rm-main/src/ui/tabs/torrents/rustmission_torrent.rs index 3c163cc..6922481 100644 --- a/rm-main/src/ui/tabs/torrents/rustmission_torrent.rs +++ b/rm-main/src/ui/tabs/torrents/rustmission_torrent.rs @@ -25,31 +25,32 @@ pub struct RustmissionTorrent { } impl RustmissionTorrent { - pub fn to_row(&self, headers: &Vec
) -> ratatui::widgets::Row { - let mut cells = vec![]; - for header in headers { - let cell = { - match header { - Header::Name => Line::from(self.torrent_name.as_str()), - Header::SizeWhenDone => Line::from(self.size_when_done.as_str()), - Header::Progress => Line::from(self.progress.as_str()), - Header::Eta => Line::from(self.eta_secs.as_str()), - Header::DownloadRate => Line::from(download_speed_format(&self.download_speed)), - Header::UploadRate => Line::from(upload_speed_format(&self.upload_speed)), - Header::DownloadDir => Line::from(self.download_dir.as_str()), - Header::Padding => Line::raw(""), - } - }; - cells.push(cell); + fn header_to_line(&self, header: Header) -> Line { + match header { + Header::Name => Line::from(self.torrent_name.as_str()), + Header::SizeWhenDone => Line::from(self.size_when_done.as_str()), + Header::Progress => Line::from(self.progress.as_str()), + Header::Eta => Line::from(self.eta_secs.as_str()), + Header::DownloadRate => Line::from(download_speed_format(&self.download_speed)), + Header::UploadRate => Line::from(upload_speed_format(&self.upload_speed)), + Header::DownloadDir => Line::from(self.download_dir.as_str()), + Header::Padding => Line::raw(""), } + } - Row::new(cells).style(self.style) + pub fn to_row(&self, headers: &Vec
) -> ratatui::widgets::Row { + headers + .iter() + .map(|header| self.header_to_line(*header)) + .collect::() + .style(self.style) } pub fn to_row_with_higlighted_indices( &self, highlighted_indices: Vec, highlight_style: Style, + headers: &Vec
, ) -> ratatui::widgets::Row { let mut torrent_name_line = Line::default(); @@ -61,16 +62,17 @@ impl RustmissionTorrent { } } - Row::new([ - Line::from(torrent_name_line), - Line::from(""), - Line::from(self.size_when_done.as_str()), - Line::from(self.progress.as_str()), - Line::from(self.eta_secs.as_str()), - Line::from(download_speed_format(&self.download_speed)), - Line::from(upload_speed_format(&self.upload_speed)), - Line::from(self.download_dir.as_str()), - ]) + let mut cells = vec![]; + + for header in headers { + if *header == Header::Name { + cells.push(Line::from(torrent_name_line.clone())) + } else { + cells.push(self.header_to_line(*header)) + } + } + + Row::new(cells) } pub const fn status(&self) -> TorrentStatus { diff --git a/rm-main/src/ui/tabs/torrents/table_manager.rs b/rm-main/src/ui/tabs/torrents/table_manager.rs index eca3aee..48df59f 100644 --- a/rm-main/src/ui/tabs/torrents/table_manager.rs +++ b/rm-main/src/ui/tabs/torrents/table_manager.rs @@ -94,7 +94,11 @@ impl TableManager { 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.push(torrent.to_row_with_higlighted_indices( + indices, + highlight_style, + &self.ctx.config.torrents_tab.headers, + )) } }