diff --git a/core/src/manager/folder.rs b/core/src/manager/folder.rs index 6899d1f7..02f92965 100644 --- a/core/src/manager/folder.rs +++ b/core/src/manager/folder.rs @@ -106,9 +106,9 @@ impl Folder { pub fn hover(&mut self, url: &Url) -> bool { let new = self.files.position(url).unwrap_or(self.cursor); if new > self.cursor { - self.next(Step::from(new - self.cursor)) + self.next(Step::next(new - self.cursor)) } else { - self.prev(Step::from(self.cursor - new)) + self.prev(Step::prev(self.cursor - new)) } } diff --git a/core/src/step.rs b/core/src/step.rs index 5faa2459..153d637e 100644 --- a/core/src/step.rs +++ b/core/src/step.rs @@ -1,5 +1,6 @@ use std::{num::ParseIntError, str::FromStr}; +#[derive(Clone, Copy)] pub enum Step { Fixed(isize), Percent(i8), @@ -25,29 +26,28 @@ impl From for Step { fn from(n: isize) -> Self { Self::Fixed(n) } } -impl From for Step { - fn from(n: usize) -> Self { Self::Fixed(n as isize) } +impl Step { + #[inline] + pub fn prev(n: usize) -> Self { Self::Fixed(-(n as isize)) } + + #[inline] + pub fn next(n: usize) -> Self { Self::Fixed(n as isize) } } impl Step { #[inline] - fn fixed usize>(self, f: F) -> isize { - match self { + pub fn add usize>(self, pos: usize, f: F) -> usize { + let fixed = match self { Self::Fixed(n) => n, Self::Percent(0) => 0, Self::Percent(n) => n as isize * f() as isize / 100, - } - } - - #[inline] - pub fn add usize>(self, pos: usize, f: F) -> usize { - let fixed = self.fixed(f); + }; if fixed > 0 { pos + fixed as usize } else { pos.saturating_sub(fixed.unsigned_abs()) } } #[inline] - pub fn is_positive(&self) -> bool { - match *self { + pub fn is_positive(self) -> bool { + match self { Self::Fixed(n) => n > 0, Self::Percent(n) => n > 0, }