From 74f3b25827f8fa6c9071af06774ceb59f37a3e78 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sat, 9 Sep 2023 19:57:20 +0200 Subject: [PATCH] feat(states): Add `command` state for command only keymappings --- .../event_types/event/handlers/command.rs | 1 + .../events/event_types/event/handlers/main.rs | 77 +++++++++++-------- src/app/events/event_types/event/mod.rs | 3 + src/app/status.rs | 2 + 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/app/events/event_types/event/handlers/command.rs b/src/app/events/event_types/event/handlers/command.rs index 841db28..f993514 100644 --- a/src/app/events/event_types/event/handlers/command.rs +++ b/src/app/events/event_types/event/handlers/command.rs @@ -81,6 +81,7 @@ pub async fn handle( Command::CommandLineShow => { app.ui.cli_enable(); + app.status.set_state(State::Command); send_status_output!("CLI online"); 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 6ddcce5..710050b 100644 --- a/src/app/events/event_types/event/handlers/main.rs +++ b/src/app/events/event_types/event/handlers/main.rs @@ -10,10 +10,52 @@ use crate::{ ui::central, }; -pub async fn handle_normal( +pub async fn handle_command( app: &mut App<'_>, input_event: &CrosstermEvent, ) -> Result { + if let Some(cli) = &app.ui.cli { + match input_event { + CrosstermEvent::Key(KeyEvent { + code: KeyCode::Esc, .. + }) => { + app.tx + .send(Event::CommandEvent(Command::SetModeNormal, None)) + .await?; + } + CrosstermEvent::Key(KeyEvent { + code: KeyCode::Enter, + .. + }) => { + let ci_event = cli + .lines() + .get(0) + .expect( + "One line always exists, + and others can't exists + because we collect on + enter", + ) + .to_owned(); + app.tx + .send(Event::LuaCommand(ci_event)) + .await + .context("Failed to send lua command to internal event stream")?; + } + _ => { + app.ui + .cli + .as_mut() + .expect("This is already checked") + .input(tui_textarea::Input::from(input_event.to_owned())); + } + } + } else { + unreachable!("The cli should not be active while no cli is defined"); + } + Ok(EventStatus::Ok) +} +pub async fn handle_normal(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { match input_event { CrosstermEvent::Key(KeyEvent { code: KeyCode::Esc, .. @@ -137,38 +179,7 @@ pub async fn handle_normal( _ => (), }; } - central::InputPosition::CLI => { - if let Some(cli) = &app.ui.cli { - match input { - CrosstermEvent::Key(KeyEvent { - code: KeyCode::Enter, - .. - }) => { - let ci_event = cli - .lines() - .get(0) - .expect( - "One line always exists, - and others can't exists - because we collect on - enter", - ) - .to_owned(); - app.tx - .send(Event::LuaCommand(ci_event)) - .await - .context("Failed to send lua command to internal event stream")?; - } - _ => { - app.ui - .cli - .as_mut() - .expect("This is already checked") - .input(tui_textarea::Input::from(input.to_owned())); - } - }; - }; - } + _ => (), }, }; diff --git a/src/app/events/event_types/event/mod.rs b/src/app/events/event_types/event/mod.rs index 942070a..8535c01 100644 --- a/src/app/events/event_types/event/mod.rs +++ b/src/app/events/event_types/event/mod.rs @@ -45,6 +45,9 @@ impl Event { State::Insert => main::handle_insert(app, &event).await.with_context(|| { format!("Failed to handle input (insert) event: `{:#?}`", event) }), + State::Command => main::handle_command(app, &event).await.with_context(|| { + format!("Failed to handle input (command) event: `{:#?}`", event) + }), State::Setup => setup::handle(app, &event).await.with_context(|| { format!("Failed to handle input (setup) event: `{:#?}`", event) }), diff --git a/src/app/status.rs b/src/app/status.rs index 9ce211c..8b9168b 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -15,6 +15,7 @@ use matrix_sdk::{ pub enum State { Normal, Insert, + Command, /// Temporary workaround until command based login is working Setup, } @@ -57,6 +58,7 @@ impl fmt::Display for State { match self { Self::Normal => write!(f, "Normal"), Self::Insert => write!(f, "Insert"), + Self::Command => write!(f, "Command"), Self::Setup => write!(f, "Setup (!! workaround !!)"), } }