diff --git a/config/docs/yazi.md b/config/docs/yazi.md index 7e6f4255..0500fde2 100644 --- a/config/docs/yazi.md +++ b/config/docs/yazi.md @@ -81,8 +81,8 @@ rules = [ Available rule parameters are as follows: -- name: Glob expression for matching the file name -- mime: Glob expression for matching the MIME type +- 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. Case insensitive by default, add `\s` to the beginning to make it sensitive. - use: Opener name corresponding to the names in the opener section. ## tasks diff --git a/config/src/pattern.rs b/config/src/pattern.rs index 1b5bdc60..7a9d7c30 100644 --- a/config/src/pattern.rs +++ b/config/src/pattern.rs @@ -1,18 +1,26 @@ use std::path::Path; +use glob::MatchOptions; use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(try_from = "String")] pub struct Pattern { inner: glob::Pattern, + sensitive: bool, is_folder: bool, full_path: bool, } impl Pattern { #[inline] - pub fn matches(&self, str: impl AsRef) -> bool { self.inner.matches(str.as_ref()) } + pub fn matches(&self, str: impl AsRef) -> bool { + self.inner.matches_with(str.as_ref(), MatchOptions { + case_sensitive: self.sensitive, + require_literal_separator: false, + require_literal_leading_dot: false, + }) + } #[inline] pub fn match_path(&self, path: impl AsRef, is_folder: Option) -> bool { @@ -20,7 +28,7 @@ impl Pattern { let s = if self.full_path { path.to_str() } 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)) } @@ -30,11 +38,13 @@ impl TryFrom<&str> for Pattern { type Error = anyhow::Error; fn try_from(s: &str) -> Result { - let new = s.trim_end_matches('/'); + let a = s.trim_start_matches("\\s"); + let b = a.trim_end_matches('/'); Ok(Self { - inner: glob::Pattern::new(new)?, - is_folder: new.len() < s.len(), - full_path: new.contains('/'), + inner: glob::Pattern::new(b)?, + sensitive: a.len() < s.len(), + is_folder: b.len() < a.len(), + full_path: b.contains('/'), }) } }