diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-29 22:53:58 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-29 22:53:58 +0800 |
commit | e811effe6424cd691260b07d1187d7c2d34ad4f1 (patch) | |
tree | a4603c37c04f7e551372b003e7b850f005db7c85 | |
parent | 7689016c537d054a519e4e61c577e30645537213 (diff) |
first step towards support aync/channel based input events
-rw-r--r-- | src/interactive/app/eventloop.rs | 120 |
1 files changed, 70 insertions, 50 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 4960566..94b6123 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -62,13 +62,13 @@ impl AppState { draw_window(window, props, terminal) } - pub fn process_events<B>( + pub fn process_event<B>( &mut self, window: &mut MainWindow, traversal: &mut Traversal, display: &mut DisplayOptions, terminal: &mut Terminal<B>, - keys: impl Iterator<Item = Result<Key, io::Error>>, + key: Key, ) -> Result<ProcessingResult, Error> where B: Backend, @@ -77,61 +77,81 @@ impl AppState { use FocussedPane::*; self.reset_message(); - self.draw(window, traversal, display.clone(), terminal)?; - for key in keys.filter_map(Result::ok) { - self.reset_message(); - match key { - Char('?') => self.toggle_help_pane(window), - Char('\t') => { - self.cycle_focus(window); - } - Ctrl('c') => { + match key { + Char('?') => self.toggle_help_pane(window), + Char('\t') => { + self.cycle_focus(window); + } + Ctrl('c') => { + return Ok(ProcessingResult::ExitRequested(WalkResult { + num_errors: traversal.io_errors, + })) + } + Char('q') | Esc => match self.focussed { + Main => { return Ok(ProcessingResult::ExitRequested(WalkResult { num_errors: traversal.io_errors, })) } - Char('q') | Esc => match self.focussed { - Main => { - return Ok(ProcessingResult::ExitRequested(WalkResult { - num_errors: traversal.io_errors, - })) - } - Mark => self.focussed = Main, - Help => { - self.focussed = Main; - window.help_pane = None - } - }, - _ => {} - } + Mark => self.focussed = Main, + Help => { + self.focussed = Main; + window.help_pane = None + } + }, + _ => {} + } - match self.focussed { - FocussedPane::Mark => { - self.dispatch_to_mark_pane(key, window, traversal, display.clone(), terminal) + match self.focussed { + FocussedPane::Mark => { + self.dispatch_to_mark_pane(key, window, traversal, display.clone(), terminal) + } + FocussedPane::Help => { + window.help_pane.as_mut().expect("help pane").key(key); + } + FocussedPane::Main => match key { + Char('O') => self.open_that(traversal), + Char(' ') => self.mark_entry(false, window, traversal), + Char('d') => self.mark_entry(true, window, traversal), + Char('u') | Char('h') | Backspace | Left => { + self.exit_node_with_traversal(traversal) } - FocussedPane::Help => { - window.help_pane.as_mut().expect("help pane").key(key); + Char('o') | Char('l') | Char('\n') | Right => { + self.enter_node_with_traversal(traversal) } - FocussedPane::Main => match key { - Char('O') => self.open_that(traversal), - Char(' ') => self.mark_entry(false, window, traversal), - Char('d') => self.mark_entry(true, window, traversal), - Char('u') | Char('h') | Backspace | Left => { - self.exit_node_with_traversal(traversal) - } - Char('o') | Char('l') | Char('\n') | Right => { - self.enter_node_with_traversal(traversal) - } - Ctrl('u') | PageUp => self.change_entry_selection(CursorDirection::PageUp), - Char('k') | Up => self.change_entry_selection(CursorDirection::Up), - Char('j') | Down => self.change_entry_selection(CursorDirection::Down), - Ctrl('d') | PageDown => self.change_entry_selection(CursorDirection::PageDown), - Char('s') => self.cycle_sorting(traversal), - Char('g') => display.byte_vis.cycle(), - _ => {} - }, - }; - self.draw(window, traversal, display.clone(), terminal)?; + Ctrl('u') | PageUp => self.change_entry_selection(CursorDirection::PageUp), + Char('k') | Up => self.change_entry_selection(CursorDirection::Up), + Char('j') | Down => self.change_entry_selection(CursorDirection::Down), + Ctrl('d') | PageDown => self.change_entry_selection(CursorDirection::PageDown), + Char('s') => self.cycle_sorting(traversal), + Char('g') => display.byte_vis.cycle(), + _ => {} + }, + }; + self.draw(window, traversal, display.clone(), terminal)?; + Ok(ProcessingResult::Finished(WalkResult { + num_errors: traversal.io_errors, + })) + } + pub fn process_events<B>( + &mut self, + window: &mut MainWindow, + traversal: &mut Traversal, + display: &mut DisplayOptions, + terminal: &mut Terminal<B>, + keys: impl Iterator<Item = Result<Key, io::Error>>, + ) -> Result<ProcessingResult, Error> + where + B: Backend, + { + self.reset_message(); + self.draw(window, traversal, display.clone(), terminal)?; + for key in keys.filter_map(Result::ok) { + if let r @ ProcessingResult::ExitRequested(_) = + self.process_event(window, traversal, display, terminal, key)? + { + return Ok(r); + } } Ok(ProcessingResult::Finished(WalkResult { num_errors: traversal.io_errors, |