Skip to content

Commit

Permalink
refactor: move getting rows to table manager
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jun 21, 2024
1 parent 55b21bb commit f5cb378
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
18 changes: 2 additions & 16 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
78 changes: 46 additions & 32 deletions rm-main/src/ui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -35,39 +35,22 @@ impl TableManager {
}
}

pub const fn header(&self) -> &Vec<String> {
&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<ratatui::widgets::Row<'a>> {
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<Row<'_>> {
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<String> {
&self.header
}

pub fn current_torrent(&mut self) -> Option<&mut RustmissionTorrent> {
Expand Down Expand Up @@ -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<Row<'a>> {
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();
Expand Down

0 comments on commit f5cb378

Please sign in to comment.