mirror of
https://github.com/imgurbot12/rmenu.git
synced 2024-11-10 11:33:48 +01:00
feat: simplify search generation, avoid using regex-pattern. update cli opts
This commit is contained in:
parent
41c7c65528
commit
f4af594cd9
@ -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"
|
||||
|
@ -82,6 +82,8 @@ pub struct Args {
|
||||
format: Format,
|
||||
#[arg(short, long)]
|
||||
run: Vec<String>,
|
||||
#[arg(long)]
|
||||
regex: Option<bool>,
|
||||
#[arg(short, long)]
|
||||
config: Option<String>,
|
||||
#[arg(long)]
|
||||
@ -184,7 +186,7 @@ impl Args {
|
||||
/// Load Application
|
||||
pub fn parse_app() -> Result<App, RMenuError> {
|
||||
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,
|
||||
|
@ -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<dyn Fn(&Entry) -> 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
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user