mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: make the glob expr case insensitive by default, and prepend \s to make it sensitive (#156)
This commit is contained in:
parent
9c6b3c9d1d
commit
b62307dc4a
2 changed files with 18 additions and 8 deletions
|
|
@ -81,8 +81,8 @@ rules = [
|
||||||
|
|
||||||
Available rule parameters are as follows:
|
Available rule parameters are as follows:
|
||||||
|
|
||||||
- name: Glob expression for matching the file name
|
- name: Glob expression for matching the file name. Case insensitive by default, add `\s` to the beginning to make it sensitive.
|
||||||
- mime: Glob expression for matching the MIME type
|
- mime: Glob expression for matching the MIME type. Case insensitive by default, add `\s` to the beginning to make it sensitive.
|
||||||
- use: Opener name corresponding to the names in the opener section.
|
- use: Opener name corresponding to the names in the opener section.
|
||||||
|
|
||||||
## tasks
|
## tasks
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,26 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use glob::MatchOptions;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(try_from = "String")]
|
#[serde(try_from = "String")]
|
||||||
pub struct Pattern {
|
pub struct Pattern {
|
||||||
inner: glob::Pattern,
|
inner: glob::Pattern,
|
||||||
|
sensitive: bool,
|
||||||
is_folder: bool,
|
is_folder: bool,
|
||||||
full_path: bool,
|
full_path: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pattern {
|
impl Pattern {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn matches(&self, str: impl AsRef<str>) -> bool { self.inner.matches(str.as_ref()) }
|
pub fn matches(&self, str: impl AsRef<str>) -> bool {
|
||||||
|
self.inner.matches_with(str.as_ref(), MatchOptions {
|
||||||
|
case_sensitive: self.sensitive,
|
||||||
|
require_literal_separator: false,
|
||||||
|
require_literal_leading_dot: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn match_path(&self, path: impl AsRef<Path>, is_folder: Option<bool>) -> bool {
|
pub fn match_path(&self, path: impl AsRef<Path>, is_folder: Option<bool>) -> bool {
|
||||||
|
|
@ -20,7 +28,7 @@ impl Pattern {
|
||||||
let s = if self.full_path {
|
let s = if self.full_path {
|
||||||
path.to_str()
|
path.to_str()
|
||||||
} else {
|
} else {
|
||||||
path.file_name().and_then(|n| n.to_str()).or(path.to_str())
|
path.file_name().and_then(|n| n.to_str()).or_else(|| path.to_str())
|
||||||
};
|
};
|
||||||
is_folder.map_or(true, |f| f == self.is_folder) && s.map_or(false, |s| self.matches(s))
|
is_folder.map_or(true, |f| f == self.is_folder) && s.map_or(false, |s| self.matches(s))
|
||||||
}
|
}
|
||||||
|
|
@ -30,11 +38,13 @@ impl TryFrom<&str> for Pattern {
|
||||||
type Error = anyhow::Error;
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||||
let new = s.trim_end_matches('/');
|
let a = s.trim_start_matches("\\s");
|
||||||
|
let b = a.trim_end_matches('/');
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
inner: glob::Pattern::new(new)?,
|
inner: glob::Pattern::new(b)?,
|
||||||
is_folder: new.len() < s.len(),
|
sensitive: a.len() < s.len(),
|
||||||
full_path: new.contains('/'),
|
is_folder: b.len() < a.len(),
|
||||||
|
full_path: b.contains('/'),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue