diff --git a/src/app/command.rs b/src/app/command.rs new file mode 100644 index 0000000..edad6d4 --- /dev/null +++ b/src/app/command.rs @@ -0,0 +1,16 @@ +use anyhow::Result; +use cli_log::info; + +#[derive(Debug, Clone)] +pub enum Command { + // Closes the application + Exit, + + // sends a message to the current room + RoomMessageSend(String), +} + +pub async fn handle(command: Command) -> Result<()> { + info!("Handling command: {:?}", command); + Ok(()) +} diff --git a/src/app/event.rs b/src/app/event.rs index b7d14b6..f63c0da 100644 --- a/src/app/event.rs +++ b/src/app/event.rs @@ -1,11 +1,13 @@ use anyhow::{Error, Result}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; + use matrix_sdk::{config::SyncSettings, Client, LoopCtrl}; use tokio::{sync::mpsc, time::Duration}; use tokio_util::sync::CancellationToken; +use crate::app::command::{handle as command_handle, Command}; use crate::{ app::{status::State, App}, ui, @@ -22,6 +24,7 @@ pub enum EventStatus { pub struct Event { input_event: Option, matrix_event: Option, + command_event: Option, } pub struct EventBuilder { @@ -33,6 +36,7 @@ impl Default for Event { Self { input_event: None, matrix_event: None, + command_event: None, } } } @@ -46,12 +50,12 @@ impl Default for EventBuilder { } impl EventBuilder { - fn input_event(&mut self, input_event: crossterm::event::Event) -> &Self { + pub fn input_event(&mut self, input_event: crossterm::event::Event) -> &Self { self.event.input_event = Some(input_event); self } - fn matrix_event( + pub fn matrix_event( &mut self, matrix_event: matrix_sdk::deserialized_responses::SyncResponse, ) -> &Self { @@ -59,20 +63,30 @@ impl EventBuilder { self } - fn build(&self) -> Event { + pub fn command_event(&mut self, command: Command) -> &Self { + self.event.command_event = Some(command); + self + } + + pub fn build(&self) -> Event { Event { input_event: self.event.input_event.clone(), matrix_event: self.event.matrix_event.clone(), + command_event: self.event.command_event.clone(), } } } impl Event { - pub async fn handle(&self, app: &mut App<'_>) -> Result { + pub async fn handle(self, app: &mut App<'_>) -> Result { if self.matrix_event.is_some() { return self.handle_matrix(app).await; } + if let Some(cmd) = &self.command_event { + command_handle(cmd.clone()).await?; + } + let status = match app.status.state() { State::None => EventStatus::Ok, State::Main => self.handle_main(app).await?, diff --git a/src/app/mod.rs b/src/app/mod.rs index 4cb6ed6..6d89806 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,3 +1,4 @@ +pub mod command; pub mod event; pub mod status; @@ -11,6 +12,7 @@ use status::{State, Status}; use tokio::sync::mpsc; use tokio_util::sync::CancellationToken; +use crate::app::command::Command; use crate::{accounts, ui}; pub struct App<'a> { @@ -173,4 +175,12 @@ impl App<'_> { pub fn client(&self) -> Option<&Client> { self.accounts_manager.client() } + + pub async fn command_execute(&self, command: Command) -> Result<()> { + let event = event::EventBuilder::default() + .command_event(command) + .build(); + self.channel_tx.send(event).await?; + Ok(()) + } }