From c3f2b9113a3950ff2cd085967f9a5dff303a26de Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 30 Jan 2024 18:57:03 +0200 Subject: [PATCH] feat: add pause/start all torrents --- src/app/command.rs | 25 ++++++++++++++++++++++++- src/app/mod.rs | 2 +- src/app/torrent.rs | 11 +++-------- src/handler.rs | 14 ++++---------- src/ui/table.rs | 2 +- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/app/command.rs b/src/app/command.rs index b0b75e2..0028b2a 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -18,7 +18,7 @@ impl Torrents { pub async fn toggle_all(&mut self) { let torrents: Vec<(Id, TorrentAction)> = self - .torrents() + .torrents .iter() .map(|torrent| { ( @@ -39,6 +39,18 @@ impl Torrents { } } + pub async fn start_all(&mut self) { + self.action_all(TorrentAction::StartNow) + .await + .expect("Error starting all torrents"); + } + + pub async fn stop_all(&mut self) { + self.action_all(TorrentAction::Stop) + .await + .expect("Error stopping all torrents"); + } + pub fn move_dir(&mut self) -> Result<()> { todo!() } @@ -50,4 +62,15 @@ impl Torrents { pub fn rename(&mut self) -> Result<()> { todo!() } + + async fn action_all(&mut self, action: TorrentAction) -> transmission_rpc::types::Result<()> { + let ids = self + .torrents + .iter() + .filter_map(|torrent| torrent.id()) + .collect::>(); + + self.client.torrent_action(action, ids).await?; + Ok(()) + } } diff --git a/src/app/mod.rs b/src/app/mod.rs index 34ae935..201aab9 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -126,7 +126,7 @@ impl<'a> App<'a> { fn selected(&self) -> Option<&Torrent> { let idx = self.state.selected()?; - let torrent = self.torrents.torrents().get(idx)?; + let torrent = self.torrents.torrents.get(idx)?; Some(torrent) } } diff --git a/src/app/torrent.rs b/src/app/torrent.rs index 0ec0c4c..3845ffa 100644 --- a/src/app/torrent.rs +++ b/src/app/torrent.rs @@ -12,9 +12,9 @@ use url::Url; pub struct Torrents { /// Constructs a new instance of [`Torrents`]. pub client: TransClient, - torrents: Vec, - ids: Option>, - fields: Option>, + pub torrents: Vec, + pub ids: Option>, + pub fields: Option>, } impl Torrents { @@ -57,11 +57,6 @@ impl Torrents { .torrents; self } - - /// Returns [`Vec`] of [`Torrent`] as reference. - pub fn torrents(&self) -> &Vec { - &self.torrents - } } impl Debug for Torrents { diff --git a/src/handler.rs b/src/handler.rs index 8a61bb8..3f9c4ea 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -6,22 +6,16 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; pub async fn handle_key_events(key_event: KeyEvent, app: &mut App<'_>) -> Result<()> { match key_event.code { // Exit application on `ESC` or `q` - KeyCode::Esc | KeyCode::Char('q') => { - app.quit(); - } + KeyCode::Esc | KeyCode::Char('q') => app.quit(), + // Exit application on `Ctrl-C` KeyCode::Char('c') | KeyCode::Char('C') => { if key_event.modifiers == KeyModifiers::CONTROL { app.quit(); } } - KeyCode::Char('l') | KeyCode::Right => { - app.next_tab(); - } - KeyCode::Char('h') | KeyCode::Left => { - app.prev_tab(); - } - + KeyCode::Char('l') | KeyCode::Right => app.next_tab(), + KeyCode::Char('h') | KeyCode::Left => app.prev_tab(), KeyCode::Char('j') | KeyCode::Down => app.next(), KeyCode::Char('k') | KeyCode::Up => app.previous(), KeyCode::Char('1') => app.switch_tab(0), diff --git a/src/ui/table.rs b/src/ui/table.rs index 26d0cb3..34bfd73 100644 --- a/src/ui/table.rs +++ b/src/ui/table.rs @@ -8,7 +8,7 @@ use crate::app::{utils::Wrapper, App, Tab}; pub fn render_table<'a>(app: &mut App, tab: Tab) -> Table<'a> { let fields = tab.fields(); - let torrents = app.torrents.set_fields(None).torrents(); + let torrents = &app.torrents.set_fields(None).torrents; let rows: Vec> = torrents .iter()