From 95fd86e8515af5df6fc6977fdc129d2a974ade18 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 5 Aug 2025 16:17:56 -0700 Subject: [PATCH] dry out the input handling --- src/app/mod.rs | 101 ++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 4310fad..0c61e87 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -102,59 +102,66 @@ impl App { Ok(()) } - pub async fn handle_key_event(&mut self, key_event: KeyEvent, event: crossterm::event::Event) { - match self.screen.last_mut().unwrap() { - CurrentScreen::Logging => match key_event.code { + async fn handle_key_event(&mut self, key_event: KeyEvent, event: crossterm::event::Event) { + let code = key_event.code; + let mode = self.screen.last_mut().unwrap(); + match mode { + CurrentScreen::Main + | CurrentScreen::Logging + | CurrentScreen::Receiving + | CurrentScreen::Sending(SendingScreen::Files) + | CurrentScreen::Sending(SendingScreen::Peers) => match code { KeyCode::Esc => self.pop(), - KeyCode::Left => change_log_level(-1), - KeyCode::Right => change_log_level(1), - KeyCode::Char('q') => self.exit(), - _ => {} - }, - CurrentScreen::Receiving => match key_event.code { - KeyCode::Up => self.receiving_state.select_previous(), - KeyCode::Down => self.receiving_state.select_next(), - KeyCode::Char('a') => self.accept(), - KeyCode::Char('d') => self.deny(), - KeyCode::Esc => self.pop(), - KeyCode::Char('q') => self.exit(), - _ => {} - }, - CurrentScreen::Sending(s) => match s { - SendingScreen::Files => match key_event.code { - KeyCode::Esc => self.pop(), - KeyCode::Char('q') => self.exit(), - KeyCode::Tab => *s = SendingScreen::Peers, - KeyCode::Enter => self.chdir_or_send_file().await, - _ => self.file_picker.handle(&event).unwrap_or_default(), - }, - SendingScreen::Peers => match key_event.code { - KeyCode::Esc => self.pop(), - KeyCode::Char('q') => self.exit(), - KeyCode::Tab => *s = SendingScreen::Files, - KeyCode::Enter => self.send_content().await, - KeyCode::Up => self.peer_state.select_previous(), - KeyCode::Down => self.peer_state.select_next(), - _ => {} - }, - SendingScreen::Text => match key_event.code { - KeyCode::Esc => { - self.text = None; - *s = SendingScreen::Files; - } - KeyCode::Tab => *s = SendingScreen::Peers, - KeyCode::Enter => self.send_text().await, - _ => {} - }, - }, - _ => match key_event.code { KeyCode::Char('q') => self.exit(), KeyCode::Char('s') => self.send(), KeyCode::Char('r') => self.recv(), KeyCode::Char('l') => self.logs(), - KeyCode::Esc => self.pop(), - _ => {} + _ => match mode { + CurrentScreen::Logging => match code { + KeyCode::Left => change_log_level(-1), + KeyCode::Right => change_log_level(1), + _ => {} + }, + CurrentScreen::Receiving => match code { + KeyCode::Up => self.receiving_state.select_previous(), + KeyCode::Down => self.receiving_state.select_next(), + KeyCode::Char('a') => self.accept(), + KeyCode::Char('d') => self.deny(), + _ => {} + }, + CurrentScreen::Sending(sending_screen) => match sending_screen { + SendingScreen::Files => match code { + KeyCode::Char('t') => *sending_screen = SendingScreen::Text, + KeyCode::Tab => *sending_screen = SendingScreen::Peers, + KeyCode::Enter => self.chdir_or_send_file().await, + _ => self.file_picker.handle(&event).unwrap_or_default(), + }, + SendingScreen::Peers => match code { + KeyCode::Tab => *sending_screen = SendingScreen::Files, + KeyCode::Enter => self.send_content().await, + KeyCode::Up => self.peer_state.select_previous(), + KeyCode::Down => self.peer_state.select_next(), + _ => {} + }, + _ => unreachable!(), + }, + _ => {} + }, }, + CurrentScreen::Sending(sending_screen) => match sending_screen { + SendingScreen::Text => match code { + KeyCode::Tab => *sending_screen = SendingScreen::Peers, + KeyCode::Enter => self.send_text().await, + KeyCode::Esc => { + self.text = None; + *sending_screen = SendingScreen::Files; + } + _ => { /* todo: add input widget to handle key input here */ } + }, + _ => unreachable!(), + }, + + CurrentScreen::Stopping => {} } }