From 529b869e80d6d25143e0a2f99a1025d18163ca81 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Thu, 13 Jul 2023 23:17:35 +0200 Subject: [PATCH] Feat(Commands): Started actual implementation of commands List of added commands: - Exit - CommandLineShow - CommandLineHide - CyclePlanes - CyclePlanesRev - RoomMessageSend --- src/app/command.rs | 6 ++++ .../event_types/event/handlers/command.rs | 30 ++++++++++++++++++- .../events/event_types/event/handlers/main.rs | 26 ++++++++-------- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/app/command.rs b/src/app/command.rs index 3c529bb..872c853 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -3,6 +3,12 @@ pub enum Command { // Closes the application Exit, + CommandLineShow, + CommandLineHide, + + CyclePlanes, + CyclePlanesRev, + // sends a message to the current room RoomMessageSend(String), } diff --git a/src/app/events/event_types/event/handlers/command.rs b/src/app/events/event_types/event/handlers/command.rs index de8441d..0c67ed2 100644 --- a/src/app/events/event_types/event/handlers/command.rs +++ b/src/app/events/event_types/event/handlers/command.rs @@ -4,5 +4,33 @@ use cli_log::info; pub async fn handle(app: &mut App<'_>, command: &Command) -> Result { info!("Handling command: {:#?}", command); - Ok(EventStatus::Ok) + + Ok(match command { + Command::Exit => EventStatus::Terminate, + + Command::CommandLineShow => { + app.ui.cli_enable(); + EventStatus::Ok + } + Command::CommandLineHide => { + app.ui.cli_disable(); + EventStatus::Ok + } + + Command::CyclePlanes => { + app.ui.cycle_main_input_position(); + EventStatus::Ok + } + Command::CyclePlanesRev => { + app.ui.cycle_main_input_position_rev(); + EventStatus::Ok + } + + Command::RoomMessageSend(msg) => { + if let Some(room) = app.status.room_mut() { + room.send(msg.clone()).await?; + } + 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 3a33d1b..fe43bf5 100644 --- a/src/app/events/event_types/event/handlers/main.rs +++ b/src/app/events/event_types/event/handlers/main.rs @@ -2,7 +2,7 @@ use anyhow::Result; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; use crate::{ - app::{events::event_types::EventStatus, App}, + app::{command::Command, events::event_types::EventStatus, App}, ui, }; @@ -10,26 +10,26 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result return Ok(EventStatus::Terminate), + }) => { + app.command_execute(Command::Exit).await?; + } CrosstermEvent::Key(KeyEvent { code: KeyCode::Tab, .. }) => { - app.ui.cycle_main_input_position(); + app.command_execute(Command::CyclePlanes).await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::BackTab, .. }) => { - app.ui.cycle_main_input_position_rev(); + app.command_execute(Command::CyclePlanesRev).await?; } CrosstermEvent::Key(KeyEvent { code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, .. }) => { - app.command_execute(crate::app::command::Command::Exit) - .await?; - app.ui.cli_enable(); + app.command_execute(Command::CommandLineShow).await?; } input => match app.ui.input_position() { ui::MainInputPosition::MessageCompose => { @@ -39,13 +39,11 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { - match app.status.room_mut() { - Some(room) => { - room.send(app.ui.message_compose.lines().join("\n")).await?; - app.ui.message_compose_clear(); - } - None => (), - }; + app.command_execute(Command::RoomMessageSend( + app.ui.message_compose.lines().join("\n"), + )) + .await?; + app.ui.message_compose_clear(); } _ => { app.ui