From ace611da27a81539246f1261b5bf054fb44b9ffb Mon Sep 17 00:00:00 2001 From: aidanaden Date: Fri, 21 Jun 2024 01:24:22 +0800 Subject: [PATCH 1/4] feat: selected item indicator in torrent tab --- rm-main/src/ui/components/table.rs | 2 +- rm-main/src/ui/tabs/torrents/bottom_stats.rs | 29 ++++++++++++++++++-- rm-main/src/ui/tabs/torrents/mod.rs | 13 +++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/rm-main/src/ui/components/table.rs b/rm-main/src/ui/components/table.rs index c394a7c..a9b03da 100644 --- a/rm-main/src/ui/components/table.rs +++ b/rm-main/src/ui/components/table.rs @@ -17,7 +17,7 @@ impl GenericTable { } } - fn get_len(&self) -> usize { + pub fn get_len(&self) -> usize { self.overwritten_len .borrow() .map_or(self.items.len(), |len| len) diff --git a/rm-main/src/ui/tabs/torrents/bottom_stats.rs b/rm-main/src/ui/tabs/torrents/bottom_stats.rs index d930ba1..5da81a3 100644 --- a/rm-main/src/ui/tabs/torrents/bottom_stats.rs +++ b/rm-main/src/ui/tabs/torrents/bottom_stats.rs @@ -1,4 +1,7 @@ -use std::sync::{Arc, Mutex}; +use std::{ + borrow::Borrow, + sync::{Arc, Mutex}, +}; use ratatui::{ layout::{Alignment, Rect}, @@ -9,13 +12,28 @@ use transmission_rpc::types::{FreeSpace, SessionStats}; use crate::{ui::components::Component, utils::bytes_to_human_format}; -#[derive(Default)] +use super::table_manager::TableManager; + pub(super) struct BottomStats { // TODO: get rid of the Option pub(super) stats: Arc>>, pub(super) free_space: Arc>>, + pub(super) table_manager: Arc>, } +impl BottomStats { + pub fn new( + stats: Arc>>, + free_space: Arc>>, + table_manager: Arc>, + ) -> Self { + Self { + stats, + free_space, + table_manager, + } + } +} impl Component for BottomStats { fn render(&mut self, f: &mut Frame, rect: Rect) { if let Some(stats) = &*self.stats.lock().unwrap() { @@ -29,6 +47,13 @@ impl Component for BottomStats { text = format!("󰋊 {free_space} | {text}") } + let table_manager = &*self.table_manager.lock().unwrap(); + let state = table_manager.table.borrow(); + if let (Some(current), total) = (state.state.borrow().selected(), state.get_len()) { + let current_idx = current + 1; + text = format!("{current_idx}/{total} | {text}") + } + let paragraph = Paragraph::new(text).alignment(Alignment::Right); f.render_widget(paragraph, rect); } diff --git a/rm-main/src/ui/tabs/torrents/mod.rs b/rm-main/src/ui/tabs/torrents/mod.rs index 013afd3..3134984 100644 --- a/rm-main/src/ui/tabs/torrents/mod.rs +++ b/rm-main/src/ui/tabs/torrents/mod.rs @@ -37,28 +37,29 @@ pub struct TorrentsTab { impl TorrentsTab { pub fn new(ctx: app::Ctx) -> Self { - let stats = BottomStats::default(); let table = GenericTable::new(vec![]); - 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()); tokio::spawn(transmission::fetchers::stats( ctx.clone(), - Arc::clone(&stats.stats), + Arc::clone(&bottom_stats.stats), )); tokio::spawn(transmission::fetchers::torrents( ctx.clone(), - Arc::clone(&table_manager), + Arc::clone(&bottom_stats.table_manager.clone()), )); tokio::spawn(transmission::fetchers::free_space( ctx.clone(), - Arc::clone(&stats.free_space), + Arc::clone(&bottom_stats.free_space), )); Self { - stats, + stats: bottom_stats, task_manager: TaskManager::new(table_manager.clone(), ctx.clone()), table_manager, popup_manager: PopupManager::new(), From 590e3df4cf5d4a1377a3d189619fd16abb242a5e Mon Sep 17 00:00:00 2001 From: aidanaden Date: Fri, 21 Jun 2024 02:22:45 +0800 Subject: [PATCH 2/4] fix: review changes --- rm-main/src/ui/tabs/torrents/bottom_stats.rs | 15 ++++++++------- rm-main/src/ui/tabs/torrents/mod.rs | 10 +++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rm-main/src/ui/tabs/torrents/bottom_stats.rs b/rm-main/src/ui/tabs/torrents/bottom_stats.rs index 5da81a3..a525a53 100644 --- a/rm-main/src/ui/tabs/torrents/bottom_stats.rs +++ b/rm-main/src/ui/tabs/torrents/bottom_stats.rs @@ -40,20 +40,21 @@ impl Component for BottomStats { let all = stats.torrent_count; let download = bytes_to_human_format(stats.download_speed); let upload = bytes_to_human_format(stats.upload_speed); + let mut text = format!(" {all} | ▼ {download} | ▲ {upload}"); + let table_manager = &*self.table_manager.lock().unwrap(); + let table = table_manager.table.borrow(); + if let Some(current) = table.state.borrow().selected() { + let current_idx = current + 1; + text = format!(" {current_idx}/{all} | ▼ {download} | ▲ {upload}"); + } + if let Some(free_space) = &*self.free_space.lock().unwrap() { let free_space = bytes_to_human_format(free_space.size_bytes); text = format!("󰋊 {free_space} | {text}") } - let table_manager = &*self.table_manager.lock().unwrap(); - let state = table_manager.table.borrow(); - if let (Some(current), total) = (state.state.borrow().selected(), state.get_len()) { - let current_idx = current + 1; - text = format!("{current_idx}/{total} | {text}") - } - let paragraph = Paragraph::new(text).alignment(Alignment::Right); f.render_widget(paragraph, rect); } diff --git a/rm-main/src/ui/tabs/torrents/mod.rs b/rm-main/src/ui/tabs/torrents/mod.rs index 3134984..105aff2 100644 --- a/rm-main/src/ui/tabs/torrents/mod.rs +++ b/rm-main/src/ui/tabs/torrents/mod.rs @@ -32,7 +32,7 @@ pub struct TorrentsTab { table_manager: Arc>, popup_manager: PopupManager, task_manager: TaskManager, - stats: BottomStats, + bottom_stats: BottomStats, } impl TorrentsTab { @@ -50,7 +50,7 @@ impl TorrentsTab { tokio::spawn(transmission::fetchers::torrents( ctx.clone(), - Arc::clone(&bottom_stats.table_manager.clone()), + Arc::clone(&bottom_stats.table_manager), )); tokio::spawn(transmission::fetchers::free_space( @@ -59,7 +59,7 @@ impl TorrentsTab { )); Self { - stats: bottom_stats, + bottom_stats, task_manager: TaskManager::new(table_manager.clone(), ctx.clone()), table_manager, popup_manager: PopupManager::new(), @@ -75,7 +75,7 @@ impl Component for TorrentsTab { self.render_table(f, torrents_list_rect); - self.stats.render(f, stats_rect); + self.bottom_stats.render(f, stats_rect); self.task_manager.render(f, stats_rect); @@ -159,7 +159,7 @@ impl TorrentsTab { } fn show_statistics_popup(&mut self) -> Option { - if let Some(stats) = &*self.stats.stats.lock().unwrap() { + if let Some(stats) = &*self.bottom_stats.stats.lock().unwrap() { let popup = StatisticsPopup::new(self.ctx.clone(), stats.clone()); self.popup_manager.show_popup(CurrentPopup::Stats(popup)); Some(Action::Render) From f165b53d96abb5398de021d1794741394329c9cd Mon Sep 17 00:00:00 2001 From: aidanaden Date: Fri, 21 Jun 2024 03:25:12 +0800 Subject: [PATCH 3/4] feat: move folder indicator to left --- rm-main/src/ui/tabs/torrents/bottom_stats.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rm-main/src/ui/tabs/torrents/bottom_stats.rs b/rm-main/src/ui/tabs/torrents/bottom_stats.rs index a525a53..a7491f8 100644 --- a/rm-main/src/ui/tabs/torrents/bottom_stats.rs +++ b/rm-main/src/ui/tabs/torrents/bottom_stats.rs @@ -41,18 +41,18 @@ impl Component for BottomStats { let download = bytes_to_human_format(stats.download_speed); let upload = bytes_to_human_format(stats.upload_speed); - let mut text = format!(" {all} | ▼ {download} | ▲ {upload}"); + let mut text = format!("▼ {download} | ▲ {upload}"); + + if let Some(free_space) = &*self.free_space.lock().unwrap() { + let free_space = bytes_to_human_format(free_space.size_bytes); + text = format!("󰋊 {free_space} | {text}") + } let table_manager = &*self.table_manager.lock().unwrap(); let table = table_manager.table.borrow(); if let Some(current) = table.state.borrow().selected() { let current_idx = current + 1; - text = format!(" {current_idx}/{all} | ▼ {download} | ▲ {upload}"); - } - - if let Some(free_space) = &*self.free_space.lock().unwrap() { - let free_space = bytes_to_human_format(free_space.size_bytes); - text = format!("󰋊 {free_space} | {text}") + text = format!(" {current_idx}/{all} | {text}"); } let paragraph = Paragraph::new(text).alignment(Alignment::Right); From 5450bc3d96af20fb65713fbe63f2c1c25a28a7ee Mon Sep 17 00:00:00 2001 From: aidanaden Date: Fri, 21 Jun 2024 03:27:54 +0800 Subject: [PATCH 4/4] fix: handle else --- rm-main/src/ui/tabs/torrents/bottom_stats.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rm-main/src/ui/tabs/torrents/bottom_stats.rs b/rm-main/src/ui/tabs/torrents/bottom_stats.rs index a7491f8..a452c7d 100644 --- a/rm-main/src/ui/tabs/torrents/bottom_stats.rs +++ b/rm-main/src/ui/tabs/torrents/bottom_stats.rs @@ -53,6 +53,8 @@ impl Component for BottomStats { if let Some(current) = table.state.borrow().selected() { let current_idx = current + 1; text = format!(" {current_idx}/{all} | {text}"); + } else { + text = format!(" {all} | {text}"); } let paragraph = Paragraph::new(text).alignment(Alignment::Right);