From f4af594cd9f68cd100c633d41e0113a920f0bbf9 Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Mon, 24 Jul 2023 23:47:04 -0700 Subject: [PATCH] feat: simplify search generation, avoid using regex-pattern. update cli opts --- rmenu/Cargo.toml | 2 +- rmenu/src/main.rs | 7 ++++- rmenu/src/search.rs | 64 +++++++++++++++++++++++---------------------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/rmenu/Cargo.toml b/rmenu/Cargo.toml index c9e14bc..049c08c 100644 --- a/rmenu/Cargo.toml +++ b/rmenu/Cargo.toml @@ -13,7 +13,7 @@ env_logger = "0.10.0" heck = "0.4.1" keyboard-types = "0.6.2" log = "0.4.19" -regex = { version = "1.9.1", features = ["pattern"] } +regex = { version = "1.9.1" } rmenu-plugin = { version = "0.0.0", path = "../rmenu-plugin" } serde = { version = "1.0.171", features = ["derive"] } serde_json = "1.0.103" diff --git a/rmenu/src/main.rs b/rmenu/src/main.rs index 2e41343..1b04c9d 100644 --- a/rmenu/src/main.rs +++ b/rmenu/src/main.rs @@ -82,6 +82,8 @@ pub struct Args { format: Format, #[arg(short, long)] run: Vec, + #[arg(long)] + regex: Option, #[arg(short, long)] config: Option, #[arg(long)] @@ -184,7 +186,7 @@ impl Args { /// Load Application pub fn parse_app() -> Result { let args = Self::parse(); - let config = args.config()?; + let mut config = args.config()?; // load css files from settings let csspath = args.css.clone().unwrap_or_else(|| DEFAULT_CSS.to_owned()); let csspath = shellexpand::tilde(&csspath).to_string(); @@ -200,6 +202,9 @@ impl Args { true => args.load_sources(&config)?, false => args.load_default(&config)?, }; + // update configuration based on cli + config.use_icons = config.use_icons && entries.iter().any(|e| e.icon.is_some()); + config.search_regex = args.regex.unwrap_or(config.search_regex); // generate app object return Ok(App { css, diff --git a/rmenu/src/search.rs b/rmenu/src/search.rs index d9e2504..1ec0cef 100644 --- a/rmenu/src/search.rs +++ b/rmenu/src/search.rs @@ -4,47 +4,49 @@ use rmenu_plugin::Entry; use crate::config::Config; -macro_rules! search { - ($search:expr) => { - Box::new(move |entry: &Entry| { - if entry.name.contains($search) { - return true; - } - if let Some(comment) = entry.comment.as_ref() { - return comment.contains($search); - } - false - }) - }; - ($search:expr,$mod:ident) => { - Box::new(move |entry: &Entry| { - if entry.name.$mod().contains($search) { - return true; - } - if let Some(comment) = entry.comment.as_ref() { - return comment.$mod().contains($search); - } - false - }) - }; -} - /// Generate a new dynamic Search Function based on /// Configurtaion Settings and Search-String pub fn new_searchfn(cfg: &Config, search: &str) -> Box bool> { + // build regex search expression if cfg.search_regex { - let regex = RegexBuilder::new(search) + let rgx = RegexBuilder::new(search) .case_insensitive(cfg.ignore_case) .build(); - return match regex { - Ok(rgx) => search!(&rgx), - Err(_) => Box::new(|_| false), + let Ok(regex) = rgx else { + return Box::new(|_| false); }; + return Box::new(move |entry: &Entry| { + if regex.is_match(&entry.name) { + return true; + } + if let Some(comment) = entry.comment.as_ref() { + return regex.is_match(&comment); + } + false + }); } + // build case-insensitive search expression if cfg.ignore_case { let matchstr = search.to_lowercase(); - return search!(&matchstr, to_lowercase); + return Box::new(move |entry: &Entry| { + if entry.name.to_lowercase().contains(&matchstr) { + return true; + } + if let Some(comment) = entry.comment.as_ref() { + return comment.to_lowercase().contains(&matchstr); + } + false + }); } + // build standard normal string comparison function let matchstr = search.to_owned(); - return search!(&matchstr); + Box::new(move |entry: &Entry| { + if entry.name.contains(&matchstr) { + return true; + } + if let Some(comment) = entry.comment.as_ref() { + return comment.contains(&matchstr); + } + false + }) }