diff --git a/yazi-core/src/tab/commands/find_arrow.rs b/yazi-core/src/tab/commands/find_arrow.rs index cc621ad3..925ab260 100644 --- a/yazi-core/src/tab/commands/find_arrow.rs +++ b/yazi-core/src/tab/commands/find_arrow.rs @@ -18,7 +18,7 @@ impl Tab { return; }; - render!(finder.catchup(&self.current.files)); + render!(finder.catchup(&self.current)); if opt.prev { finder.prev(&self.current.files, self.current.cursor, false).map(|s| self.arrow(s)); } else { diff --git a/yazi-core/src/tab/finder.rs b/yazi-core/src/tab/finder.rs index 8f0e02f3..534a6a85 100644 --- a/yazi-core/src/tab/finder.rs +++ b/yazi-core/src/tab/finder.rs @@ -2,17 +2,29 @@ use std::collections::HashMap; use anyhow::Result; use yazi_fs::{Files, Filter, FilterCase}; -use yazi_shared::url::Url; +use yazi_shared::url::{Url, Urn, UrnBuf}; + +use crate::tab::Folder; pub struct Finder { - pub filter: Filter, - matched: HashMap, - revision: u64, + pub filter: Filter, + pub matched: HashMap, + lock: FinderLock, +} + +#[derive(Default)] +struct FinderLock { + cwd: Url, + revision: u64, } impl Finder { pub(super) fn new(s: &str, case: FilterCase) -> Result { - Ok(Self { filter: Filter::new(s, case)?, matched: Default::default(), revision: 0 }) + Ok(Self { + filter: Filter::new(s, case)?, + matched: Default::default(), + lock: Default::default(), + }) } pub(super) fn prev(&self, files: &Files, cursor: usize, include: bool) -> Option { @@ -35,19 +47,19 @@ impl Finder { None } - pub(super) fn catchup(&mut self, files: &Files) -> bool { - if self.revision == files.revision { + pub(super) fn catchup(&mut self, folder: &Folder) -> bool { + if self.lock == *folder { return false; } self.matched.clear(); let mut i = 0u8; - for file in files.iter() { + for file in folder.files.iter() { if !self.filter.matches(file.name()) { continue; } - self.matched.insert(file.url_owned(), i); + self.matched.insert(file.urn_owned(), i); if self.matched.len() > 99 { break; } @@ -55,15 +67,27 @@ impl Finder { i += 1; } - self.revision = files.revision; + self.lock = folder.into(); true } } impl Finder { #[inline] - pub fn matched(&self) -> &HashMap { &self.matched } - - #[inline] - pub fn matched_idx(&self, url: &Url) -> Option { self.matched.get(url).copied() } + pub fn matched_idx(&self, folder: &Folder, urn: &Urn) -> Option { + if self.lock == *folder { self.matched.get(urn).copied() } else { None } + } +} + +// --- Lock +impl From<&Folder> for FinderLock { + fn from(value: &Folder) -> Self { + Self { cwd: value.url.clone(), revision: value.files.revision } + } +} + +impl PartialEq for FinderLock { + fn eq(&self, other: &Folder) -> bool { + self.revision == other.files.revision && self.cwd == other.url + } } diff --git a/yazi-fm/src/lives/file.rs b/yazi-fm/src/lives/file.rs index 990189d1..522e5177 100644 --- a/yazi-fm/src/lives/file.rs +++ b/yazi-fm/src/lives/file.rs @@ -125,11 +125,11 @@ impl UserData for File { return Ok(None); }; - let Some(idx) = finder.matched_idx(&me.url) else { + let Some(idx) = finder.matched_idx(&me.folder, me.urn()) else { return Ok(None); }; - Some(lua.create_sequence_from([idx.into_lua(lua)?, finder.matched().len().into_lua(lua)?])) + Some(lua.create_sequence_from([idx.into_lua(lua)?, finder.matched.len().into_lua(lua)?])) .transpose() }) }); diff --git a/yazi-fs/src/filter.rs b/yazi-fs/src/filter.rs index 290b0067..b0a55cf1 100644 --- a/yazi-fs/src/filter.rs +++ b/yazi-fs/src/filter.rs @@ -23,11 +23,13 @@ impl Filter { } #[inline] - pub fn matches(&self, name: &OsStr) -> bool { self.regex.is_match(name.as_encoded_bytes()) } + pub fn matches(&self, name: impl AsRef) -> bool { + self.regex.is_match(name.as_ref().as_encoded_bytes()) + } #[inline] - pub fn highlighted(&self, name: &OsStr) -> Option>> { - self.regex.find(name.as_encoded_bytes()).map(|m| vec![m.range()]) + pub fn highlighted(&self, name: impl AsRef) -> Option>> { + self.regex.find(name.as_ref().as_encoded_bytes()).map(|m| vec![m.range()]) } } diff --git a/yazi-fs/src/mounts/macos.rs b/yazi-fs/src/mounts/macos.rs index 82dcebd5..9d080da4 100644 --- a/yazi-fs/src/mounts/macos.rs +++ b/yazi-fs/src/mounts/macos.rs @@ -44,7 +44,7 @@ impl Partitions { if mem::replace(&mut me.write().need_update, true) { return; } - Self::update(me.clone(), move || _ = cb()); + Self::update(me.clone(), cb); }); Box::into_raw(Box::new(boxed)) as *mut c_void };