summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2024-01-17 20:39:21 +0100
committerqkzk <qu3nt1n@gmail.com>2024-01-17 20:39:21 +0100
commit665de276fb7c1780bfe5191477e5c5fe64d4ab8c (patch)
treee58cefccf26e17fc1f484ed5b8eb0b721df9c874 /src
parent7d70b2267e95a8e42f4e1b84d7bc9c269a879371 (diff)
WIP: all search methods moved to search. All use the same algorithm
Diffstat (limited to 'src')
-rw-r--r--src/app/tab.rs46
-rw-r--r--src/event/event_exec.rs12
-rw-r--r--src/modes/edit/leave_mode.rs2
-rw-r--r--src/modes/edit/search.rs65
4 files changed, 64 insertions, 61 deletions
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: &regex::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: &regex::Regex,
- current_index: usize,
- ) -> Option<usize> {
- 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: &regex::Regex,
- current_index: usize,
- ) -> Option<usize> {
- 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: &regex::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<usize> {
+ 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: &regex::Regex, current_index: usize) -> Option<usize> {
+ 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: &regex::Regex, current_index: usize) -> Option<usize> {
+ 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