dry out the input handling

This commit is contained in:
Joe Ardent 2025-08-05 16:17:56 -07:00
parent 95e24d14b3
commit 95fd86e851

View file

@ -102,59 +102,66 @@ impl App {
Ok(()) Ok(())
} }
pub async fn handle_key_event(&mut self, key_event: KeyEvent, event: crossterm::event::Event) { async fn handle_key_event(&mut self, key_event: KeyEvent, event: crossterm::event::Event) {
match self.screen.last_mut().unwrap() { let code = key_event.code;
CurrentScreen::Logging => match 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::Esc => self.pop(),
KeyCode::Char('q') => self.exit(),
KeyCode::Char('s') => self.send(),
KeyCode::Char('r') => self.recv(),
KeyCode::Char('l') => self.logs(),
_ => match mode {
CurrentScreen::Logging => match code {
KeyCode::Left => change_log_level(-1), KeyCode::Left => change_log_level(-1),
KeyCode::Right => change_log_level(1), KeyCode::Right => change_log_level(1),
KeyCode::Char('q') => self.exit(),
_ => {} _ => {}
}, },
CurrentScreen::Receiving => match key_event.code { CurrentScreen::Receiving => match code {
KeyCode::Up => self.receiving_state.select_previous(), KeyCode::Up => self.receiving_state.select_previous(),
KeyCode::Down => self.receiving_state.select_next(), KeyCode::Down => self.receiving_state.select_next(),
KeyCode::Char('a') => self.accept(), KeyCode::Char('a') => self.accept(),
KeyCode::Char('d') => self.deny(), KeyCode::Char('d') => self.deny(),
KeyCode::Esc => self.pop(),
KeyCode::Char('q') => self.exit(),
_ => {} _ => {}
}, },
CurrentScreen::Sending(s) => match s { CurrentScreen::Sending(sending_screen) => match sending_screen {
SendingScreen::Files => match key_event.code { SendingScreen::Files => match code {
KeyCode::Esc => self.pop(), KeyCode::Char('t') => *sending_screen = SendingScreen::Text,
KeyCode::Char('q') => self.exit(), KeyCode::Tab => *sending_screen = SendingScreen::Peers,
KeyCode::Tab => *s = SendingScreen::Peers,
KeyCode::Enter => self.chdir_or_send_file().await, KeyCode::Enter => self.chdir_or_send_file().await,
_ => self.file_picker.handle(&event).unwrap_or_default(), _ => self.file_picker.handle(&event).unwrap_or_default(),
}, },
SendingScreen::Peers => match key_event.code { SendingScreen::Peers => match code {
KeyCode::Esc => self.pop(), KeyCode::Tab => *sending_screen = SendingScreen::Files,
KeyCode::Char('q') => self.exit(),
KeyCode::Tab => *s = SendingScreen::Files,
KeyCode::Enter => self.send_content().await, KeyCode::Enter => self.send_content().await,
KeyCode::Up => self.peer_state.select_previous(), KeyCode::Up => self.peer_state.select_previous(),
KeyCode::Down => self.peer_state.select_next(), KeyCode::Down => self.peer_state.select_next(),
_ => {} _ => {}
}, },
SendingScreen::Text => match key_event.code { _ => 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 => { KeyCode::Esc => {
self.text = None; self.text = None;
*s = SendingScreen::Files; *sending_screen = SendingScreen::Files;
} }
KeyCode::Tab => *s = SendingScreen::Peers, _ => { /* todo: add input widget to handle key input here */ }
KeyCode::Enter => self.send_text().await,
_ => {}
}, },
_ => unreachable!(),
}, },
_ => match key_event.code {
KeyCode::Char('q') => self.exit(), CurrentScreen::Stopping => {}
KeyCode::Char('s') => self.send(),
KeyCode::Char('r') => self.recv(),
KeyCode::Char('l') => self.logs(),
KeyCode::Esc => self.pop(),
_ => {}
},
} }
} }