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(())
}
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 => {}
}
}