forked from trinitrix/core
1
0
Fork 0

Merge: command-backend -> master

This commit is contained in:
antifallobst 2023-07-13 18:41:08 +02:00
commit 33948164c4
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
7 changed files with 48 additions and 5 deletions

8
src/app/command.rs Normal file
View File

@ -0,0 +1,8 @@
#[derive(Debug, Clone)]
pub enum Command {
// Closes the application
Exit,
// sends a message to the current room
RoomMessageSend(String),
}

View File

@ -0,0 +1,8 @@
use crate::app::{command::Command, events::event_types::EventStatus, App};
use anyhow::Result;
use cli_log::info;
pub async fn handle(app: &mut App<'_>, command: &Command) -> Result<EventStatus> {
info!("Handling command: {:#?}", command);
Ok(EventStatus::Ok)
}

View File

@ -27,6 +27,8 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
modifiers: KeyModifiers::CONTROL, modifiers: KeyModifiers::CONTROL,
.. ..
}) => { }) => {
app.command_execute(crate::app::command::Command::Exit)
.await?;
app.ui.cli_enable(); app.ui.cli_enable();
} }
input => match app.ui.input_position() { input => match app.ui.input_position() {

View File

@ -1,3 +1,4 @@
pub mod command;
pub mod main;
pub mod matrix; pub mod matrix;
pub mod setup; pub mod setup;
pub mod main;

View File

@ -3,9 +3,9 @@ mod handlers;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use crossterm::event::Event as CrosstermEvent; use crossterm::event::Event as CrosstermEvent;
use crate::app::{status::State, App}; use crate::app::{command::Command, status::State, App};
use self::handlers::{main, matrix, setup}; use self::handlers::{command, main, matrix, setup};
use super::EventStatus; use super::EventStatus;
@ -13,6 +13,7 @@ use super::EventStatus;
pub struct Event { pub struct Event {
pub(super) input_event: Option<CrosstermEvent>, pub(super) input_event: Option<CrosstermEvent>,
pub(super) matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>, pub(super) matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>,
pub(super) command_event: Option<Command>,
} }
impl Event { impl Event {
@ -23,6 +24,12 @@ impl Event {
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", matrix_event)); .with_context(|| format!("Failed to handle matrix event: `{:#?}`", matrix_event));
} }
if let Some(command_event) = &self.command_event {
return command::handle(app, command_event).await.with_context(|| {
format!("Failed to handle command event: `{:#?}`", command_event)
});
}
if let Some(input_event) = &self.input_event { if let Some(input_event) = &self.input_event {
let status = match app.status.state() { let status = match app.status.state() {
State::None => EventStatus::Ok, State::None => EventStatus::Ok,

View File

@ -9,6 +9,7 @@ impl Default for Event {
Self { Self {
input_event: None, input_event: None,
matrix_event: None, matrix_event: None,
command_event: None,
} }
} }
} }
@ -35,10 +36,16 @@ impl EventBuilder {
self self
} }
pub fn command_event(&mut self, command_event: crate::app::command::Command) -> &Self {
self.event.command_event = Some(command_event);
self
}
pub fn build(&self) -> Event { pub fn build(&self) -> Event {
Event { Event {
input_event: self.event.input_event.to_owned(), input_event: self.event.input_event.to_owned(),
matrix_event: self.event.matrix_event.to_owned(), matrix_event: self.event.matrix_event.to_owned(),
command_event: self.event.command_event.to_owned(),
} }
} }
} }

View File

@ -1,3 +1,4 @@
pub mod command;
pub mod command_interface; pub mod command_interface;
pub mod events; pub mod events;
pub mod status; pub mod status;
@ -13,9 +14,10 @@ use status::{State, Status};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::app::command::Command;
use crate::{accounts, app::command_interface::generate_ci_functions, ui}; use crate::{accounts, app::command_interface::generate_ci_functions, ui};
use self::events::event_types; use self::events::event_types::{self, EventBuilder};
pub struct App<'ui> { pub struct App<'ui> {
ui: ui::UI<'ui>, ui: ui::UI<'ui>,
@ -130,7 +132,9 @@ impl App<'_> {
match event.handle(self).await? { match event.handle(self).await? {
event_types::EventStatus::Ok => (), event_types::EventStatus::Ok => (),
event_types::EventStatus::Finished => return Ok(()), event_types::EventStatus::Finished => return Ok(()),
event_types::EventStatus::Terminate => return Err(Error::msg("Terminated by user")), event_types::EventStatus::Terminate => {
return Err(Error::msg("Terminated by user"))
}
} }
} }
} }
@ -201,4 +205,10 @@ impl App<'_> {
pub fn client(&self) -> Option<&Client> { pub fn client(&self) -> Option<&Client> {
self.accounts_manager.client() 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(())
}
} }