Feat(CommandBackend): Implemented the basic structure of the command handling backend
This commit is contained in:
parent
a30229b763
commit
b0c09f9c65
|
@ -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(())
|
||||||
|
}
|
|
@ -1,11 +1,13 @@
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
|
|
||||||
use matrix_sdk::{config::SyncSettings, Client, LoopCtrl};
|
use matrix_sdk::{config::SyncSettings, Client, LoopCtrl};
|
||||||
use tokio::{sync::mpsc, time::Duration};
|
use tokio::{sync::mpsc, time::Duration};
|
||||||
|
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
use crate::app::command::{handle as command_handle, Command};
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{status::State, App},
|
app::{status::State, App},
|
||||||
ui,
|
ui,
|
||||||
|
@ -22,6 +24,7 @@ pub enum EventStatus {
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
input_event: Option<crossterm::event::Event>,
|
input_event: Option<crossterm::event::Event>,
|
||||||
matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>,
|
matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>,
|
||||||
|
command_event: Option<Command>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventBuilder {
|
pub struct EventBuilder {
|
||||||
|
@ -33,6 +36,7 @@ impl Default for Event {
|
||||||
Self {
|
Self {
|
||||||
input_event: None,
|
input_event: None,
|
||||||
matrix_event: None,
|
matrix_event: None,
|
||||||
|
command_event: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,12 +50,12 @@ impl Default for EventBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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.event.input_event = Some(input_event);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn matrix_event(
|
pub fn matrix_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
matrix_event: matrix_sdk::deserialized_responses::SyncResponse,
|
matrix_event: matrix_sdk::deserialized_responses::SyncResponse,
|
||||||
) -> &Self {
|
) -> &Self {
|
||||||
|
@ -59,20 +63,30 @@ impl EventBuilder {
|
||||||
self
|
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 {
|
Event {
|
||||||
input_event: self.event.input_event.clone(),
|
input_event: self.event.input_event.clone(),
|
||||||
matrix_event: self.event.matrix_event.clone(),
|
matrix_event: self.event.matrix_event.clone(),
|
||||||
|
command_event: self.event.command_event.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
pub async fn handle(&self, app: &mut App<'_>) -> Result<EventStatus> {
|
pub async fn handle(self, app: &mut App<'_>) -> Result<EventStatus> {
|
||||||
if self.matrix_event.is_some() {
|
if self.matrix_event.is_some() {
|
||||||
return self.handle_matrix(app).await;
|
return self.handle_matrix(app).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(cmd) = &self.command_event {
|
||||||
|
command_handle(cmd.clone()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
let status = match app.status.state() {
|
let status = match app.status.state() {
|
||||||
State::None => EventStatus::Ok,
|
State::None => EventStatus::Ok,
|
||||||
State::Main => self.handle_main(app).await?,
|
State::Main => self.handle_main(app).await?,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod command;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ 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, ui};
|
use crate::{accounts, ui};
|
||||||
|
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
|
@ -173,4 +175,12 @@ 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 = event::EventBuilder::default()
|
||||||
|
.command_event(command)
|
||||||
|
.build();
|
||||||
|
self.channel_tx.send(event).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue