Skip to content

Commit

Permalink
feat: sorting (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski authored Aug 26, 2024
1 parent 382092c commit 7690ce7
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 77 deletions.
8 changes: 7 additions & 1 deletion rm-config/defaults/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ free_space_refresh = 10
# SmallStatus, Category, CategoryIcon
headers = ["Name", "SizeWhenDone", "Progress", "Eta", "DownloadRate", "UploadRate"]

# Default header to sort by:
default_sort = "AddedDate"
# Reverse the default sort?
default_sort_reverse = true

# Whether to insert category icon into name as declared in categories.toml.
# An alternative to inserting category's icon into torrent's name is adding a
# CategoryIcon header into your headers.
Expand Down Expand Up @@ -68,4 +73,5 @@ category_icon_insert_into_name = true
# provider_disabled = "⛔" # "󰪎"
# provider_category_general = "[G]" # ""
# provider_category_anime = "[A]" # "󰎁"

# sort_ascending = "↓" # "󰒼"
# sort_descending = "↑" # "󰒽""
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,
}
}
}
14 changes: 14 additions & 0 deletions rm-config/src/main_config/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct Icons {
pub provider_category_general: String,
#[serde(default = "default_provider_category_anime")]
pub provider_category_anime: String,
#[serde(default = "default_sort_ascending")]
pub sort_ascending: String,
#[serde(default = "default_sort_descending")]
pub sort_descending: String,
}

impl Default for Icons {
Expand Down Expand Up @@ -73,6 +77,8 @@ impl Default for Icons {
provider_disabled: default_provider_disabled(),
provider_category_general: default_provider_category_general(),
provider_category_anime: default_provider_category_anime(),
sort_ascending: default_sort_ascending(),
sort_descending: default_sort_descending(),
}
}
}
Expand Down Expand Up @@ -163,3 +169,11 @@ fn default_provider_category_general() -> String {
fn default_provider_category_anime() -> String {
"󰎁".into()
}

fn default_sort_ascending() -> String {
"󰒼".into()
}

fn default_sort_descending() -> String {
"󰒽".into()
}
10 changes: 10 additions & 0 deletions rm-config/src/main_config/torrents_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use serde::Deserialize;
pub struct TorrentsTab {
#[serde(default = "default_headers")]
pub headers: Vec<Header>,
#[serde(default = "default_sort")]
pub default_sort: Header,
#[serde(default = "default_true")]
pub default_sort_reverse: bool,
#[serde(default = "default_true")]
pub category_icon_insert_into_name: bool,
}
Expand All @@ -13,6 +17,10 @@ fn default_true() -> bool {
true
}

fn default_sort() -> Header {
Header::AddedDate
}

fn default_headers() -> Vec<Header> {
vec![
Header::Name,
Expand All @@ -28,6 +36,8 @@ impl Default for TorrentsTab {
fn default() -> Self {
Self {
headers: default_headers(),
default_sort: default_sort(),
default_sort_reverse: default_true(),
category_icon_insert_into_name: default_true(),
}
}
Expand Down
72 changes: 70 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 @@ -76,6 +76,35 @@ impl Component for TorrentsTab {
return ComponentAction::Nothing;
}

if self.table_manager.sorting_is_being_selected {
match action {
A::Close => {
self.table_manager.leave_sorting();
self.task_manager.default();
self.ctx.send_action(Action::Render);
}
A::MoveToColumnLeft => {
self.table_manager.move_to_column_left();
self.ctx.send_action(Action::Render);
}
A::MoveToColumnRight => {
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();
self.ctx.send_action(Action::Render);
}
_ => (),
}
return ComponentAction::Nothing;
}

if action.is_quit() {
self.ctx.send_action(Action::HardQuit);
}
Expand Down Expand Up @@ -122,6 +151,11 @@ 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 => {
self.task_manager.handle_actions(other);
}
Expand Down Expand Up @@ -196,11 +230,45 @@ impl TorrentsTab {
.fg(CONFIG.general.accent_color);

let rows = self.table_manager.rows();

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 {
&CONFIG.icons.sort_descending
} else {
&CONFIG.icons.sort_ascending
};

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<_>>();

if let Some(sort_header) = self.table_manager.sort_header {
if self.table_manager.sorting_is_being_selected {
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
10 changes: 5 additions & 5 deletions rm-main/src/tui/tabs/torrents/popups/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::tui::{
app,
components::{keybinding_style, popup_close_button_highlight, Component, ComponentAction},
main_window::centered_rect,
tabs::torrents::rustmission_torrent::RustmissionTorrent,
tabs::torrents::rustmission_torrent::{CategoryType, RustmissionTorrent},
};

pub struct DetailsPopup {
Expand Down Expand Up @@ -140,12 +140,12 @@ impl Component for DetailsPopup {
lines.push(Line::from(format!("Error: {error}")).red());
}

if let Some(category) = &self.torrent.categories.first() {
if let Some(category) = &self.torrent.category {
let mut category_line = Line::from("Category: ");
let mut category_span = Span::raw(category.as_str());
let mut category_span = Span::raw(category.name());

if let Some(config_category) = CONFIG.categories.map.get(*category) {
category_span = category_span.set_style(Style::default().fg(config_category.color))
if let CategoryType::Config(category) = category {
category_span = category_span.set_style(Style::default().fg(category.color))
}

category_line.push_span(category_span);
Expand Down
Loading

0 comments on commit 7690ce7

Please sign in to comment.