forked from trinitrix/core
1
0
Fork 0

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
This commit is contained in:
antifallobst 2023-07-13 23:40:17 +02:00
parent 529b869e80
commit dd3c765ea2
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 23 additions and 13 deletions

View File

@ -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<Event>, command: Command) -> Result<()> {
let event = EventBuilder::default().command_event(command).build();
channel.send(event).await?;
Ok(())
}

View File

@ -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<E
CrosstermEvent::Key(KeyEvent {
code: KeyCode::Esc, ..
}) => {
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<E
modifiers: KeyModifiers::ALT,
..
}) => {
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();
}

View File

@ -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<Event> {
&self.channel_tx
}
}