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 cli_log::info;
@ -32,5 +32,9 @@ pub async fn handle(app: &mut App<'_>, command: &Command) -> Result<EventStatus>
}
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,
..
}) => {
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<E
"There can only be one line in the buffer, as we collect it on enter being inputted"
)
.to_owned();
let output = app.handle_ci_event(&cli_event).await?;
// delete the old text:
// 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.tx
.send(Event::LuaCommand(ci_event))
.await
.context("Failed to send lua command to internal event stream")?;
}
_ => {
app.ui

View File

@ -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),

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<()> {
// Spawn input event listener
tokio::task::spawn(events::poll_input_events(

View File

@ -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() {