From 665de276fb7c1780bfe5191477e5c5fe64d4ab8c Mon Sep 17 00:00:00 2001 From: qkzk Date: Wed, 17 Jan 2024 20:39:21 +0100 Subject: WIP: all search methods moved to search. All use the same algorithm --- src/app/tab.rs | 46 ------------------------------- src/event/event_exec.rs | 12 +++++--- src/modes/edit/leave_mode.rs | 2 +- src/modes/edit/search.rs | 65 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 64 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/app/tab.rs b/src/app/tab.rs index 4f03311..875ade8 100644 --- a/src/app/tab.rs +++ b/src/app/tab.rs @@ -737,50 +737,4 @@ impl Tab { self.tree.go(To::Path(&path)); Ok(()) } - - /// Search in current directory for an file whose name contains `searched_name`, - /// from a starting position `next_index`. - /// We search forward from that position and start again from top if nothing is found. - /// We move the selection to the first matching file. - pub fn search_from(&mut self, searched_pattern: ®ex::Regex, current_index: usize) { - if let Some(found_index) = self.search_from_index(searched_pattern, current_index) { - self.go_to_index(found_index); - } else if let Some(found_index) = self.search_from_top(searched_pattern, current_index) { - self.go_to_index(found_index); - } - } - - /// Search a file by filename from given index, moving down - fn search_from_index( - &self, - searched_pattern: ®ex::Regex, - current_index: usize, - ) -> Option { - for (index, file) in self.directory.enumerate().skip(current_index) { - if searched_pattern.is_match(&file.filename) { - return Some(index); - } - } - None - } - - /// Search a file by filename from first line, moving down - fn search_from_top( - &self, - searched_pattern: ®ex::Regex, - current_index: usize, - ) -> Option { - for (index, file) in self.directory.enumerate().take(current_index) { - if searched_pattern.is_match(&file.filename) { - return Some(index); - } - } - None - } - - /// Search the next matching file in display directory - pub fn normal_search_next(&mut self, search_pattern: ®ex::Regex) { - let next_index = (self.directory.index + 1) % self.directory.content.len(); - self.search_from(search_pattern, next_index); - } } diff --git a/src/event/event_exec.rs b/src/event/event_exec.rs index 0852b41..675e827 100644 --- a/src/event/event_exec.rs +++ b/src/event/event_exec.rs @@ -820,12 +820,16 @@ impl EventAction { return Ok(()); } let tab = &mut status.tabs[status.index]; - let Some(re) = tab.search.regex.clone() else { - return Ok(()); - }; + // let Some(re) = tab.search.regex.clone() else { + // return Ok(()); + // }; match tab.display_mode { Display::Tree => tab.search.tree(&mut tab.tree), - Display::Directory => tab.normal_search_next(&re), + Display::Directory => { + if let Some(index) = tab.search.directory_search_next(tab) { + tab.go_to_index(index) + } + } Display::Preview => { return Ok(()); } diff --git a/src/modes/edit/leave_mode.rs b/src/modes/edit/leave_mode.rs index 524f776..d96efb9 100644 --- a/src/modes/edit/leave_mode.rs +++ b/src/modes/edit/leave_mode.rs @@ -255,7 +255,7 @@ impl LeaveMode { status.current_tab_mut().search = Search::default(); return Ok(()); } - let Ok(mut search) = Search::new(searched) else { + let Ok(search) = Search::new(searched) else { status.current_tab_mut().search = Search::default(); return Ok(()); }; diff --git a/src/modes/edit/search.rs b/src/modes/edit/search.rs index 132fcb6..4f8e42c 100644 --- a/src/modes/edit/search.rs +++ b/src/modes/edit/search.rs @@ -25,13 +25,13 @@ impl Search { Ok(()) } - pub fn leave(&mut self, status: &mut Status) -> Result<()> { + pub fn leave(&self, status: &mut Status) -> Result<()> { match status.current_tab().display_mode { Display::Tree => { self.tree(&mut status.current_tab_mut().tree); } Display::Directory => { - self.directory(status.current_tab_mut())?; + self.directory(status.current_tab_mut()); } Display::Flagged => self.flagged(&mut status.menu.flagged), _ => (), @@ -39,20 +39,65 @@ impl Search { status.update_second_pane_for_preview() } - pub fn directory(&mut self, tab: &mut Tab) -> Result<()> { - if let Some(re) = &self.regex { - let next_index = tab.directory.index; - tab.search_from(re, next_index); + /// Search in current directory for an file whose name contains `searched_name`, + /// from a starting position `next_index`. + /// We search forward from that position and start again from top if nothing is found. + /// We move the selection to the first matching file. + pub fn directory(&self, tab: &mut Tab) { + let Some(re) = &self.regex else { + return; + }; + let current_index = tab.directory.index; + let next_index = if let Some(index) = Self::search_from_index(tab, re, current_index) { + index + } else if let Some(index) = Self::search_from_top(tab, re, current_index) { + index + } else { + return; + }; + tab.go_to_index(next_index); + } + + pub fn directory_search_next(&self, tab: &Tab) -> Option { + let Some(re) = &self.regex else { + return None; + }; + let current_index = tab.directory.index; + if let Some(index) = Self::search_from_index(tab, re, current_index) { + Some(index) + } else if let Some(index) = Self::search_from_top(tab, re, current_index) { + Some(index) + } else { + None } - Ok(()) } - pub fn tree(&mut self, tree: &mut Tree) { + /// Search a file by filename from given index, moving down + fn search_from_index(tab: &Tab, re: ®ex::Regex, current_index: usize) -> Option { + for (index, file) in tab.directory.enumerate().skip(current_index) { + if re.is_match(&file.filename) { + return Some(index); + } + } + None + } + + /// Search a file by filename from first line, moving down + fn search_from_top(tab: &Tab, re: ®ex::Regex, current_index: usize) -> Option { + for (index, file) in tab.directory.enumerate().take(current_index) { + if re.is_match(&file.filename) { + return Some(index); + } + } + None + } + + pub fn tree(&self, tree: &mut Tree) { let path = self.tree_find_next_path(tree).to_owned(); tree.go(To::Path(&path)); } - fn tree_find_next_path<'a>(&mut self, tree: &'a mut Tree) -> &'a std::path::Path { + fn tree_find_next_path<'a>(&self, tree: &'a mut Tree) -> &'a std::path::Path { if let Some(pattern) = &self.regex { for line in tree .displayable() @@ -84,7 +129,7 @@ impl Search { tree.selected_path() } - pub fn flagged(&mut self, flagged: &mut Flagged) { + pub fn flagged(&self, flagged: &mut Flagged) { if let Some(re) = &self.regex { let position = if let Some(pos) = flagged .content -- cgit v1.2.3