Skip to content

Commit

Permalink
feat: sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 25, 2024
1 parent 382092c commit ea14d77
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 11 deletions.
6 changes: 6 additions & 0 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum GeneralAction {
GoToBeginning,
GoToEnd,
XdgOpen,
MoveToColumnLeft,
MoveToColumnRight,
}

impl UserAction for GeneralAction {
Expand All @@ -46,6 +48,8 @@ impl UserAction for GeneralAction {
GeneralAction::GoToBeginning => "scroll to the beginning",
GeneralAction::GoToEnd => "scroll to the end",
GeneralAction::XdgOpen => "open with xdg-open",
GeneralAction::MoveToColumnRight => "move to right column",
GeneralAction::MoveToColumnLeft => "move to left column",
}
}
}
Expand All @@ -71,6 +75,8 @@ impl From<GeneralAction> for Action {
GeneralAction::GoToBeginning => Action::Home,
GeneralAction::GoToEnd => Action::End,
GeneralAction::XdgOpen => Action::XdgOpen,
GeneralAction::MoveToColumnLeft => Action::MoveToColumnLeft,
GeneralAction::MoveToColumnRight => Action::MoveToColumnRight,
}
}
}
27 changes: 25 additions & 2 deletions rm-main/src/tui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::tui::components::{Component, ComponentAction};
use popups::details::DetailsPopup;
use popups::stats::StatisticsPopup;
use ratatui::prelude::*;
use ratatui::widgets::{Row, Table};
use ratatui::widgets::{Cell, Row, Table};
use rm_config::CONFIG;
use rm_shared::status_task::StatusTask;
use rustmission_torrent::RustmissionTorrent;
Expand Down Expand Up @@ -122,6 +122,14 @@ impl Component for TorrentsTab {
}
}
A::XdgOpen => self.xdg_open_current_torrent(),
A::MoveToColumnRight => {
self.table_manager.move_to_column_right();
self.ctx.send_action(Action::Render);
}
A::MoveToColumnLeft => {
self.table_manager.move_to_column_left();
self.ctx.send_action(Action::Render);
}
other => {
self.task_manager.handle_actions(other);
}
Expand Down Expand Up @@ -196,11 +204,26 @@ impl TorrentsTab {
.fg(CONFIG.general.accent_color);

let rows = self.table_manager.rows();

let mut headers = self
.table_manager
.headers()
.iter()
.map(|h| h.header_name())
.map(Cell::from)
.collect::<Vec<_>>();

if let Some(sort_header) = self.table_manager.sort_header {
headers[sort_header] = headers[sort_header]
.clone()
.style(Style::default().fg(CONFIG.general.accent_color));
}

let table_widget = {
let table =
Table::new(rows, &self.table_manager.widths).highlight_style(highlight_table_style);
if !CONFIG.general.headers_hide {
table.header(Row::new(self.table_manager.headers().iter().cloned()))
table.header(Row::new(headers))
} else {
table
}
Expand Down
103 changes: 94 additions & 9 deletions rm-main/src/tui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use ratatui::{prelude::*, widgets::Row};
use rm_config::CONFIG;
use rm_shared::header::Header;
use rm_shared::{action::Action, header::Header};
use std::collections::HashMap;

use crate::tui::components::GenericTable;
Expand All @@ -13,7 +13,8 @@ pub struct TableManager {
pub widths: Vec<Constraint>,
pub filter: Option<Filter>,
pub torrents_displaying_no: u16,
headers: Vec<&'static str>,
pub sort_header: Option<usize>,
pub sorting_is_being_selected: bool,
}

pub struct Filter {
Expand All @@ -26,17 +27,100 @@ impl TableManager {
pub fn new() -> Self {
let table = GenericTable::new(vec![]);
let widths = Self::default_widths(&CONFIG.torrents_tab.headers);
let mut headers = vec![];
for header in &CONFIG.torrents_tab.headers {
headers.push(header.header_name());
}

Self {
table,
widths,
filter: None,
torrents_displaying_no: 0,
headers,
sort_header: None,
sorting_is_being_selected: false,
}
}

pub fn move_to_column_left(&mut self) {
self.sorting_is_being_selected = true;
if let Some(selected) = self.sort_header {
self.sort_header = Some(selected.saturating_sub(1));
self.sort();
} else {
self.sort_header = Some(0);
self.sort();
}
}

pub fn move_to_column_right(&mut self) {
self.sorting_is_being_selected = true;
if let Some(selected) = self.sort_header {
let headers_count = self.headers().len();
if selected < headers_count {
self.sort_header = Some(selected + 1);
self.sort();
}
} else {
self.sort_header = Some(0);
self.sort();
}
}

pub fn sort(&mut self) {
if let Some(selected_header) = self.sort_header {
let sort_by = CONFIG.torrents_tab.headers[selected_header];
match sort_by {
Header::Id => todo!(),
Header::Name => self
.table
.items
.sort_unstable_by(|x, y| x.torrent_name.cmp(&y.torrent_name)),
Header::SizeWhenDone => self
.table
.items
.sort_unstable_by(|x, y| x.size_when_done.cmp(&y.size_when_done)),
Header::Progress => self
.table
.items
.sort_unstable_by(|x, y| x.progress.cmp(&y.progress)),
Header::Eta => self
.table
.items
.sort_unstable_by(|x, y| x.eta_secs.cmp(&y.eta_secs)),
Header::DownloadRate => self
.table
.items
.sort_unstable_by(|x, y| x.download_speed.cmp(&y.download_speed)),
Header::UploadRate => self
.table
.items
.sort_unstable_by(|x, y| x.upload_speed.cmp(&y.upload_speed)),
Header::DownloadDir => self
.table
.items
.sort_unstable_by(|x, y| x.download_dir.cmp(&y.download_dir)),
Header::Padding => (),
Header::UploadRatio => self
.table
.items
.sort_unstable_by(|x, y| x.upload_ratio.cmp(&y.upload_ratio)),
Header::UploadedEver => self
.table
.items
.sort_unstable_by(|x, y| x.uploaded_ever.cmp(&y.uploaded_ever)),
Header::ActivityDate => self
.table
.items
.sort_unstable_by(|x, y| x.activity_date.cmp(&y.activity_date)),
Header::AddedDate => self
.table
.items
.sort_unstable_by(|x, y| x.added_date.cmp(&y.added_date)),
Header::PeersConnected => self
.table
.items
.sort_unstable_by(|x, y| x.peers_connected.cmp(&y.peers_connected)),
Header::SmallStatus => (),
Header::Category => (),
Header::CategoryIcon => (),
}
}
}

Expand Down Expand Up @@ -73,8 +157,8 @@ impl TableManager {
}
}

pub const fn headers(&self) -> &Vec<&'static str> {
&self.headers
pub fn headers(&self) -> &Vec<Header> {
&CONFIG.torrents_tab.headers
}

pub fn current_torrent(&mut self) -> Option<&mut RustmissionTorrent> {
Expand All @@ -97,6 +181,7 @@ impl TableManager {
self.table.set_items(rows);
self.widths = self.header_widths(&self.table.items);
self.update_rows_number();
self.sort();
}

pub fn set_filter(&mut self, filter: String) {
Expand Down
2 changes: 2 additions & 0 deletions rm-shared/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Action {
ChangeTab(u8),
XdgOpen,
Input(KeyEvent),
MoveToColumnLeft,
MoveToColumnRight,
// Torrents Tab
ShowStats,
ShowFiles,
Expand Down

0 comments on commit ea14d77

Please sign in to comment.