From 73b9c62944a81dfc552e9c77ac07cf3cc3db12cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BB=A9c=20To=C3=A0n?= <33489972+ndtoan96@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:38:00 +0700 Subject: [PATCH] feat: loop through to find (#234) --- core/src/manager/finder.rs | 63 +++++++++----------------------------- core/src/manager/tab.rs | 20 ++++++++---- 2 files changed, 28 insertions(+), 55 deletions(-) diff --git a/core/src/manager/finder.rs b/core/src/manager/finder.rs index 414de6b20..0544e1397 100644 --- a/core/src/manager/finder.rs +++ b/core/src/manager/finder.rs @@ -17,59 +17,24 @@ impl Finder { Ok(Self { query: Regex::new(s)?, matched: Default::default(), version: 0 }) } - pub(super) fn arrow(&self, files: &Files, cursor: usize, prev: bool) -> Option { - if prev { - files - .iter() - .take(cursor) - .rev() - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| -(i as isize) - 1) - } else { - files - .iter() - .skip(cursor + 1) - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| i as isize + 1) + pub(super) fn prev(&self, files: &Files, cursor: usize, include: bool) -> Option { + for i in !include as usize..files.len() { + let idx = (cursor + files.len() - i) % files.len(); + if files[idx].name().is_some_and(|n| self.matches(n)) { + return Some(idx as isize - cursor as isize); + } } + None } - pub(super) fn ring(&self, files: &Files, cursor: usize, prev: bool) -> Option { - if prev { - files - .iter() - .take(cursor + 1) - .rev() - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| -(i as isize)) - .or_else(|| { - files - .iter() - .skip(cursor + 1) - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| i as isize + 1) - }) - } else { - files - .iter() - .skip(cursor) - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| i as isize) - .or_else(|| { - files - .iter() - .take(cursor) - .rev() - .enumerate() - .find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) - .map(|(i, _)| -(i as isize) - 1) - }) + pub(super) fn next(&self, files: &Files, cursor: usize, include: bool) -> Option { + for i in !include as usize..files.len() { + let idx = (cursor + i) % files.len(); + if files[idx].name().is_some_and(|n| self.matches(n)) { + return Some(idx as isize - cursor as isize); + } } + None } pub(super) fn catchup(&mut self, files: &Files) -> bool { diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index 295e8c65e..a8a28cebc 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -243,7 +243,13 @@ impl Tab { return false; }; - if let Some(step) = finder.ring(&self.current.files, self.current.cursor(), prev) { + let step = if prev { + finder.prev(&self.current.files, self.current.cursor(), true) + } else { + finder.next(&self.current.files, self.current.cursor(), true) + }; + + if let Some(step) = step { self.arrow(step.into()); } @@ -274,12 +280,14 @@ impl Tab { return false; }; - let mut b = finder.catchup(&self.current.files); - if let Some(step) = finder.arrow(&self.current.files, self.current.cursor(), prev) { - b |= self.arrow(step.into()); - } + let b = finder.catchup(&self.current.files); + let step = if prev { + finder.prev(&self.current.files, self.current.cursor(), false) + } else { + finder.next(&self.current.files, self.current.cursor(), false) + }; - b + b | step.is_some_and(|s| self.arrow(s.into())) } pub fn search(&mut self, grep: bool) -> bool {