Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: selected item indicator in torrent tab bottom stats #20

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 27 additions & 2 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,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<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() {
Expand All @@ -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()) {
aidanaden marked this conversation as resolved.
Show resolved Hide resolved
let current_idx = current + 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if borrowing like this is optimal ..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, it just increments an int or something

text = format!("{current_idx}/{total} | {text}")
}
aidanaden marked this conversation as resolved.
Show resolved Hide resolved

let paragraph = Paragraph::new(text).alignment(Alignment::Right);
f.render_widget(paragraph, rect);
}
Expand Down
13 changes: 7 additions & 6 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
aidanaden marked this conversation as resolved.
Show resolved Hide resolved
));

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

Self {
stats,
stats: bottom_stats,
aidanaden marked this conversation as resolved.
Show resolved Hide resolved
task_manager: TaskManager::new(table_manager.clone(), ctx.clone()),
table_manager,
popup_manager: PopupManager::new(),
Expand Down
Loading