forked from trinitrix/core
1
0
Fork 0

Fix(handles): Add command handling over the internal event stream

This commit is contained in:
Benedikt Peetz 2023-07-18 08:11:21 +02:00
parent a413171ffe
commit 14333944dc
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
5 changed files with 34 additions and 31 deletions

View File

@ -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 anyhow::Result;
use cli_log::info; use cli_log::info;
@ -32,5 +32,9 @@ pub async fn handle(app: &mut App<'_>, command: &Command) -> Result<EventStatus>
} }
EventStatus::Ok EventStatus::Ok
} }
Command::Greet(name) => {
info!("Greated {}", name);
EventStatus::Ok
}
}) })
} }

View File

@ -155,7 +155,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
code: KeyCode::Enter, code: KeyCode::Enter,
.. ..
}) => { }) => {
let cli_event = app.ui let ci_event = app.ui
.cli .cli
.as_mut() .as_mut()
.expect("This is already checked") .expect("This is already checked")
@ -165,16 +165,10 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
"There can only be one line in the buffer, as we collect it on enter being inputted" "There can only be one line in the buffer, as we collect it on enter being inputted"
) )
.to_owned(); .to_owned();
let output = app.handle_ci_event(&cli_event).await?; app.tx
.send(Event::LuaCommand(ci_event))
// delete the old text: .await
.context("Failed to send lua command to internal event stream")?;
// We can use a mutable borrow now, as we should only need one
let cli = app.ui.cli.as_mut().expect("Checked above");
cli.move_cursor(tui_textarea::CursorMove::Jump(0, 0));
cli.delete_str(0, cli_event.chars().count());
assert!(cli.is_empty());
cli.insert_str(output);
} }
_ => { _ => {
app.ui app.ui

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, 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; use super::EventStatus;
@ -14,6 +14,8 @@ pub enum Event {
InputEvent(CrosstermEvent), InputEvent(CrosstermEvent),
MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse), MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse),
CommandEvent(Command), CommandEvent(Command),
CiOutput(String),
LuaCommand(String),
} }
impl Event { impl Event {
@ -26,6 +28,14 @@ impl Event {
Event::CommandEvent(event) => command::handle(app, event) Event::CommandEvent(event) => command::handle(app, event)
.await .await
.with_context(|| format!("Failed to handle command event: `{:#?}`", event)), .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() { Event::InputEvent(event) => match app.status.state() {
State::None => Ok(EventStatus::Ok), State::None => Ok(EventStatus::Ok),

View File

@ -66,21 +66,6 @@ impl App<'_> {
}) })
} }
pub async fn handle_ci_event(&self, event: &str) -> Result<String> {
info!("Recieved ci event: `{event}`; executing..");
// TODO: Should the ci support more than strings?
let output = self.lua.context(|context| -> Result<String> {
let output = context
.load(&event)
.eval::<String>()
.with_context(|| format!("Failed to execute: `{event}`"))?;
info!("Function evaluated to: `{output}`");
Ok(output)
})?;
Ok(output)
}
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self) -> Result<()> {
// Spawn input event listener // Spawn input event listener
tokio::task::spawn(events::poll_input_events( tokio::task::spawn(events::poll_input_events(

View File

@ -2,8 +2,8 @@ pub mod update;
use std::io::Stdout; use std::io::Stdout;
use anyhow::{bail, Result, Context}; use anyhow::{bail, Context, Result};
use cli_log::info; use cli_log::{info, warn};
use crossterm::{ use crossterm::{
event::DisableMouseCapture, event::DisableMouseCapture,
execute, 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) { pub fn cli_enable(&mut self) {
self.input_position = InputPosition::CLI; self.input_position = InputPosition::CLI;
if self.cli.is_some() { if self.cli.is_some() {