feat: exe_contains condition

This commit is contained in:
iff 2025-04-06 16:58:52 +02:00
parent d81ee69611
commit 5b690ed7ca
6 changed files with 15 additions and 8 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- `exe_contains` condition to check if the command contains the argument
### Fixed ### Fixed
- Parsing command environment variables (e.g. `LANG=ja_JP.UTF-8 pacman` will work as intended) - Parsing command environment variables (e.g. `LANG=ja_JP.UTF-8 pacman` will work as intended)

View file

@ -8,6 +8,7 @@ pub fn match_pattern(executable: &str, data: &Data) -> Option<Vec<String>> {
let last_command = &data.command; let last_command = &data.command;
let executables = &data.executables; let executables = &data.executables;
let mut candidates = vec![]; let mut candidates = vec![];
let split = split_command(last_command);
parse_rules!("rules"); parse_rules!("rules");
if candidates.is_empty() { if candidates.is_empty() {
None None

View file

@ -108,6 +108,7 @@ fn eval_condition(
"executable" => executables.contains(&arg.to_string()), "executable" => executables.contains(&arg.to_string()),
"err_contains" => error_msg.contains(arg), "err_contains" => error_msg.contains(arg),
"cmd_contains" => last_command.contains(arg), "cmd_contains" => last_command.contains(arg),
"exe_contains" => split_command[0].contains(arg),
"min_length" => split_command.len() >= arg.parse::<usize>().unwrap(), "min_length" => split_command.len() >= arg.parse::<usize>().unwrap(),
"length" => split_command.len() == arg.parse::<usize>().unwrap(), "length" => split_command.len() == arg.parse::<usize>().unwrap(),
"max_length" => split_command.len() <= arg.parse::<usize>().unwrap() + 1, "max_length" => split_command.len() <= arg.parse::<usize>().unwrap() + 1,

View file

@ -104,7 +104,6 @@ fn gen_match_rules(rules: &[Rule]) -> TokenStream {
#( #(
for pattern in #patterns_tokens { for pattern in #patterns_tokens {
if error_msg.contains(pattern) { if error_msg.contains(pattern) {
let split = split_command(&last_command);
#suggestion_tokens; #suggestion_tokens;
}; };
})* })*
@ -173,6 +172,7 @@ fn eval_condition(condition: &str, arg: &str) -> TokenStream2 {
"executable" => quote! {executables.contains(&#arg.to_string())}, "executable" => quote! {executables.contains(&#arg.to_string())},
"err_contains" => quote! {error_msg.contains(#arg)}, "err_contains" => quote! {error_msg.contains(#arg)},
"cmd_contains" => quote! {last_command.contains(#arg)}, "cmd_contains" => quote! {last_command.contains(#arg)},
"exe_contains" => quote! {split[0].contains(#arg)},
"min_length" => quote! {(split.len() >= #arg.parse::<usize>().unwrap())}, "min_length" => quote! {(split.len() >= #arg.parse::<usize>().unwrap())},
"length" => quote! {(split.len() == #arg.parse::<usize>().unwrap())}, "length" => quote! {(split.len() == #arg.parse::<usize>().unwrap())},
"max_length" => quote! {(split.len() <= #arg.parse::<usize>().unwrap() + 1)}, "max_length" => quote! {(split.len() <= #arg.parse::<usize>().unwrap() + 1)},

View file

@ -76,11 +76,12 @@ The placeholder is evaluated as following:
Suggestions can have additional conditions to check. To specify conditions, add a `#[...]` at the first line (just like derive macros in Rust). Available conditions: Suggestions can have additional conditions to check. To specify conditions, add a `#[...]` at the first line (just like derive macros in Rust). Available conditions:
- `executable`: Check whether the argument can be found in path - `executable`: Check if the argument can be found in path
- `cmd_contains`: Check whether the last user input contains the argument - `cmd_contains`: Check if the last user input contains the argument
- `err_contains`: Check whether the error of the command contains the argument - `err_contains`: Check if the error of the command contains the argument
- `length`: Check whether the given command has the length of the argument - `exe_contains`: Check if the command name itself contains the argument
- `min_length`: Check whether the given command has at least the length of the argument - `length`: Check if the given command has the length of the argument
- `max_length`: Check whether the given command has at most the length of the argument - `min_length`: Check if the given command has at least the length of the argument
- `max_length`: Check if the given command has at most the length of the argument
- `shell`: Check if the current running shell is the argument - `shell`: Check if the current running shell is the argument

View file

@ -19,7 +19,7 @@ pattern = [
] ]
suggest = [ suggest = [
''' '''
#[cmd_contains(./)] #[exe_contains(/)]
chmod +x {{command[0]}} && chmod +x {{command[0]}} &&
{{command}}''' {{command}}'''
] ]