Skip to content

Commit

Permalink
feat: selected item indicator in bottom stats (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanaden authored Jun 20, 2024
1 parent d526f82 commit 24b416d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rm-main/src/ui/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<T: Clone> GenericTable<T> {
}
}

fn get_len(&self) -> usize {
pub fn get_len(&self) -> usize {
self.overwritten_len
.borrow()
.map_or(self.items.len(), |len| len)
Expand Down
34 changes: 31 additions & 3 deletions rm-main/src/ui/tabs/torrents/bottom_stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex};
use std::{
borrow::Borrow,
sync::{Arc, Mutex},
};

use ratatui::{
layout::{Alignment, Rect},
Expand All @@ -9,26 +12,51 @@ 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<Mutex<Option<SessionStats>>>,
pub(super) free_space: Arc<Mutex<Option<FreeSpace>>>,
pub(super) table_manager: Arc<Mutex<TableManager>>,
}

impl BottomStats {
pub fn new(
stats: Arc<Mutex<Option<SessionStats>>>,
free_space: Arc<Mutex<Option<FreeSpace>>>,
table_manager: Arc<Mutex<TableManager>>,
) -> 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() {
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 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} | {text}");
} else {
text = format!(" {all} | {text}");
}

let paragraph = Paragraph::new(text).alignment(Alignment::Right);
f.render_widget(paragraph, rect);
}
Expand Down
19 changes: 10 additions & 9 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,34 @@ pub struct TorrentsTab {
table_manager: Arc<Mutex<TableManager>>,
popup_manager: PopupManager,
task_manager: TaskManager,
stats: BottomStats,
bottom_stats: BottomStats,
}

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),
));

tokio::spawn(transmission::fetchers::free_space(
ctx.clone(),
Arc::clone(&stats.free_space),
Arc::clone(&bottom_stats.free_space),
));

Self {
stats,
bottom_stats,
task_manager: TaskManager::new(table_manager.clone(), ctx.clone()),
table_manager,
popup_manager: PopupManager::new(),
Expand All @@ -74,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);

Expand Down Expand Up @@ -158,7 +159,7 @@ impl TorrentsTab {
}

fn show_statistics_popup(&mut self) -> Option<Action> {
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)
Expand Down

0 comments on commit 24b416d

Please sign in to comment.