Skip to content

Commit

Permalink
fix headers when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jul 5, 2024
1 parent 8a31b2f commit cc095a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion rm-config/src/main_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 29 additions & 27 deletions rm-main/src/ui/tabs/torrents/rustmission_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,32 @@ pub struct RustmissionTorrent {
}

impl RustmissionTorrent {
pub fn to_row(&self, headers: &Vec<Header>) -> 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<Header>) -> ratatui::widgets::Row {
headers
.iter()
.map(|header| self.header_to_line(*header))
.collect::<Row>()
.style(self.style)
}

pub fn to_row_with_higlighted_indices(
&self,
highlighted_indices: Vec<usize>,
highlight_style: Style,
headers: &Vec<Header>,
) -> ratatui::widgets::Row {
let mut torrent_name_line = Line::default();

Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion rm-main/src/ui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
}

Expand Down

0 comments on commit cc095a1

Please sign in to comment.