mirror of
https://github.com/sxyazi/yazi.git
synced 2024-09-11 18:38:19 +03:00
Fix edge cases; support regex filter
This commit is contained in:
parent
be51ad5374
commit
99dc0e389c
@ -1,4 +1,4 @@
|
||||
use std::{env, path::Path, sync::{atomic::Ordering, Arc}};
|
||||
use std::{env, path::Path, sync::Arc};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use ratatui::prelude::Rect;
|
||||
|
@ -5,7 +5,7 @@ use tokio::{fs, select, sync::mpsc::{self, UnboundedReceiver}};
|
||||
use yazi_config::{manager::SortBy, MANAGER};
|
||||
use yazi_shared::fs::{File, Url, FILES_TICKET};
|
||||
|
||||
use super::FilesSorter;
|
||||
use super::{FilesSorter, Filter};
|
||||
|
||||
pub struct Files {
|
||||
hidden: Vec<File>,
|
||||
@ -18,7 +18,7 @@ pub struct Files {
|
||||
selected: BTreeSet<Url>,
|
||||
|
||||
sorter: FilesSorter,
|
||||
filter: Option<String>,
|
||||
filter: Option<Filter>,
|
||||
show_hidden: bool,
|
||||
}
|
||||
|
||||
@ -138,11 +138,7 @@ impl Files {
|
||||
self.ticket = FILES_TICKET.fetch_add(1, Ordering::Relaxed);
|
||||
self.revision += 1;
|
||||
|
||||
(self.hidden, self.items) = if self.show_hidden {
|
||||
(vec![], files)
|
||||
} else {
|
||||
files.into_iter().partition(|f| f.is_hidden())
|
||||
};
|
||||
(self.hidden, self.items) = self.split_files(files);
|
||||
}
|
||||
|
||||
pub fn update_part(&mut self, files: Vec<File>, ticket: u64) {
|
||||
@ -152,14 +148,9 @@ impl Files {
|
||||
}
|
||||
|
||||
self.revision += 1;
|
||||
if self.show_hidden {
|
||||
self.hidden.clear();
|
||||
self.items.extend(files);
|
||||
} else {
|
||||
let (hidden, items): (Vec<_>, Vec<_>) = files.into_iter().partition(|f| f.is_hidden());
|
||||
self.hidden.extend(hidden);
|
||||
self.items.extend(items);
|
||||
}
|
||||
let (hidden, items) = self.split_files(files);
|
||||
self.hidden.extend(hidden);
|
||||
self.items.extend(items);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -202,12 +193,7 @@ impl Files {
|
||||
};
|
||||
}
|
||||
|
||||
let (hidden, items) = if self.show_hidden {
|
||||
(vec![], files)
|
||||
} else {
|
||||
files.into_iter().partition(|f| f.is_hidden())
|
||||
};
|
||||
|
||||
let (hidden, items) = self.split_files(files);
|
||||
if !items.is_empty() {
|
||||
go!(self.items, items);
|
||||
}
|
||||
@ -233,8 +219,15 @@ impl Files {
|
||||
};
|
||||
}
|
||||
|
||||
let (hidden, items) =
|
||||
if self.show_hidden { (vec![], urls) } else { urls.into_iter().partition(|u| u.is_hidden()) };
|
||||
let (hidden, items) = if let Some(filter) = &self.filter {
|
||||
urls.into_iter().partition(|u| {
|
||||
(u.is_hidden() && !self.show_hidden) || !u.file_name().is_some_and(|s| filter.matches(s))
|
||||
})
|
||||
} else if self.show_hidden {
|
||||
(vec![], urls)
|
||||
} else {
|
||||
urls.into_iter().partition(|u| u.is_hidden())
|
||||
};
|
||||
|
||||
if !items.is_empty() {
|
||||
go!(self.items, items);
|
||||
@ -244,7 +237,10 @@ impl Files {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_updating(&mut self, files: BTreeMap<Url, File>) -> [BTreeMap<Url, File>; 2] {
|
||||
pub fn update_updating(
|
||||
&mut self,
|
||||
files: BTreeMap<Url, File>,
|
||||
) -> (BTreeMap<Url, File>, BTreeMap<Url, File>) {
|
||||
if files.is_empty() {
|
||||
return Default::default();
|
||||
}
|
||||
@ -266,7 +262,12 @@ impl Files {
|
||||
};
|
||||
}
|
||||
|
||||
let (mut hidden, mut items) = if self.show_hidden {
|
||||
let (mut hidden, mut items) = if let Some(filter) = &self.filter {
|
||||
files.into_iter().partition(|(_, f)| {
|
||||
(f.is_hidden() && !self.show_hidden)
|
||||
|| !f.url.file_name().is_some_and(|s| filter.matches(s))
|
||||
})
|
||||
} else if self.show_hidden {
|
||||
(BTreeMap::new(), files)
|
||||
} else {
|
||||
files.into_iter().partition(|(_, f)| f.is_hidden())
|
||||
@ -278,7 +279,7 @@ impl Files {
|
||||
if !hidden.is_empty() {
|
||||
go!(self.hidden, hidden);
|
||||
}
|
||||
[hidden, items]
|
||||
(hidden, items)
|
||||
}
|
||||
|
||||
pub fn update_upserting(&mut self, files: BTreeMap<Url, File>) {
|
||||
@ -286,7 +287,7 @@ impl Files {
|
||||
return;
|
||||
}
|
||||
|
||||
let [hidden, items] = self.update_updating(files);
|
||||
let (hidden, items) = self.update_updating(files);
|
||||
if hidden.is_empty() && items.is_empty() {
|
||||
return;
|
||||
}
|
||||
@ -309,6 +310,19 @@ impl Files {
|
||||
self.sorter.sort(&mut self.items, &self.sizes);
|
||||
true
|
||||
}
|
||||
|
||||
fn split_files(&self, files: impl IntoIterator<Item = File>) -> (Vec<File>, Vec<File>) {
|
||||
if let Some(filter) = &self.filter {
|
||||
files.into_iter().partition(|f| {
|
||||
(f.is_hidden() && !self.show_hidden)
|
||||
|| !f.url.file_name().is_some_and(|s| filter.matches(s))
|
||||
})
|
||||
} else if self.show_hidden {
|
||||
(vec![], files.into_iter().collect())
|
||||
} else {
|
||||
files.into_iter().partition(|f| f.is_hidden())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Files {
|
||||
@ -377,47 +391,26 @@ impl Files {
|
||||
}
|
||||
|
||||
// --- Filter
|
||||
pub fn set_filter(&mut self, keyword: &str) -> bool {
|
||||
if keyword == self.filter.as_deref().unwrap_or_default() {
|
||||
pub fn set_filter(&mut self, filter: Option<Filter>) -> bool {
|
||||
if self.filter == filter {
|
||||
return false;
|
||||
}
|
||||
|
||||
if keyword.is_empty() {
|
||||
let (hidden, items) = if self.show_hidden {
|
||||
(vec![], mem::take(&mut self.hidden))
|
||||
} else {
|
||||
mem::take(&mut self.hidden).into_iter().partition(|f| f.is_hidden())
|
||||
};
|
||||
self.filter = filter;
|
||||
if self.filter.is_none() {
|
||||
let take = mem::take(&mut self.hidden);
|
||||
let (hidden, items) = self.split_files(take);
|
||||
|
||||
self.hidden = hidden;
|
||||
if !items.is_empty() {
|
||||
self.items.extend(items);
|
||||
self.sorter.sort(&mut self.items, &self.sizes);
|
||||
}
|
||||
|
||||
self.filter = None;
|
||||
return true;
|
||||
}
|
||||
|
||||
let keyword = keyword.to_ascii_lowercase();
|
||||
let it = mem::take(&mut self.items).into_iter().chain(mem::take(&mut self.hidden));
|
||||
(self.items, self.hidden) = if self.show_hidden {
|
||||
it.partition(|f| {
|
||||
f.url
|
||||
.file_name()
|
||||
.is_some_and(|s| s.to_string_lossy().to_ascii_lowercase().contains(&keyword))
|
||||
})
|
||||
} else {
|
||||
it.partition(|f| {
|
||||
!f.is_hidden()
|
||||
&& f
|
||||
.url
|
||||
.file_name()
|
||||
.is_some_and(|s| s.to_string_lossy().to_ascii_lowercase().contains(&keyword))
|
||||
})
|
||||
};
|
||||
|
||||
self.filter = Some(keyword);
|
||||
(self.hidden, self.items) = self.split_files(it);
|
||||
self.sorter.sort(&mut self.items, &self.sizes);
|
||||
true
|
||||
}
|
||||
@ -435,12 +428,12 @@ impl Files {
|
||||
return;
|
||||
}
|
||||
|
||||
let take =
|
||||
if self.show_hidden { mem::take(&mut self.hidden) } else { mem::take(&mut self.items) };
|
||||
let (hidden, items) = self.split_files(take);
|
||||
|
||||
self.revision += 1;
|
||||
if self.show_hidden {
|
||||
self.items.append(&mut self.hidden);
|
||||
} else {
|
||||
let items = mem::take(&mut self.items);
|
||||
(self.hidden, self.items) = items.into_iter().partition(|f| f.is_hidden());
|
||||
}
|
||||
self.hidden.extend(hidden);
|
||||
self.items.extend(items);
|
||||
}
|
||||
}
|
||||
|
53
yazi-core/src/folder/filter.rs
Normal file
53
yazi-core/src/folder/filter.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use std::{ffi::OsStr, ops::Range};
|
||||
|
||||
use anyhow::Result;
|
||||
use regex::bytes::{Regex, RegexBuilder};
|
||||
use yazi_shared::event::Exec;
|
||||
|
||||
pub struct Filter {
|
||||
raw: String,
|
||||
regex: Regex,
|
||||
}
|
||||
|
||||
impl PartialEq for Filter {
|
||||
fn eq(&self, other: &Self) -> bool { self.raw == other.raw }
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
pub fn new(s: &str, case: FilterCase) -> Result<Self> {
|
||||
let regex = match case {
|
||||
FilterCase::Smart => {
|
||||
let uppercase = s.chars().any(|c| c.is_uppercase());
|
||||
RegexBuilder::new(s).case_insensitive(!uppercase).build()?
|
||||
}
|
||||
FilterCase::Sensitive => Regex::new(s)?,
|
||||
FilterCase::Insensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
|
||||
};
|
||||
Ok(Self { raw: s.to_owned(), regex })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn matches(&self, name: &OsStr) -> bool { self.regex.is_match(name.as_encoded_bytes()) }
|
||||
|
||||
#[inline]
|
||||
pub fn highlighted(&self, name: &OsStr) -> Option<Vec<Range<usize>>> {
|
||||
self.regex.find(name.as_encoded_bytes()).map(|m| vec![m.range()])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum FilterCase {
|
||||
Smart,
|
||||
Sensitive,
|
||||
Insensitive,
|
||||
}
|
||||
|
||||
impl From<&Exec> for FilterCase {
|
||||
fn from(e: &Exec) -> Self {
|
||||
match (e.named.contains_key("smart"), e.named.contains_key("insensitive")) {
|
||||
(true, _) => Self::Smart,
|
||||
(_, false) => Self::Sensitive,
|
||||
(_, true) => Self::Insensitive,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
mod files;
|
||||
mod filter;
|
||||
mod folder;
|
||||
mod sorter;
|
||||
|
||||
pub use files::*;
|
||||
pub use filter::*;
|
||||
pub use folder::*;
|
||||
pub use sorter::*;
|
||||
|
@ -45,7 +45,7 @@ impl Tab {
|
||||
fn escape_select(&mut self) -> bool { self.select_all(Some(false)) }
|
||||
|
||||
#[inline]
|
||||
fn escape_filter(&mut self) -> bool { self.current.files.set_filter("") }
|
||||
fn escape_filter(&mut self) -> bool { self.current.files.set_filter(None) }
|
||||
|
||||
#[inline]
|
||||
fn escape_search(&mut self) -> bool { self.search_stop() }
|
||||
|
@ -5,18 +5,22 @@ use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
|
||||
use yazi_config::popup::InputCfg;
|
||||
use yazi_shared::{emit, event::Exec, Debounce, InputError, Layer};
|
||||
|
||||
use crate::{input::Input, tab::Tab};
|
||||
use crate::{folder::{Filter, FilterCase}, input::Input, tab::Tab};
|
||||
|
||||
pub struct Opt<'a> {
|
||||
query: Option<&'a str>,
|
||||
case: FilterCase,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Exec> for Opt<'a> {
|
||||
fn from(e: &'a Exec) -> Self { Self { query: e.args.first().map(|s| s.as_str()) } }
|
||||
fn from(e: &'a Exec) -> Self {
|
||||
Self { query: e.args.first().map(|s| s.as_str()), case: e.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Tab {
|
||||
pub fn filter<'a>(&mut self, _: impl Into<Opt<'a>>) -> bool {
|
||||
pub fn filter<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool {
|
||||
let opt = opt.into() as Opt;
|
||||
tokio::spawn(async move {
|
||||
let rx = Input::_show(InputCfg::filter());
|
||||
|
||||
@ -24,7 +28,13 @@ impl Tab {
|
||||
pin!(rx);
|
||||
|
||||
while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await {
|
||||
emit!(Call(Exec::call("filter_do", vec![s]).vec(), Layer::Manager));
|
||||
emit!(Call(
|
||||
Exec::call("filter_do", vec![s])
|
||||
.with_bool("smart", opt.case == FilterCase::Smart)
|
||||
.with_bool("insensitive", opt.case == FilterCase::Insensitive)
|
||||
.vec(),
|
||||
Layer::Manager
|
||||
));
|
||||
}
|
||||
});
|
||||
false
|
||||
@ -36,8 +46,16 @@ impl Tab {
|
||||
return false;
|
||||
};
|
||||
|
||||
let filter = if query.is_empty() {
|
||||
None
|
||||
} else if let Ok(f) = Filter::new(query, opt.case) {
|
||||
Some(f)
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let hovered = self.current.hovered().map(|f| f.url());
|
||||
if !self.current.files.set_filter(query) {
|
||||
if !self.current.files.set_filter(filter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,12 @@ use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
|
||||
use yazi_config::popup::InputCfg;
|
||||
use yazi_shared::{emit, event::Exec, Debounce, InputError, Layer};
|
||||
|
||||
use crate::{input::Input, tab::{Finder, FinderCase, Tab}};
|
||||
use crate::{folder::FilterCase, input::Input, tab::{Finder, Tab}};
|
||||
|
||||
pub struct Opt<'a> {
|
||||
query: Option<&'a str>,
|
||||
prev: bool,
|
||||
case: FinderCase,
|
||||
case: FilterCase,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Exec> for Opt<'a> {
|
||||
@ -18,11 +18,7 @@ impl<'a> From<&'a Exec> for Opt<'a> {
|
||||
Self {
|
||||
query: e.args.first().map(|s| s.as_str()),
|
||||
prev: e.named.contains_key("previous"),
|
||||
case: match (e.named.contains_key("smart"), e.named.contains_key("insensitive")) {
|
||||
(true, _) => FinderCase::Smart,
|
||||
(_, false) => FinderCase::Sensitive,
|
||||
(_, true) => FinderCase::Insensitive,
|
||||
},
|
||||
case: e.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -48,8 +44,8 @@ impl Tab {
|
||||
emit!(Call(
|
||||
Exec::call("find_do", vec![s])
|
||||
.with_bool("previous", opt.prev)
|
||||
.with_bool("smart", opt.case == FinderCase::Smart)
|
||||
.with_bool("insensitive", opt.case == FinderCase::Insensitive)
|
||||
.with_bool("smart", opt.case == FilterCase::Smart)
|
||||
.with_bool("insensitive", opt.case == FilterCase::Insensitive)
|
||||
.vec(),
|
||||
Layer::Manager
|
||||
));
|
||||
@ -63,10 +59,16 @@ impl Tab {
|
||||
let Some(query) = opt.query else {
|
||||
return false;
|
||||
};
|
||||
if query.is_empty() {
|
||||
return self.escape(super::escape::Opt::FIND);
|
||||
}
|
||||
|
||||
let Ok(finder) = Finder::new(query, opt.case) else {
|
||||
return false;
|
||||
};
|
||||
if matches!(&self.finder, Some(f) if f.filter == finder.filter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let step = if opt.prev {
|
||||
finder.prev(&self.current.files, self.current.cursor, true)
|
||||
|
@ -1,41 +1,25 @@
|
||||
use std::{collections::BTreeMap, ffi::OsStr, ops::Range};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use anyhow::Result;
|
||||
use regex::bytes::{Regex, RegexBuilder};
|
||||
use yazi_shared::fs::Url;
|
||||
|
||||
use crate::folder::Files;
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum FinderCase {
|
||||
Smart,
|
||||
Sensitive,
|
||||
Insensitive,
|
||||
}
|
||||
use crate::folder::{Files, Filter, FilterCase};
|
||||
|
||||
pub struct Finder {
|
||||
query: Regex,
|
||||
matched: BTreeMap<Url, u8>,
|
||||
revision: u64,
|
||||
pub filter: Filter,
|
||||
matched: BTreeMap<Url, u8>,
|
||||
revision: u64,
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> {
|
||||
let query = match case {
|
||||
FinderCase::Smart => {
|
||||
let uppercase = s.chars().any(|c| c.is_uppercase());
|
||||
RegexBuilder::new(s).case_insensitive(!uppercase).build()?
|
||||
}
|
||||
FinderCase::Sensitive => Regex::new(s)?,
|
||||
FinderCase::Insensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
|
||||
};
|
||||
Ok(Self { query, matched: Default::default(), revision: 0 })
|
||||
pub(super) fn new(s: &str, case: FilterCase) -> Result<Self> {
|
||||
Ok(Self { filter: Filter::new(s, case)?, matched: Default::default(), revision: 0 })
|
||||
}
|
||||
|
||||
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)) {
|
||||
if files[idx].name().is_some_and(|n| self.filter.matches(n)) {
|
||||
return Some(idx as isize - cursor as isize);
|
||||
}
|
||||
}
|
||||
@ -45,7 +29,7 @@ impl Finder {
|
||||
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)) {
|
||||
if files[idx].name().is_some_and(|n| self.filter.matches(n)) {
|
||||
return Some(idx as isize - cursor as isize);
|
||||
}
|
||||
}
|
||||
@ -60,7 +44,7 @@ impl Finder {
|
||||
|
||||
let mut i = 0u8;
|
||||
for file in files.iter() {
|
||||
if file.name().map(|n| self.matches(n)) != Some(true) {
|
||||
if file.name().map(|n| self.filter.matches(n)) != Some(true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -75,15 +59,6 @@ impl Finder {
|
||||
self.revision = files.revision;
|
||||
true
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn matches(&self, name: &OsStr) -> bool { self.query.is_match(name.as_encoded_bytes()) }
|
||||
|
||||
/// Explode the name into three parts: head, body, tail.
|
||||
#[inline]
|
||||
pub fn highlighted(&self, name: &OsStr) -> Option<Vec<Range<usize>>> {
|
||||
self.query.find(name.as_encoded_bytes()).map(|m| vec![m.range()])
|
||||
}
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
|
@ -147,7 +147,7 @@ impl<'a, 'b> Folder<'a, 'b> {
|
||||
};
|
||||
|
||||
let file = me.borrow::<yazi_shared::fs::File>()?;
|
||||
let Some(h) = file.name().and_then(|n| finder.highlighted(n)) else {
|
||||
let Some(h) = file.name().and_then(|n| finder.filter.highlighted(n)) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user