From 0114e695428579ef4461b289d7372e3b392b5e62 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 23 Nov 2023 15:26:42 +0200 Subject: [PATCH] Add next_search_result and previous_search_result shortcuts Signed-off-by: Manos Pitsidianakis --- meli/docs/meli.conf.5 | 13 +++++++++++-- meli/src/conf/shortcuts.rs | 8 +++++--- meli/src/utilities.rs | 35 ++++++++++++++++++++++++----------- meli/src/utilities/pager.rs | 26 +++++++++++++++++--------- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/meli/docs/meli.conf.5 b/meli/docs/meli.conf.5 index e692bcd3..18faf7ad 100644 --- a/meli/docs/meli.conf.5 +++ b/meli/docs/meli.conf.5 @@ -747,8 +747,17 @@ Open list entry. Show next info message, if any .Pq Ql Em M-> \" default value .It Ic info_message_previous -Show previous info message, if any -.Pq Ql Em M-< \" default value +Show previous info message, if any. +.Pq Em M-< \" default value +.It Ic focus_in_text_field +Focus on a text field. +.Pq Em Enter \" default value +.It Ic next_search_result +Scroll to next search result. +.Pq Em n \" default value +.It Ic previous_search_result +Scroll to previous search result. +.Pq Em N .El .sp .Em listing diff --git a/meli/src/conf/shortcuts.rs b/meli/src/conf/shortcuts.rs index 21763144..41129d02 100644 --- a/meli/src/conf/shortcuts.rs +++ b/meli/src/conf/shortcuts.rs @@ -221,9 +221,11 @@ shortcut_key_values! { "general", home_page |> "Go to first page. (catch-all setting)" |> Key::Home, end_page |> "Go to last page. (catch-all setting)" |> Key::End, open_entry |> "Open list entry. (catch-all setting)" |> Key::Char('\n'), - info_message_next |> "Show next info message, if any" |> Key::Alt('>'), - info_message_previous |> "Show previous info message, if any" |> Key::Alt('<'), - focus_in_text_field |> "Focus on a text field." |> Key::Char('\n') + info_message_next |> "Show next info message, if any." |> Key::Alt('>'), + info_message_previous |> "Show previous info message, if any." |> Key::Alt('<'), + focus_in_text_field |> "Focus on a text field." |> Key::Char('\n'), + next_search_result |> "Scroll to next search result." |> Key::Char('n'), + previous_search_result |> "Scroll to previous search result." |> Key::Char('N') } } diff --git a/meli/src/utilities.rs b/meli/src/utilities.rs index e9d6e540..307b75a1 100644 --- a/meli/src/utilities.rs +++ b/meli/src/utilities.rs @@ -51,12 +51,21 @@ use indexmap::IndexMap; pub use self::tables::*; use crate::{components::ExtendShortcutsMaps, jobs::JobId, melib::text_processing::TextProcessing}; +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] +pub enum SearchMovement { + Previous, + #[default] + Next, + First, + Last, +} + #[derive(Default, Debug, Clone)] pub struct SearchPattern { pattern: String, positions: Vec<(usize, usize)>, cursor: usize, - movement: Option, + movement: Option, } /// Status bar. @@ -1215,7 +1224,7 @@ impl Component for Tabbed { if !search.positions.is_empty() { if let Some(mvm) = search.movement.take() { match mvm { - PageMovement::Home => { + SearchMovement::First => { if self.help_view.cursor.1 > search.positions[search.cursor].0 { self.help_view.cursor.1 = search.positions[search.cursor].0; } @@ -1225,12 +1234,12 @@ impl Component for Tabbed { self.help_view.cursor.1 = search.positions[search.cursor].0; } } - PageMovement::Up(_) => { + SearchMovement::Previous => { if self.help_view.cursor.1 > search.positions[search.cursor].0 { self.help_view.cursor.1 = search.positions[search.cursor].0; } } - PageMovement::Down(_) => { + SearchMovement::Next => { if self.help_view.cursor.1 + rows < search.positions[search.cursor].0 { @@ -1415,16 +1424,18 @@ impl Component for Tabbed { pattern: pattern.to_string(), positions: vec![], cursor: 0, - movement: Some(PageMovement::Home), + movement: Some(SearchMovement::First), }); self.dirty = true; return true; } - UIEvent::Input(Key::Char('n')) - if self.show_shortcuts && self.help_view.search.is_some() => + UIEvent::Input(ref key) + if shortcut!(key == shortcuts[Shortcuts::GENERAL]["next_search_result"]) + && self.show_shortcuts + && self.help_view.search.is_some() => { if let Some(ref mut search) = self.help_view.search { - search.movement = Some(PageMovement::Down(1)); + search.movement = Some(SearchMovement::Next); search.cursor += 1; } else { unsafe { @@ -1434,11 +1445,13 @@ impl Component for Tabbed { self.dirty = true; return true; } - UIEvent::Input(Key::Char('N')) - if self.show_shortcuts && self.help_view.search.is_some() => + UIEvent::Input(ref key) + if shortcut!(key == shortcuts[Shortcuts::GENERAL]["previous_search_result"]) + && self.show_shortcuts + && self.help_view.search.is_some() => { if let Some(ref mut search) = self.help_view.search { - search.movement = Some(PageMovement::Up(1)); + search.movement = Some(SearchMovement::Previous); search.cursor = search.cursor.saturating_sub(1); } else { unsafe { diff --git a/meli/src/utilities/pager.rs b/meli/src/utilities/pager.rs index ea1c843f..67c3771f 100644 --- a/meli/src/utilities/pager.rs +++ b/meli/src/utilities/pager.rs @@ -537,17 +537,19 @@ impl Component for Pager { if !search.positions.is_empty() { if let Some(mvm) = search.movement.take() { match mvm { - PageMovement::Up(_) => { + SearchMovement::First | SearchMovement::Last => { + self.cursor.1 = search.positions[search.cursor].0; + } + SearchMovement::Previous => { if self.cursor.1 > search.positions[search.cursor].0 { self.cursor.1 = search.positions[search.cursor].0; } } - PageMovement::Down(_) => { - if self.cursor.1 + height < search.positions[search.cursor].0 { + SearchMovement::Next => { + if self.cursor.1 + rows < search.positions[search.cursor].0 { self.cursor.1 = search.positions[search.cursor].0; } } - _ => {} } } } @@ -767,24 +769,30 @@ impl Component for Pager { pattern: pattern.to_string(), positions: vec![], cursor: 0, - movement: Some(PageMovement::Home), + movement: Some(SearchMovement::First), }); self.initialised = false; self.dirty = true; return true; } - UIEvent::Input(Key::Char('n')) if self.search.is_some() => { + UIEvent::Input(ref key) + if shortcut!(key == shortcuts[Shortcuts::GENERAL]["next_search_result"]) + && self.search.is_some() => + { if let Some(ref mut search) = self.search { - search.movement = Some(PageMovement::Down(1)); + search.movement = Some(SearchMovement::Next); search.cursor += 1; self.initialised = false; self.dirty = true; return true; } } - UIEvent::Input(Key::Char('N')) if self.search.is_some() => { + UIEvent::Input(ref key) + if shortcut!(key == shortcuts[Shortcuts::GENERAL]["previous_search_result"]) + && self.search.is_some() => + { if let Some(ref mut search) = self.search { - search.movement = Some(PageMovement::Up(1)); + search.movement = Some(SearchMovement::Previous); search.cursor = search.cursor.saturating_sub(1); self.initialised = false; self.dirty = true;