From 14333944dc3d58de2ef517d5dad7883454b293ba Mon Sep 17 00:00:00 2001 From: Soispha Date: Tue, 18 Jul 2023 08:11:21 +0200 Subject: [PATCH] Fix(handles): Add command handling over the internal event stream --- .../events/event_types/event/handlers/command.rs | 6 +++++- .../events/event_types/event/handlers/main.rs | 16 +++++----------- src/app/events/event_types/event/mod.rs | 14 ++++++++++++-- src/app/mod.rs | 15 --------------- src/ui/central/mod.rs | 14 ++++++++++++-- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/app/events/event_types/event/handlers/command.rs b/src/app/events/event_types/event/handlers/command.rs index 364ad23..78a58ac 100644 --- a/src/app/events/event_types/event/handlers/command.rs +++ b/src/app/events/event_types/event/handlers/command.rs @@ -1,4 +1,4 @@ -use crate::app::{events::event_types::EventStatus, App, command_interface::Command}; +use crate::app::{command_interface::Command, events::event_types::EventStatus, App}; use anyhow::Result; use cli_log::info; @@ -32,5 +32,9 @@ pub async fn handle(app: &mut App<'_>, command: &Command) -> Result } EventStatus::Ok } + Command::Greet(name) => { + info!("Greated {}", name); + EventStatus::Ok + } }) } diff --git a/src/app/events/event_types/event/handlers/main.rs b/src/app/events/event_types/event/handlers/main.rs index 8e65604..4e87c40 100644 --- a/src/app/events/event_types/event/handlers/main.rs +++ b/src/app/events/event_types/event/handlers/main.rs @@ -155,7 +155,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - let cli_event = app.ui + let ci_event = app.ui .cli .as_mut() .expect("This is already checked") @@ -165,16 +165,10 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { app.ui diff --git a/src/app/events/event_types/event/mod.rs b/src/app/events/event_types/event/mod.rs index b37d452..1e48bc6 100644 --- a/src/app/events/event_types/event/mod.rs +++ b/src/app/events/event_types/event/mod.rs @@ -3,9 +3,9 @@ mod handlers; use anyhow::{Context, Result}; use crossterm::event::Event as CrosstermEvent; -use crate::app::{status::State, App, command_interface::Command}; +use crate::app::{command_interface::Command, status::State, App}; -use self::handlers::{command, main, matrix, setup}; +use self::handlers::{ci_output, command, lua_command, main, matrix, setup}; use super::EventStatus; @@ -14,6 +14,8 @@ pub enum Event { InputEvent(CrosstermEvent), MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse), CommandEvent(Command), + CiOutput(String), + LuaCommand(String), } impl Event { @@ -26,6 +28,14 @@ impl Event { Event::CommandEvent(event) => command::handle(app, event) .await .with_context(|| format!("Failed to handle command event: `{:#?}`", event)), + Event::CiOutput(output) => ci_output::handle(app, output).await.with_context(|| { + format!("Failed to handle command interface output: `{:#?}`", output) + }), + Event::LuaCommand(lua_code) => { + lua_command::handle(app, lua_code).await.with_context(|| { + format!("Failed to handle lua code: `{:#?}`", lua_code) + }) + } Event::InputEvent(event) => match app.status.state() { State::None => Ok(EventStatus::Ok), diff --git a/src/app/mod.rs b/src/app/mod.rs index 8580990..e745aa3 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -66,21 +66,6 @@ impl App<'_> { }) } - pub async fn handle_ci_event(&self, event: &str) -> Result { - info!("Recieved ci event: `{event}`; executing.."); - - // TODO: Should the ci support more than strings? - let output = self.lua.context(|context| -> Result { - let output = context - .load(&event) - .eval::() - .with_context(|| format!("Failed to execute: `{event}`"))?; - info!("Function evaluated to: `{output}`"); - Ok(output) - })?; - Ok(output) - } - pub async fn run(&mut self) -> Result<()> { // Spawn input event listener tokio::task::spawn(events::poll_input_events( diff --git a/src/ui/central/mod.rs b/src/ui/central/mod.rs index e03e658..882584d 100644 --- a/src/ui/central/mod.rs +++ b/src/ui/central/mod.rs @@ -2,8 +2,8 @@ pub mod update; use std::io::Stdout; -use anyhow::{bail, Result, Context}; -use cli_log::info; +use anyhow::{bail, Context, Result}; +use cli_log::{info, warn}; use crossterm::{ event::DisableMouseCapture, execute, @@ -125,6 +125,16 @@ impl UI<'_> { ); } + pub fn set_command_output(&mut self, output: &str) { + info!("Setting output to: `{}`", output); + if let Some(_) = self.cli { + let cli = Some(TextArea::from([output])); + self.cli = cli; + } else { + warn!("Failed to set output"); + } + } + pub fn cli_enable(&mut self) { self.input_position = InputPosition::CLI; if self.cli.is_some() {