diff --git a/src/app/action.rs b/src/app/action.rs new file mode 100644 index 0000000..bd35285 --- /dev/null +++ b/src/app/action.rs @@ -0,0 +1,17 @@ +#[derive(Debug, Clone, PartialEq)] +pub enum Action { + Quit, + NextTab, + PrevTab, + NextTorrent, + PrevTorrent, + SwitchTab(u8), + TogglePopup, + ToggleTorrent, + ToggleAll, + PauseAll, + StartAll, + Move, + Delete(bool), + Rename, +} diff --git a/src/app/command.rs b/src/app/command.rs index 6058df2..4022258 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -1,6 +1,5 @@ use std::path::Path; -use anyhow::Result; use transmission_rpc::types::{Id, Torrent, TorrentAction, TorrentStatus}; use super::Torrents; diff --git a/src/app/mod.rs b/src/app/mod.rs index 201aab9..b7cffcd 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -4,6 +4,7 @@ pub mod utils; use ratatui::widgets::TableState; use transmission_rpc::types::Torrent; +pub mod action; mod command; pub use self::{tab::Tab, torrent::Torrents}; @@ -124,6 +125,15 @@ impl<'a> App<'a> { self.close_popup(); } + pub async fn delete(&mut self, delete_local_data: bool) -> transmission_rpc::types::Result<()> { + let torrent = self.selected().expect("Torrent not found"); + self.torrents + .delete(&torrent.clone(), delete_local_data) + .await?; + self.close_popup(); + Ok(()) + } + fn selected(&self) -> Option<&Torrent> { let idx = self.state.selected()?; let torrent = self.torrents.torrents.get(idx)?; diff --git a/src/handler.rs b/src/handler.rs index 3f9c4ea..4adacc4 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,37 +1,49 @@ -use crate::app::App; -use anyhow::Result; +use crate::app::{action::Action, App}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -/// Handles the key events and updates the state of [`App`]. -pub async fn handle_key_events(key_event: KeyEvent, app: &mut App<'_>) -> Result<()> { +/// Handles the key events of [`App`]. +pub fn get_action(key_event: KeyEvent) -> Option { match key_event.code { // Exit application on `ESC` or `q` - KeyCode::Esc | KeyCode::Char('q') => app.quit(), + KeyCode::Esc | KeyCode::Char('q') => Some(Action::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('j') | KeyCode::Down => app.next(), - KeyCode::Char('k') | KeyCode::Up => app.previous(), - KeyCode::Char('1') => app.switch_tab(0), - KeyCode::Char('2') => app.switch_tab(1), - KeyCode::Char('3') => app.switch_tab(2), - KeyCode::Char('4') => app.switch_tab(3), - KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => { - app.toggle_popup(); - app.toggle_torrent().await; - } - KeyCode::Char('a') => { - app.toggle_popup(); - app.torrents.toggle_all().await; - } + KeyCode::Char('c') | KeyCode::Char('C') => match key_event.modifiers { + KeyModifiers::CONTROL => Some(Action::Quit), + _ => None, + }, + KeyCode::Char('l') | KeyCode::Right => Some(Action::NextTab), + KeyCode::Char('h') | KeyCode::Left => Some(Action::PrevTab), + KeyCode::Char('j') | KeyCode::Down => Some(Action::NextTorrent), + KeyCode::Char('k') | KeyCode::Up => Some(Action::PrevTorrent), + KeyCode::Char('1') => Some(Action::SwitchTab(0)), + KeyCode::Char('2') => Some(Action::SwitchTab(1)), + KeyCode::Char('3') => Some(Action::SwitchTab(2)), + KeyCode::Char('4') => Some(Action::SwitchTab(3)), + KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => Some(Action::ToggleTorrent), + KeyCode::Char('a') => Some(Action::ToggleAll), // Other handlers you could add here. - _ => (), + _ => None, + } +} + +/// Handles the updates of [`App`]. +pub async fn update(app: &mut App<'_>, action: Action) -> transmission_rpc::types::Result<()> { + match action { + Action::Quit => app.quit(), + Action::NextTab => app.next_tab(), + Action::PrevTab => app.prev_tab(), + Action::NextTorrent => app.next(), + Action::PrevTorrent => app.previous(), + Action::SwitchTab(x) => app.switch_tab(x as usize), + Action::TogglePopup => app.toggle_popup(), + Action::ToggleTorrent => app.toggle_torrent().await, + Action::ToggleAll => app.torrents.toggle_all().await, + Action::PauseAll => app.torrents.stop_all().await, + Action::StartAll => app.torrents.start_all().await, + Action::Move => unimplemented!(), + Action::Delete(x) => app.delete(x).await?, + Action::Rename => unimplemented!(), } Ok(()) } diff --git a/src/main.rs b/src/main.rs index 8c3de76..f4e7000 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use ratatui::Terminal; use std::io; use traxor::app::App; use traxor::event::{Event, EventHandler}; -use traxor::handler::handle_key_events; +use traxor::handler::{get_action, update}; use traxor::tui::Tui; #[tokio::main] @@ -26,7 +26,12 @@ async fn main() -> Result<()> { // Handle events. match tui.events.next()? { Event::Tick => app.tick().await, - Event::Key(key_event) => handle_key_events(key_event, &mut app).await?, + // Event::Key(key_event) => handle_key_events(key_event, &mut app).await?, + Event::Key(key_event) => { + if let Some(action) = get_action(key_event) { + update(&mut app, action).await.unwrap(); + } + } Event::Mouse(_) => {} Event::Resize(_, _) => {} }