From dd3c765ea2a529c8d8b877a47bac3c2bf5a4121b Mon Sep 17 00:00:00 2001 From: antifallobst Date: Thu, 13 Jul 2023 23:40:17 +0200 Subject: [PATCH] Refactor(Commands): Moved high level command execution function out of app struct. This refactor was done to prevent borrow checker issues, with the lua bindings --- src/app/command.rs | 11 +++++++++++ .../events/event_types/event/handlers/main.rs | 17 +++++++++-------- src/app/mod.rs | 8 +++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/app/command.rs b/src/app/command.rs index 872c853..47b372a 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -1,3 +1,8 @@ +use anyhow::Result; +use tokio::sync::mpsc; + +use super::events::event_types::{Event, EventBuilder}; + #[derive(Debug, Clone)] pub enum Command { // Closes the application @@ -12,3 +17,9 @@ pub enum Command { // sends a message to the current room RoomMessageSend(String), } + +pub async fn execute(channel: &mpsc::Sender, command: Command) -> Result<()> { + let event = EventBuilder::default().command_event(command).build(); + channel.send(event).await?; + Ok(()) +} diff --git a/src/app/events/event_types/event/handlers/main.rs b/src/app/events/event_types/event/handlers/main.rs index fe43bf5..8e3bdd1 100644 --- a/src/app/events/event_types/event/handlers/main.rs +++ b/src/app/events/event_types/event/handlers/main.rs @@ -2,7 +2,7 @@ use anyhow::Result; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; use crate::{ - app::{command::Command, events::event_types::EventStatus, App}, + app::{command, command::Command, events::event_types::EventStatus, App}, ui, }; @@ -11,25 +11,25 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.command_execute(Command::Exit).await?; + command::execute(app.channel_tx(), Command::Exit).await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::Tab, .. }) => { - app.command_execute(Command::CyclePlanes).await?; + command::execute(app.channel_tx(), Command::CyclePlanes).await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::BackTab, .. }) => { - app.command_execute(Command::CyclePlanesRev).await?; + command::execute(app.channel_tx(), Command::CyclePlanesRev).await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, .. }) => { - app.command_execute(Command::CommandLineShow).await?; + command::execute(app.channel_tx(), Command::CommandLineShow).await?; } input => match app.ui.input_position() { ui::MainInputPosition::MessageCompose => { @@ -39,9 +39,10 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - app.command_execute(Command::RoomMessageSend( - app.ui.message_compose.lines().join("\n"), - )) + command::execute( + app.channel_tx(), + Command::RoomMessageSend(app.ui.message_compose.lines().join("\n")), + ) .await?; app.ui.message_compose_clear(); } diff --git a/src/app/mod.rs b/src/app/mod.rs index 4c48437..9ae1ef6 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -17,7 +17,7 @@ use tokio_util::sync::CancellationToken; use crate::app::command::Command; use crate::{accounts, app::command_interface::generate_ci_functions, ui}; -use self::events::event_types::{self, EventBuilder}; +use self::events::event_types::{self, Event, EventBuilder}; pub struct App<'ui> { ui: ui::UI<'ui>, @@ -206,9 +206,7 @@ impl App<'_> { self.accounts_manager.client() } - pub async fn command_execute(&self, command: Command) -> Result<()> { - let event = EventBuilder::default().command_event(command).build(); - self.channel_tx.send(event).await?; - Ok(()) + pub fn channel_tx(&self) -> &mpsc::Sender { + &self.channel_tx } }