Skip to content

Commit

Permalink
store category info in rustmissiontorrent
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Aug 25, 2024
1 parent c0ccadc commit bfc23a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
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
77 changes: 46 additions & 31 deletions rm-main/src/tui/tabs/torrents/rustmission_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::{
text::{Line, Span},
widgets::{Cell, Row},
};
use rm_config::CONFIG;
use rm_config::{categories::Category, CONFIG};
use rm_shared::{
header::Header,
utils::{bytes_to_human_format, seconds_to_human_format},
Expand All @@ -28,10 +28,25 @@ pub struct RustmissionTorrent {
pub activity_date: NaiveDateTime,
pub added_date: NaiveDateTime,
pub peers_connected: i64,
pub categories: Vec<String>,
pub category: Option<CategoryType>,
pub error: Option<String>,
}

#[derive(Clone)]
pub enum CategoryType {
Plain(String),
Config(Category),
}

impl CategoryType {
pub fn name(&self) -> &str {
match self {
CategoryType::Plain(name) => name,
CategoryType::Config(config) => &config.name,
}
}
}

impl RustmissionTorrent {
pub fn to_row(&self, headers: &[Header]) -> ratatui::widgets::Row {
headers
Expand Down Expand Up @@ -159,11 +174,7 @@ impl RustmissionTorrent {
}

fn category_icon_span(&self) -> Span {
if let Some(category) = self
.categories
.first()
.and_then(|category| CONFIG.categories.map.get(category))
{
if let Some(CategoryType::Config(category)) = &self.category {
Span::styled(
format!("{} ", category.icon),
Style::default().fg(category.color),
Expand All @@ -175,17 +186,15 @@ impl RustmissionTorrent {

fn torrent_name_with_category_icon(&self) -> Line<'_> {
let mut line = Line::default();
if let Some(category) = self
.categories
.first()
.and_then(|category| CONFIG.categories.map.get(category))
{

if let Some(CategoryType::Config(category)) = &self.category {
line.push_span(Span::styled(
category.icon.as_str(),
Style::default().fg(category.color),
));
line.push_span(Span::raw(" "));
}

line.push_span(self.torrent_name.as_str());
line
}
Expand Down Expand Up @@ -238,26 +247,25 @@ impl RustmissionTorrent {
}
}
}
Header::Category => match self.categories.first() {
Some(category) => {
if let Some(config_category) = CONFIG.categories.map.get(category) {
Cell::from(category.as_str()).fg(config_category.color)
} else {
Cell::from(category.as_str())
Header::Category => {
if let Some(category) = &self.category {
match category {
CategoryType::Plain(name) => Cell::from(name.as_str()),
CategoryType::Config(category) => {
Cell::from(category.name.as_str()).fg(category.color)
}
}
} else {
Cell::default()
}
None => Cell::default(),
},
Header::CategoryIcon => match self.categories.first() {
Some(category) => {
if let Some(config_category) = CONFIG.categories.map.get(category) {
Cell::from(config_category.icon.as_str()).fg(config_category.color)
} else {
Cell::default()
}
}
Header::CategoryIcon => {
if let Some(CategoryType::Config(category)) = &self.category {
Cell::from(category.icon.as_str()).fg(category.color)
} else {
Cell::default()
}
None => Cell::default(),
},
}
}
}

Expand Down Expand Up @@ -343,7 +351,14 @@ impl From<Torrent> for RustmissionTorrent {
}
};

let categories = t.labels.unwrap();
let category = if let Some(category) = t.labels.unwrap().first() {
match CONFIG.categories.map.get(category) {
Some(category) => Some(CategoryType::Config(category.clone())),
None => Some(CategoryType::Plain(category.to_string())),
}
} else {
None
};

Self {
torrent_name,
Expand All @@ -361,7 +376,7 @@ impl From<Torrent> for RustmissionTorrent {
activity_date,
added_date,
peers_connected,
categories,
category,
error,
}
}
Expand Down
16 changes: 15 additions & 1 deletion rm-main/src/tui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,21 @@ impl TableManager {
.items
.sort_unstable_by(|x, y| x.peers_connected.cmp(&y.peers_connected)),
Header::SmallStatus => (),
Header::Category => (),
Header::Category => self.table.items.sort_unstable_by(|x, y| {
x.category
.as_ref()
.and_then(|cat| {
Some(
cat.name().cmp(
y.category
.as_ref()
.and_then(|cat| Some(cat.name()))
.unwrap_or_default(),
),
)
})
.unwrap_or(Ordering::Less)
}),
Header::CategoryIcon => (),
}
}
Expand Down

0 comments on commit bfc23a1

Please sign in to comment.