Skip to content

Commit

Permalink
impl reverse sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 25, 2024
1 parent 4f0a2ba commit 01edca8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
24 changes: 23 additions & 1 deletion rm-main/src/tui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl Component for TorrentsTab {
self.table_manager.move_to_column_right();
self.ctx.send_action(Action::Render);
}
A::Down | A::Up => {
self.table_manager.reverse_sort();
self.ctx.send_action(Action::Render);
}
A::Confirm => {
self.table_manager.apply_sort();
self.task_manager.default();
Expand Down Expand Up @@ -149,6 +153,7 @@ impl Component for TorrentsTab {
A::XdgOpen => self.xdg_open_current_torrent(),
A::MoveToColumnLeft | A::MoveToColumnRight => {
self.table_manager.enter_sorting_selection();
self.task_manager.sort();
self.ctx.send_action(Action::Render);
}
other => {
Expand Down Expand Up @@ -226,11 +231,28 @@ impl TorrentsTab {

let rows = self.table_manager.rows();

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

let sorted_header_name;
if let Some(sort_header) = self.table_manager.sort_header {
let icon = if self.table_manager.sort_reverse {
"󰒽"
} else {
"󰒼"
};

sorted_header_name = format!("{icon} {}", text_headers[sort_header]);
text_headers[sort_header] = sorted_header_name.as_str();
}

let mut headers = text_headers
.iter()
.cloned()
.map(Cell::from)
.collect::<Vec<_>>();

Expand Down
48 changes: 29 additions & 19 deletions rm-main/src/tui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct TableManager {
pub filter: Option<Filter>,
pub torrents_displaying_no: u16,
pub sort_header: Option<usize>,
pub sort_reverse: bool,
pub sorting_is_being_selected: bool,
}

Expand All @@ -34,13 +35,21 @@ impl TableManager {
filter: None,
torrents_displaying_no: 0,
sort_header: None,
sort_reverse: false,
sorting_is_being_selected: false,
}
}

pub fn enter_sorting_selection(&mut self) {
self.sorting_is_being_selected = true;
self.sort_header = Some(0);
if self.sort_header.is_none() {
self.sort_header = Some(0);
self.sort();
}
}

pub fn reverse_sort(&mut self) {
self.sort_reverse = !self.sort_reverse;
self.sort();
}

Expand Down Expand Up @@ -79,58 +88,56 @@ impl TableManager {
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::Name => self.table.items.sort_by(|x, y| {
x.torrent_name
.to_lowercase()
.cmp(&y.torrent_name.to_lowercase())
}),
Header::SizeWhenDone => self
.table
.items
.sort_unstable_by(|x, y| x.size_when_done.cmp(&y.size_when_done)),
.sort_by(|x, y| x.size_when_done.cmp(&y.size_when_done)),
Header::Progress => self.table.items.sort_unstable_by(|x, y| {
x.progress
.partial_cmp(&y.progress)
.unwrap_or(Ordering::Equal)
}),
Header::Eta => self
.table
.items
.sort_unstable_by(|x, y| x.eta_secs.cmp(&y.eta_secs)),
Header::Eta => self.table.items.sort_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)),
.sort_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)),
.sort_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)),
.sort_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)),
.sort_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)),
.sort_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)),
.sort_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)),
.sort_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)),
.sort_by(|x, y| x.peers_connected.cmp(&y.peers_connected)),
Header::SmallStatus => (),
Header::Category => self.table.items.sort_unstable_by(|x, y| {
Header::Category => self.table.items.sort_by(|x, y| {
x.category
.as_ref()
.and_then(|cat| {
Expand All @@ -147,6 +154,9 @@ impl TableManager {
}),
Header::CategoryIcon => (),
}
if self.sort_reverse {
self.table.items.reverse();
}
}
}

Expand Down

0 comments on commit 01edca8

Please sign in to comment.