summaryrefslogtreecommitdiffstats
path: root/src/tree.rs
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-11-09 22:00:17 +0100
committerqkzk <qu3nt1n@gmail.com>2023-11-09 22:00:17 +0100
commitdf22bbdd3e5e176d97cfa742ca5c4667fd53af89 (patch)
treef7462d6596fc3ae584cbd92d6bc711bd59a32070 /src/tree.rs
parent77dc7c93cc17b68e87c02e9d4ea3c164f2bd6780 (diff)
refactor search
Diffstat (limited to 'src/tree.rs')
-rw-r--r--src/tree.rs33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/tree.rs b/src/tree.rs
index b00c37a..6aa0bd6 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -477,20 +477,33 @@ impl Tree {
let Some(current_index) = self.nodes.keys().position(|path| path == &self.selected) else {
unreachable!("selected should be in pos");
};
- if let Some(found_path) = self
- .nodes
+ let Some(found_path) = self.find_pattern(pattern, current_index) else {
+ return;
+ };
+ self.go(To::Path(found_path.to_owned().as_path()));
+ }
+
+ fn find_pattern(&self, pattern: &str, current_index: usize) -> Option<&PathBuf> {
+ if let Some(found_path) = self.find_path_from_index(current_index, pattern) {
+ Some(found_path)
+ } else if let Some(found_path) = self.find_path_from_top(pattern) {
+ Some(found_path)
+ } else {
+ None
+ }
+ }
+
+ fn find_path_from_top(&self, pattern: &str) -> Option<&PathBuf> {
+ self.nodes
.keys()
- .skip(current_index + 1)
.find(|path| path_filename_contains(path, pattern))
- {
- self.go(To::Path(found_path.to_owned().as_path()));
- } else if let Some(found_path) = self
- .nodes
+ }
+
+ fn find_path_from_index(&self, current_index: usize, pattern: &str) -> Option<&PathBuf> {
+ self.nodes
.keys()
+ .skip(current_index + 1)
.find(|path| path_filename_contains(path, pattern))
- {
- self.go(To::Path(found_path.to_owned().as_path()));
- };
}
/// Returns a navigable vector of `ColoredTriplet` and the index of selected file