From 5b690ed7cadf0e3e019037efd64a9af7fb2629d9 Mon Sep 17 00:00:00 2001 From: iff Date: Sun, 6 Apr 2025 16:58:52 +0200 Subject: [PATCH] feat: exe_contains condition --- CHANGELOG.md | 4 ++++ core/src/rules.rs | 1 + module-runtime-rules/src/rules.rs | 1 + parser/src/lib.rs | 2 +- rules.md | 13 +++++++------ rules/pr_general.toml | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56b14f1..281f56a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `exe_contains` condition to check if the command contains the argument + ### Fixed - Parsing command environment variables (e.g. `LANG=ja_JP.UTF-8 pacman` will work as intended) diff --git a/core/src/rules.rs b/core/src/rules.rs index 85242a8..03b15b0 100644 --- a/core/src/rules.rs +++ b/core/src/rules.rs @@ -8,6 +8,7 @@ pub fn match_pattern(executable: &str, data: &Data) -> Option> { let last_command = &data.command; let executables = &data.executables; let mut candidates = vec![]; + let split = split_command(last_command); parse_rules!("rules"); if candidates.is_empty() { None diff --git a/module-runtime-rules/src/rules.rs b/module-runtime-rules/src/rules.rs index 9590fd3..8e370fd 100644 --- a/module-runtime-rules/src/rules.rs +++ b/module-runtime-rules/src/rules.rs @@ -108,6 +108,7 @@ fn eval_condition( "executable" => executables.contains(&arg.to_string()), "err_contains" => error_msg.contains(arg), "cmd_contains" => last_command.contains(arg), + "exe_contains" => split_command[0].contains(arg), "min_length" => split_command.len() >= arg.parse::().unwrap(), "length" => split_command.len() == arg.parse::().unwrap(), "max_length" => split_command.len() <= arg.parse::().unwrap() + 1, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 34f4e3e..0953abe 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -104,7 +104,6 @@ fn gen_match_rules(rules: &[Rule]) -> TokenStream { #( for pattern in #patterns_tokens { if error_msg.contains(pattern) { - let split = split_command(&last_command); #suggestion_tokens; }; })* @@ -173,6 +172,7 @@ fn eval_condition(condition: &str, arg: &str) -> TokenStream2 { "executable" => quote! {executables.contains(&#arg.to_string())}, "err_contains" => quote! {error_msg.contains(#arg)}, "cmd_contains" => quote! {last_command.contains(#arg)}, + "exe_contains" => quote! {split[0].contains(#arg)}, "min_length" => quote! {(split.len() >= #arg.parse::().unwrap())}, "length" => quote! {(split.len() == #arg.parse::().unwrap())}, "max_length" => quote! {(split.len() <= #arg.parse::().unwrap() + 1)}, diff --git a/rules.md b/rules.md index 86a736c..32df274 100644 --- a/rules.md +++ b/rules.md @@ -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: -- `executable`: Check whether the argument can be found in path -- `cmd_contains`: Check whether the last user input contains the argument -- `err_contains`: Check whether the error of the command contains the argument -- `length`: Check whether the given command has the length of the argument -- `min_length`: Check whether the given command has at least the length of the argument -- `max_length`: Check whether the given command has at most the length of the argument +- `executable`: Check if the argument can be found in path +- `cmd_contains`: Check if the last user input contains the argument +- `err_contains`: Check if the error of the command contains the argument +- `exe_contains`: Check if the command name itself contains the argument +- `length`: Check if the given command has 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 diff --git a/rules/pr_general.toml b/rules/pr_general.toml index 77c62a8..967fe30 100644 --- a/rules/pr_general.toml +++ b/rules/pr_general.toml @@ -19,7 +19,7 @@ pattern = [ ] suggest = [ ''' -#[cmd_contains(./)] +#[exe_contains(/)] chmod +x {{command[0]}} && {{command}}''' ]