Skip to content

Commit

Permalink
feat: loop through to find (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndtoan96 authored Oct 1, 2023
1 parent b2d3e39 commit 73b9c62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 55 deletions.
63 changes: 14 additions & 49 deletions core/src/manager/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<isize> {
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<isize> {
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<isize> {
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<isize> {
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 {
Expand Down
20 changes: 14 additions & 6 deletions core/src/manager/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 73b9c62

Please sign in to comment.