diff options
author | Tim Oram <dev@mitmaro.ca> | 2024-02-19 14:39:31 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2024-02-20 08:21:29 -0330 |
commit | 954f0976fda9e9d70ee4ee582718407b2eefd41c (patch) | |
tree | ac616c5288b75e2f9e6b47951ea49bdac65562ae | |
parent | 2227eb36d6b278746392bb9f910385515e58bdbc (diff) |
Use select macro for event handling
-rw-r--r-- | src/components/help.rs | 25 | ||||
-rw-r--r-- | src/components/help/tests.rs | 4 | ||||
-rw-r--r-- | src/modules/error.rs | 18 | ||||
-rw-r--r-- | src/modules/list.rs | 18 | ||||
-rw-r--r-- | src/modules/show_commit.rs | 71 | ||||
-rw-r--r-- | src/util.rs | 24 |
6 files changed, 75 insertions, 85 deletions
diff --git a/src/components/help.rs b/src/components/help.rs index e68fa72..59f5a59 100644 --- a/src/components/help.rs +++ b/src/components/help.rs @@ -5,8 +5,9 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ display::DisplayColor, - first, input::{Event, InputOptions, StandardEvent}, + process::Results, + select, util::handle_view_data_scroll, view::{LineSegment, ViewData, ViewLine}, }; @@ -87,15 +88,19 @@ impl Help { (self.is_active() || event == Event::Standard(StandardEvent::Help)).then_some(event) } - pub(crate) fn handle_event(&mut self, event: Event, view_state: &crate::view::State) { - let mut event_handler = || { - match event { - Event::Key(_) | Event::Standard(StandardEvent::Help) => self.active = false, - _ => {}, - } - Some(()) - }; - first!(|| handle_view_data_scroll(event, view_state), event_handler); + pub(crate) fn handle_event(&mut self, event: Event, view_state: &crate::view::State) -> Option<Results> { + self.is_active().then(|| { + select!( + default || { + match event { + Event::Key(_) | Event::Standard(StandardEvent::Help) => self.active = false, + _ => {}, + } + Results::new() + }, + || handle_view_data_scroll(event, view_state) + ) + }) } pub(crate) fn set_active(&mut self) { diff --git a/src/components/help/tests.rs b/src/components/help/tests.rs index fa62db1..836cf43 100644 --- a/src/components/help/tests.rs +++ b/src/components/help/tests.rs @@ -9,7 +9,9 @@ use crate::{ fn handle_event(help: &mut Help, event: Event) { let evt = help.read_event(event).unwrap_or(event); - with_view_state(|context| help.handle_event(evt, &context.state)); + with_view_state(|context| { + let _result = help.handle_event(evt, &context.state); + }); } #[test] diff --git a/src/modules/error.rs b/src/modules/error.rs index a8d2bea..235f032 100644 --- a/src/modules/error.rs +++ b/src/modules/error.rs @@ -6,6 +6,7 @@ use crate::{ input::{Event, InputOptions}, module::{Module, State}, process::Results, + select, util::handle_view_data_scroll, view::{LineSegment, RenderContext, ViewData, ViewLine}, }; @@ -34,13 +35,16 @@ impl Module for Error { } fn handle_event(&mut self, event: Event, view_state: &crate::view::State) -> Results { - let mut results = Results::new(); - if handle_view_data_scroll(event, view_state).is_none() { - if let Event::Key(_) = event { - results.state(self.return_state); - } - } - results + select!( + default || { + let mut results = Results::new(); + if let Event::Key(_) = event { + results.state(self.return_state); + } + results + }, + || handle_view_data_scroll(event, view_state) + ) } fn handle_error(&mut self, error: &anyhow::Error) -> Results { diff --git a/src/modules/list.rs b/src/modules/list.rs index 8f106f1..8b56b45 100644 --- a/src/modules/list.rs +++ b/src/modules/list.rs @@ -117,8 +117,8 @@ impl Module for List { ListState::Edit => self.handle_edit_mode_input(event), } }, - || self.handle_normal_help_input(event, view_state), - || self.handle_visual_help_input(event, view_state), + || self.normal_mode_help.handle_event(event, view_state), + || self.visual_mode_help.handle_event(event, view_state), || self.handle_search_input(event) ) } @@ -590,20 +590,6 @@ impl List { } } - fn handle_normal_help_input(&mut self, event: Event, view_state: &crate::view::State) -> Option<Results> { - self.normal_mode_help.is_active().then(|| { - self.normal_mode_help.handle_event(event, view_state); - Results::new() - }) - } - - fn handle_visual_help_input(&mut self, event: Event, view_state: &crate::view::State) -> Option<Results> { - self.visual_mode_help.is_active().then(|| { - self.visual_mode_help.handle_event(event, view_state); - Results::new() - }) - } - fn handle_search_input(&mut self, event: Event) -> Option<Results> { if !self.search_bar.is_active() { return None; diff --git a/src/modules/show_commit.rs b/src/modules/show_commit.rs index 4140d8f..64673ed 100644 --- a/src/modules/show_commit.rs +++ b/src/modules/show_commit.rs @@ -136,42 +136,41 @@ impl Module for ShowCommit { } fn handle_event(&mut self, event: Event, view_state: &crate::view::State) -> Results { - if self.help.is_active() { - self.help.handle_event(event, view_state); - return Results::new(); - } - - let mut results = Results::new(); - - let active_view_data = match self.state { - ShowCommitState::Overview => &mut self.overview_view_data, - ShowCommitState::Diff => &mut self.diff_view_data, - }; - - if handle_view_data_scroll(event, view_state).is_none() { - match event { - Event::Standard(StandardEvent::ShowDiff) => { - active_view_data.update_view_data(|updater| updater.clear()); - self.state = match self.state { - ShowCommitState::Overview => ShowCommitState::Diff, - ShowCommitState::Diff => ShowCommitState::Overview, - } - }, - Event::Standard(StandardEvent::Help) => self.help.set_active(), - Event::Key(_) => { - active_view_data.update_view_data(|updater| updater.clear()); - if self.state == ShowCommitState::Diff { - self.state = ShowCommitState::Overview; - } - else { - results.state(State::List); - } - }, - Event::Resize(..) => active_view_data.update_view_data(|updater| updater.clear()), - _ => {}, - } - } - results + select!( + default || { + let mut results = Results::new(); + + let active_view_data = match self.state { + ShowCommitState::Overview => &mut self.overview_view_data, + ShowCommitState::Diff => &mut self.diff_view_data, + }; + + match event { + Event::Standard(StandardEvent::ShowDiff) => { + active_view_data.update_view_data(|updater| updater.clear()); + self.state = match self.state { + ShowCommitState::Overview => ShowCommitState::Diff, + ShowCommitState::Diff => ShowCommitState::Overview, + } + }, + Event::Standard(StandardEvent::Help) => self.help.set_active(), + Event::Key(_) => { + active_view_data.update_view_data(|updater| updater.clear()); + if self.state == ShowCommitState::Diff { + self.state = ShowCommitState::Overview; + } + else { + results.state(State::List); + } + }, + Event::Resize(..) => active_view_data.update_view_data(|updater| updater.clear()), + _ => {}, + } + results + }, + || self.help.handle_event(event, view_state), + || handle_view_data_scroll(event, view_state) + ) } } diff --git a/src/util.rs b/src/util.rs index 81eac5d..1eb20ec 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,7 @@ -use crate::input::{Event, StandardEvent}; +use crate::{ + input::{Event, StandardEvent}, + process::Results, +}; #[macro_export] macro_rules! select { @@ -23,19 +26,9 @@ macro_rules! select { }; } -#[macro_export] -macro_rules! first { - ($first: expr, $($arg:expr),*) => { - if $first().is_some() { - } - $(else if $arg().is_some() { - })* - }; -} - /// Utility function to handle scroll events. #[must_use] -pub(crate) fn handle_view_data_scroll(event: Event, view_state: &crate::view::State) -> Option<Event> { +pub(crate) fn handle_view_data_scroll(event: Event, view_state: &crate::view::State) -> Option<Results> { match event { Event::Standard(StandardEvent::ScrollLeft) => view_state.scroll_left(), Event::Standard(StandardEvent::ScrollRight) => view_state.scroll_right(), @@ -47,12 +40,13 @@ pub(crate) fn handle_view_data_scroll(event: Event, view_state: &crate::view::St Event::Standard(StandardEvent::ScrollJumpUp) => view_state.scroll_page_up(), _ => return None, }; - Some(event) + Some(Results::new()) } #[cfg(test)] mod tests { use captur::capture; + use claims::{assert_none, assert_some}; use rstest::rstest; use super::*; @@ -71,7 +65,7 @@ mod tests { with_view_state(|context| { capture!(action); let event = Event::from(meta_event); - assert_eq!(handle_view_data_scroll(event, &context.state), Some(event)); + assert_some!(handle_view_data_scroll(event, &context.state)); context.assert_render_action(&[action]); }); } @@ -80,7 +74,7 @@ mod tests { fn handle_view_data_scroll_event_other() { with_view_state(|context| { let event = Event::from('a'); - assert!(handle_view_data_scroll(event, &context.state).is_none()); + assert_none!(handle_view_data_scroll(event, &context.state)); context.assert_render_action(&[]); }); } |