diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2022-09-12 09:45:50 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2022-09-12 09:46:26 +0800 |
commit | 28f5ac90cc1ba7d668ae8a83eb5cd899294a8301 (patch) | |
tree | ed192e3b13c084afc1ab9a9ef08ddb369577714c /src | |
parent | e35baeaf25176e94ccc352b623f5cfd4b6b95b7b (diff) |
feat: Automatically resize if the terminal changes in size. (#28)
Diffstat (limited to 'src')
-rw-r--r-- | src/interactive/app/eventloop.rs | 30 | ||||
-rw-r--r-- | src/interactive/app/tests/journeys_with_writes.rs | 8 | ||||
-rw-r--r-- | src/interactive/app/tests/utils.rs | 8 |
3 files changed, 25 insertions, 21 deletions
diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index 6814dd2..77901ed 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -5,7 +5,7 @@ use crate::interactive::{ SortMode, }; use anyhow::Result; -use crosstermion::input::{key_input_channel, Key}; +use crosstermion::input::{input_channel, Event, Key}; use dua::{ traverse::{Traversal, TreeIndex}, WalkOptions, WalkResult, @@ -69,7 +69,7 @@ impl AppState { traversal: &mut Traversal, display: &mut DisplayOptions, terminal: &mut Terminal<B>, - keys: impl Iterator<Item = Key>, + events: impl Iterator<Item = Event>, ) -> Result<ProcessingResult> where B: Backend, @@ -77,8 +77,12 @@ impl AppState { use crosstermion::input::Key::*; use FocussedPane::*; - self.draw(window, traversal, *display, terminal)?; - for key in keys { + for event in events { + let key = match event { + Event::Key(key) => key, + Event::Resize(_, _) => Alt('\r'), + }; + self.reset_message(); match key { Char('?') => self.toggle_help_pane(window), @@ -106,17 +110,15 @@ impl AppState { } match self.focussed { - FocussedPane::Mark => { - self.dispatch_to_mark_pane(key, window, traversal, *display, terminal) - } - FocussedPane::Help => { + Mark => self.dispatch_to_mark_pane(key, window, traversal, *display, terminal), + Help => { window .help_pane .as_mut() .expect("help pane") .process_events(key); } - FocussedPane::Main => match key { + Main => match key { Char('O') => self.open_that(traversal), Char(' ') => self.mark_entry( CursorMode::KeepPosition, @@ -184,7 +186,7 @@ pub struct TerminalApp { pub window: MainWindow, } -type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Key>, TerminalApp); +type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Event>, TerminalApp); impl TerminalApp { pub fn refresh_view<B>(&mut self, terminal: &mut Terminal<B>) @@ -198,14 +200,14 @@ impl TerminalApp { &mut self.traversal, &mut self.display, terminal, - std::iter::once(Key::Alt('\r')), + std::iter::once(Event::Key(Key::Alt('\r'))), ) .ok(); } pub fn process_events<B>( &mut self, terminal: &mut Terminal<B>, - keys: impl Iterator<Item = Key>, + events: impl Iterator<Item = Event>, ) -> Result<WalkResult> where B: Backend, @@ -215,7 +217,7 @@ impl TerminalApp { &mut self.traversal, &mut self.display, terminal, - keys, + events, )? { ProcessingResult::Finished(res) | ProcessingResult::ExitRequested(res) => Ok(res), } @@ -240,7 +242,7 @@ impl TerminalApp { let (_, keys_rx) = std::sync::mpsc::channel(); keys_rx } - Interaction::Full => key_input_channel(), + Interaction::Full => input_channel(), }; let fetch_buffered_key_events = || { diff --git a/src/interactive/app/tests/journeys_with_writes.rs b/src/interactive/app/tests/journeys_with_writes.rs index 6e411f3..4e990ee 100644 --- a/src/interactive/app/tests/journeys_with_writes.rs +++ b/src/interactive/app/tests/journeys_with_writes.rs @@ -2,6 +2,8 @@ use crate::interactive::app::tests::utils::{ initialized_app_and_terminal_from_paths, into_keys, WritableFixture, }; use anyhow::Result; +use crosstermion::input::Event; +use crosstermion::input::Key; use pretty_assertions::assert_eq; #[test] @@ -24,11 +26,7 @@ fn basic_user_journey_with_deletion() -> Result<()> { // When selecting the marker window and pressing the combination to delete entries app.process_events( &mut terminal, - vec![ - crosstermion::input::Key::Char('\t'), - crosstermion::input::Key::Ctrl('r'), - ] - .into_iter(), + vec![Event::Key(Key::Char('\t')), Event::Key(Key::Ctrl('r'))].into_iter(), )?; assert!( app.window.mark_pane.is_none(), diff --git a/src/interactive/app/tests/utils.rs b/src/interactive/app/tests/utils.rs index 0e1a8f0..2882f6d 100644 --- a/src/interactive/app/tests/utils.rs +++ b/src/interactive/app/tests/utils.rs @@ -21,8 +21,12 @@ use crate::interactive::{app::tests::FIXTURE_PATH, Interaction, TerminalApp}; pub fn into_keys<'a>( bytes: impl Iterator<Item = &'a u8> + 'a, -) -> impl Iterator<Item = crosstermion::input::Key> + 'a { - bytes.map(|b| crosstermion::input::Key::Char(std::char::from_u32(*b as u32).unwrap())) +) -> impl Iterator<Item = crosstermion::input::Event> + 'a { + bytes.map(|b| { + crosstermion::input::Event::Key(crosstermion::input::Key::Char( + std::char::from_u32(*b as u32).unwrap(), + )) + }) } pub fn node_by_index(app: &TerminalApp, id: TreeIndex) -> &EntryData { |