loop through search result

This commit is contained in:
Nguyen Duc Toan 2023-10-01 09:49:58 +07:00 committed by sxyazi
parent b2d3e39e96
commit 2965a23dbf
No known key found for this signature in database

View file

@ -7,7 +7,7 @@ use shared::Url;
use crate::files::Files; use crate::files::Files;
pub struct Finder { pub struct Finder {
query: Regex, query: Regex,
matched: BTreeMap<Url, u8>, matched: BTreeMap<Url, u8>,
version: u64, version: u64,
} }
@ -19,20 +19,21 @@ impl Finder {
pub(super) fn arrow(&self, files: &Files, cursor: usize, prev: bool) -> Option<isize> { pub(super) fn arrow(&self, files: &Files, cursor: usize, prev: bool) -> Option<isize> {
if prev { if prev {
files for i in 1..files.len() {
.iter() let index = (cursor + files.len() - i) % files.len();
.take(cursor) if files[index].name().is_some_and(|name| self.matches(name)) {
.rev() return Some((index as isize) - (cursor as isize));
.enumerate() }
.find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) }
.map(|(i, _)| -(i as isize) - 1) None
} else { } else {
files for i in 1..files.len() {
.iter() let index = (cursor + i) % files.len();
.skip(cursor + 1) if files[index].name().is_some_and(|name| self.matches(name)) {
.enumerate() return Some((index as isize) - (cursor as isize));
.find(|(_, f)| f.name().map_or(false, |n| self.matches(n))) }
.map(|(i, _)| i as isize + 1) }
None
} }
} }
@ -123,10 +124,14 @@ impl Finder {
impl Finder { impl Finder {
#[inline] #[inline]
pub fn matched(&self) -> &BTreeMap<Url, u8> { &self.matched } pub fn matched(&self) -> &BTreeMap<Url, u8> {
&self.matched
}
#[inline] #[inline]
pub fn has_matched(&self) -> bool { !self.matched.is_empty() } pub fn has_matched(&self) -> bool {
!self.matched.is_empty()
}
#[inline] #[inline]
pub fn matched_idx(&self, url: &Url) -> Option<u8> { pub fn matched_idx(&self, url: &Url) -> Option<u8> {