From 6a06aa59545fdfcfce8a5b54275ee17273e8c9c6 Mon Sep 17 00:00:00 2001 From: iff Date: Tue, 8 Aug 2023 16:57:14 +0200 Subject: [PATCH] fix: check executable in nushell --- README.md | 2 +- rule_parser/src/lib.rs | 12 +----------- src/suggestions.rs | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 08f6d4f..b54f53b 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ You can now **press `F` to Pay Respects**! Currently, only corrections to `bash`, `zsh`, and `fish` are working flawlessly. -Note that `nushell` support is currently very broken. +`nushell` is currently usable, but there is no alias expansion and you will have to add the corresponding environment variables manually. ## Installing diff --git a/rule_parser/src/lib.rs b/rule_parser/src/lib.rs index 7e9e315..25a9c74 100644 --- a/rule_parser/src/lib.rs +++ b/rule_parser/src/lib.rs @@ -166,17 +166,7 @@ fn parse_conditions(suggest: &str) -> (String, Vec) { fn eval_condition(condition: &str, arg: &str) -> TokenStream2 { match condition { - "executable" => { - quote!{ - std::process::Command::new(shell) - .arg("-c") - .arg(format!("command -v {}", #arg)) - .output() - .expect("failed to execute process") - .status - .success() - } - }, + "executable" => quote!{check_executable(shell, #arg)}, "err_contains" => quote!{error_msg.contains(#arg)}, "cmd_contains" => quote!{last_command.contains(#arg)}, _ => unreachable!("Unknown condition when evaluation condition: {}", condition), diff --git a/src/suggestions.rs b/src/suggestions.rs index da55c57..dc08dba 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -44,6 +44,30 @@ fn match_pattern( parse_rules!("rules"); } +fn check_executable(shell: &str, executable: &str) -> bool { + match shell { + "nu" => { + std::process::Command::new(shell) + .arg("-c") + .arg(format!("if (which {} | is-empty) {{ exit 1 }}", executable)) + .output() + .expect("failed to execute process") + .status + .success() + } + _ => { + std::process::Command::new(shell) + .arg("-c") + .arg(format!("command -v {}", executable)) + .output() + .expect("failed to execute process") + .status + .success() + } + } + +} + fn opt_regex(regex: &str, command: &mut String) -> String { let regex = Regex::new(regex).unwrap(); let opts = regex