feat: KeyEvents send modifier, so can quit on ctrl + c

This commit is contained in:
Jack Wills
2023-03-02 16:09:57 +00:00
parent 507660d835
commit 598f67c6f6
3 changed files with 28 additions and 13 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
use crossterm::event::{KeyCode, MouseEvent}; use crossterm::event::{KeyCode, KeyModifiers, MouseEvent};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum InputMessages { pub enum InputMessages {
ButtonPress(KeyCode), ButtonPress((KeyCode, KeyModifiers)),
MouseEvent(MouseEvent), MouseEvent(MouseEvent),
} }
+13 -3
View File
@@ -4,7 +4,7 @@ use std::sync::{
}; };
use crossterm::{ use crossterm::{
event::{DisableMouseCapture, KeyCode, MouseButton, MouseEvent, MouseEventKind}, event::{DisableMouseCapture, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind},
execute, execute,
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -60,7 +60,7 @@ impl InputHandler {
async fn start(&mut self) { async fn start(&mut self) {
while let Some(message) = self.rec.recv().await { while let Some(message) = self.rec.recv().await {
match message { match message {
InputMessages::ButtonPress(key_code) => self.button_press(key_code).await, InputMessages::ButtonPress(key) => self.button_press(key.0, key.1).await,
InputMessages::MouseEvent(mouse_event) => { InputMessages::MouseEvent(mouse_event) => {
let error_or_help = self let error_or_help = self
.gui_state .gui_state
@@ -135,11 +135,17 @@ impl InputHandler {
/// Handle any keyboard button events /// Handle any keyboard button events
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
async fn button_press(&mut self, key_code: KeyCode) { async fn button_press(&mut self, key_code: KeyCode, key_modififer: KeyModifiers) {
// TODO - refactor this to a single call, maybe return Error, Help or Normal // TODO - refactor this to a single call, maybe return Error, Help or Normal
let contains_error = self.gui_state.lock().status_contains(&[Status::Error]); let contains_error = self.gui_state.lock().status_contains(&[Status::Error]);
let contains_help = self.gui_state.lock().status_contains(&[Status::Help]); let contains_help = self.gui_state.lock().status_contains(&[Status::Help]);
// Quit on Ctrl + c/ Ctrl + C
let is_c = || key_code == KeyCode::Char('c') || key_code == KeyCode::Char('C');
if key_modififer == KeyModifiers::CONTROL && is_c() {
self.quit().await;
}
if contains_error { if contains_error {
match key_code { match key_code {
KeyCode::Char('q' | 'Q') => self.quit().await, KeyCode::Char('q' | 'Q') => self.quit().await,
@@ -157,7 +163,11 @@ impl InputHandler {
_ => (), _ => (),
} }
} else { } else {
// let abc = KeyEvent::new(KeyCode::Char('d'), KeyModifiers::Ctrl);
match key_code { match key_code {
// KeyCode::Ctrl('c') => {
// self.quit().await;
// }
KeyCode::Char('0') => self.app_data.lock().reset_sorted(), KeyCode::Char('0') => self.app_data.lock().reset_sorted(),
KeyCode::Char('1') => self.sort(Header::State), KeyCode::Char('1') => self.sort(Header::State),
KeyCode::Char('2') => self.sort(Header::Status), KeyCode::Char('2') => self.sort(Header::Status),
+13 -8
View File
@@ -23,7 +23,10 @@ use docker_data::DockerData;
use input_handler::InputMessages; use input_handler::InputMessages;
use parking_lot::Mutex; use parking_lot::Mutex;
use parse_args::CliArgs; use parse_args::CliArgs;
use std::sync::{atomic::AtomicBool, Arc}; use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{info, Level}; use tracing::{info, Level};
@@ -134,14 +137,16 @@ async fn main() {
if args.gui { if args.gui {
Ui::create(app_data, docker_sx, gui_state, is_running, input_sx).await; Ui::create(app_data, docker_sx, gui_state, is_running, input_sx).await;
} else { } else {
// Debug mode for testing, mostly pointless, doesn't take terminal
info!("in debug mode"); info!("in debug mode");
loop { while is_running.load(Ordering::SeqCst) {
docker_sx.send(DockerMessage::Update).await.unwrap_or(()); // Debug mode for testing, mostly pointless, doesn't take terminal
tokio::time::sleep(std::time::Duration::from_millis(u64::from( loop {
args.docker_interval, docker_sx.send(DockerMessage::Update).await.unwrap_or(());
))) tokio::time::sleep(std::time::Duration::from_millis(u64::from(
.await; args.docker_interval,
)))
.await;
}
} }
} }
} }