forked from mirrors/rmenu
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"
|
heck = "0.4.1"
|
||||||
keyboard-types = "0.6.2"
|
keyboard-types = "0.6.2"
|
||||||
log = "0.4.19"
|
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" }
|
rmenu-plugin = { version = "0.0.0", path = "../rmenu-plugin" }
|
||||||
serde = { version = "1.0.171", features = ["derive"] }
|
serde = { version = "1.0.171", features = ["derive"] }
|
||||||
serde_json = "1.0.103"
|
serde_json = "1.0.103"
|
||||||
|
@ -82,6 +82,8 @@ pub struct Args {
|
|||||||
format: Format,
|
format: Format,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
run: Vec<String>,
|
run: Vec<String>,
|
||||||
|
#[arg(long)]
|
||||||
|
regex: Option<bool>,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
config: Option<String>,
|
config: Option<String>,
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
@ -184,7 +186,7 @@ impl Args {
|
|||||||
/// Load Application
|
/// Load Application
|
||||||
pub fn parse_app() -> Result<App, RMenuError> {
|
pub fn parse_app() -> Result<App, RMenuError> {
|
||||||
let args = Self::parse();
|
let args = Self::parse();
|
||||||
let config = args.config()?;
|
let mut config = args.config()?;
|
||||||
// load css files from settings
|
// load css files from settings
|
||||||
let csspath = args.css.clone().unwrap_or_else(|| DEFAULT_CSS.to_owned());
|
let csspath = args.css.clone().unwrap_or_else(|| DEFAULT_CSS.to_owned());
|
||||||
let csspath = shellexpand::tilde(&csspath).to_string();
|
let csspath = shellexpand::tilde(&csspath).to_string();
|
||||||
@ -200,6 +202,9 @@ impl Args {
|
|||||||
true => args.load_sources(&config)?,
|
true => args.load_sources(&config)?,
|
||||||
false => args.load_default(&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
|
// generate app object
|
||||||
return Ok(App {
|
return Ok(App {
|
||||||
css,
|
css,
|
||||||
|
@ -4,47 +4,49 @@ use rmenu_plugin::Entry;
|
|||||||
|
|
||||||
use crate::config::Config;
|
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
|
/// Generate a new dynamic Search Function based on
|
||||||
/// Configurtaion Settings and Search-String
|
/// Configurtaion Settings and Search-String
|
||||||
pub fn new_searchfn(cfg: &Config, search: &str) -> Box<dyn Fn(&Entry) -> bool> {
|
pub fn new_searchfn(cfg: &Config, search: &str) -> Box<dyn Fn(&Entry) -> bool> {
|
||||||
|
// build regex search expression
|
||||||
if cfg.search_regex {
|
if cfg.search_regex {
|
||||||
let regex = RegexBuilder::new(search)
|
let rgx = RegexBuilder::new(search)
|
||||||
.case_insensitive(cfg.ignore_case)
|
.case_insensitive(cfg.ignore_case)
|
||||||
.build();
|
.build();
|
||||||
return match regex {
|
let Ok(regex) = rgx else {
|
||||||
Ok(rgx) => search!(&rgx),
|
return Box::new(|_| false);
|
||||||
Err(_) => 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 {
|
if cfg.ignore_case {
|
||||||
let matchstr = search.to_lowercase();
|
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();
|
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