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)]
pub enum InputMessages {
ButtonPress(KeyCode),
ButtonPress((KeyCode, KeyModifiers)),
MouseEvent(MouseEvent),
}
+13 -3
View File
@@ -4,7 +4,7 @@ use std::sync::{
};
use crossterm::{
event::{DisableMouseCapture, KeyCode, MouseButton, MouseEvent, MouseEventKind},
event::{DisableMouseCapture, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind},
execute,
};
use parking_lot::Mutex;
@@ -60,7 +60,7 @@ impl InputHandler {
async fn start(&mut self) {
while let Some(message) = self.rec.recv().await {
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) => {
let error_or_help = self
.gui_state
@@ -135,11 +135,17 @@ impl InputHandler {
/// Handle any keyboard button events
#[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
let contains_error = self.gui_state.lock().status_contains(&[Status::Error]);
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 {
match key_code {
KeyCode::Char('q' | 'Q') => self.quit().await,
@@ -157,7 +163,11 @@ impl InputHandler {
_ => (),
}
} else {
// let abc = KeyEvent::new(KeyCode::Char('d'), KeyModifiers::Ctrl);
match key_code {
// KeyCode::Ctrl('c') => {
// self.quit().await;
// }
KeyCode::Char('0') => self.app_data.lock().reset_sorted(),
KeyCode::Char('1') => self.sort(Header::State),
KeyCode::Char('2') => self.sort(Header::Status),
+7 -2
View File
@@ -23,7 +23,10 @@ use docker_data::DockerData;
use input_handler::InputMessages;
use parking_lot::Mutex;
use parse_args::CliArgs;
use std::sync::{atomic::AtomicBool, Arc};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{info, Level};
@@ -134,8 +137,9 @@ async fn main() {
if args.gui {
Ui::create(app_data, docker_sx, gui_state, is_running, input_sx).await;
} else {
// Debug mode for testing, mostly pointless, doesn't take terminal
info!("in debug mode");
while is_running.load(Ordering::SeqCst) {
// Debug mode for testing, mostly pointless, doesn't take terminal
loop {
docker_sx.send(DockerMessage::Update).await.unwrap_or(());
tokio::time::sleep(std::time::Duration::from_millis(u64::from(
@@ -144,4 +148,5 @@ async fn main() {
.await;
}
}
}
}