From fedee1dc8d8a7b3ed728e5150102aedf7f841f64 Mon Sep 17 00:00:00 2001 From: iff Date: Sun, 8 Dec 2024 15:33:43 +0100 Subject: [PATCH] fix: modules --- pay-respects-module-runtime-rules/src/main.rs | 21 +++++++++++++------ .../src/replaces.rs | 1 - .../src/rules.rs | 16 +++++--------- pay-respects-parser/src/replaces.rs | 5 ++++- pay-respects-utils/src/evals.rs | 8 +++---- pay-respects-utils/src/files.rs | 1 - pay-respects/src/modes.rs | 3 +-- pay-respects/src/rules.rs | 2 +- pay-respects/src/shell.rs | 17 ++++++++++----- pay-respects/src/style.rs | 2 +- pay-respects/src/suggestions.rs | 8 ++++--- pay-respects/src/system.rs | 9 ++++++-- 12 files changed, 54 insertions(+), 39 deletions(-) diff --git a/pay-respects-module-runtime-rules/src/main.rs b/pay-respects-module-runtime-rules/src/main.rs index 4b5309c..6776160 100644 --- a/pay-respects-module-runtime-rules/src/main.rs +++ b/pay-respects-module-runtime-rules/src/main.rs @@ -1,16 +1,25 @@ mod replaces; mod rules; -fn main() -> Result<(), std::io::Error>{ - let executable = std::env::var("_PR_COMMAND").unwrap(); - let shell = std::env::var("_PR_SHELL").unwrap(); - let last_command = std::env::var("_PR_LAST_COMMAND").unwrap(); - let error_msg = std::env::var("_PR_ERROR_MSG").unwrap(); +fn main() -> Result<(), std::io::Error> { + let executable = std::env::var("_PR_COMMAND").expect("_PR_COMMAND not set"); + let shell = std::env::var("_PR_SHELL").expect("_PR_SHELL not set"); + let last_command = std::env::var("_PR_LAST_COMMAND").expect("_PR_LAST_COMMAND not set"); + let error_msg = std::env::var("_PR_ERROR_MSG").expect("_PR_ERROR_MSG not set"); let executables: Vec = { - let executables = std::env::var("_PR_EXECUTABLES").unwrap(); + let executables = std::env::var("_PR_EXECUTABLES").expect("_PR_EXECUTABLES not set"); executables.split(",").map(|s| s.to_string()).collect() }; + #[cfg(debug_assertions)] + { + eprintln!("shell: {}", shell); + eprintln!("executable: {}", executable); + eprintln!("last_command: {}", last_command); + eprintln!("error_msg: {}", error_msg); + eprintln!("executables: {:?}", executables); + } + rules::runtime_match(&executable, &shell, &last_command, &error_msg, &executables); Ok(()) } diff --git a/pay-respects-module-runtime-rules/src/replaces.rs b/pay-respects-module-runtime-rules/src/replaces.rs index d5646fc..ab57ae8 100644 --- a/pay-respects-module-runtime-rules/src/replaces.rs +++ b/pay-respects-module-runtime-rules/src/replaces.rs @@ -183,4 +183,3 @@ pub fn shell(suggest: &mut String, shell: &str) { suggest.replace_range(placeholder, &command.join("\n")); } } - diff --git a/pay-respects-module-runtime-rules/src/rules.rs b/pay-respects-module-runtime-rules/src/rules.rs index be15d31..4af0f7a 100644 --- a/pay-respects-module-runtime-rules/src/rules.rs +++ b/pay-respects-module-runtime-rules/src/rules.rs @@ -13,7 +13,7 @@ struct MatchError { } pub fn runtime_match( - executable: &str, + executable: &str, shell: &str, last_command: &str, error_msg: &str, @@ -83,16 +83,11 @@ pub fn runtime_match( if pure_suggest.contains("{{command}}") { pure_suggest = pure_suggest.replace("{{command}}", last_command); } - print!("{}", - eval_suggest( - &pure_suggest, - last_command, - error_msg, - executables, - shell, - ) + print!( + "{}", + eval_suggest(&pure_suggest, last_command, error_msg, executables, shell,) ); - print!("{}", "<_PR_BR>"); + print!("<_PR_BR>"); } } } @@ -194,4 +189,3 @@ fn get_rule(executable: &str) -> Option { None } - diff --git a/pay-respects-parser/src/replaces.rs b/pay-respects-parser/src/replaces.rs index 090b9d9..026c186 100644 --- a/pay-respects-parser/src/replaces.rs +++ b/pay-respects-parser/src/replaces.rs @@ -207,7 +207,10 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec) { &function[function.find(',').unwrap() + 1..function.len() - 1], "\")" ); - format!("suggest_typo(&split[{}], {}, executables)", string_index, function) + format!( + "suggest_typo(&split[{}], {}, executables)", + string_index, function + ) } else { let string_match_list = match_list.join("\".to_string(), \""); let string_match_list = format!("\"{}\".to_string()", string_match_list); diff --git a/pay-respects-utils/src/evals.rs b/pay-respects-utils/src/evals.rs index daf6fca..83883e9 100644 --- a/pay-respects-utils/src/evals.rs +++ b/pay-respects-utils/src/evals.rs @@ -1,5 +1,5 @@ -use regex_lite::Regex; use crate::files::*; +use regex_lite::Regex; pub fn opt_regex(regex: &str, command: &mut String) -> String { let regex = Regex::new(regex).unwrap(); @@ -56,9 +56,8 @@ pub fn eval_shell_command(shell: &str, command: &str) -> Vec { } pub fn split_command(command: &str) -> Vec { - if cfg!(debug_assertions) { - eprintln!("command: {command}") - } + #[cfg(debug_assertions)] + eprintln!("command: {command}"); // this regex splits the command separated by spaces, except when the space // is escaped by a backslash or surrounded by quotes let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#; @@ -152,4 +151,3 @@ pub fn compare_string(a: &str, b: &str) -> usize { } matrix[a.chars().count()][b.chars().count()] } - diff --git a/pay-respects-utils/src/files.rs b/pay-respects-utils/src/files.rs index 14f18ef..245fc88 100644 --- a/pay-respects-utils/src/files.rs +++ b/pay-respects-utils/src/files.rs @@ -164,4 +164,3 @@ fn path_env() -> String { fn path_env_sep() -> &'static str { ":" } - diff --git a/pay-respects/src/modes.rs b/pay-respects/src/modes.rs index 74a3e63..1c1e6a0 100644 --- a/pay-respects/src/modes.rs +++ b/pay-respects/src/modes.rs @@ -2,13 +2,12 @@ use crate::shell::Data; use crate::suggestions::suggest_candidates; use crate::system; use crate::{shell, suggestions}; -use pay_respects_utils::evals::best_match_path; use colored::Colorize; use inquire::*; +use pay_respects_utils::evals::best_match_path; use std::path::Path; - pub fn suggestion(data: &mut Data) { let shell = data.shell.clone(); let mut last_command; diff --git a/pay-respects/src/rules.rs b/pay-respects/src/rules.rs index ccfc370..a5cee8a 100644 --- a/pay-respects/src/rules.rs +++ b/pay-respects/src/rules.rs @@ -1,6 +1,6 @@ use crate::shell::Data; -use pay_respects_utils::evals::*; use pay_respects_parser::parse_rules; +use pay_respects_utils::evals::*; pub fn match_pattern(executable: &str, data: &mut Data) { let error_msg = &data.error; diff --git a/pay-respects/src/shell.rs b/pay-respects/src/shell.rs index 484d212..0729deb 100644 --- a/pay-respects/src/shell.rs +++ b/pay-respects/src/shell.rs @@ -238,7 +238,7 @@ pub fn command_output(shell: &str, command: &str) -> String { } } -pub fn module_output(data: &Data, module: &str) -> Vec { +pub fn module_output(data: &Data, module: &str) -> Option> { let shell = &data.shell; let executable = &data.split[0]; let last_command = &data.command; @@ -248,16 +248,23 @@ pub fn module_output(data: &Data, module: &str) -> Vec { .arg("-c") .arg(module) .env("_PR_COMMAND", executable) + .env("_PR_SHELL", shell) .env("_PR_LAST_COMMAND", last_command) .env("_PR_ERROR_MSG", error_msg) .env("_PR_EXECUTABLES", executables) .output() .expect("failed to execute process"); - String::from_utf8_lossy(&output.stderr) - .split("<_PR_BR>") - .map(|s| s.trim().to_string()) - .collect::>() + if output.stdout.is_empty() { + return None; + } + let break_holder = "<_PR_BR>"; + Some( + String::from_utf8_lossy(&output.stdout)[..output.stdout.len() - break_holder.len()] + .split("<_PR_BR>") + .map(|s| s.trim().to_string()) + .collect::>(), + ) } pub fn last_command(shell: &str) -> String { diff --git a/pay-respects/src/style.rs b/pay-respects/src/style.rs index ec2ddfd..f4184dc 100644 --- a/pay-respects/src/style.rs +++ b/pay-respects/src/style.rs @@ -1,6 +1,6 @@ -use pay_respects_utils::evals::split_command; use crate::shell::PRIVILEGE_LIST; use colored::*; +use pay_respects_utils::evals::split_command; // to_string() is necessary here, otherwise there won't be color in the output #[warn(clippy::unnecessary_to_owned)] diff --git a/pay-respects/src/suggestions.rs b/pay-respects/src/suggestions.rs index 26be399..4c4d9c7 100644 --- a/pay-respects/src/suggestions.rs +++ b/pay-respects/src/suggestions.rs @@ -6,7 +6,7 @@ use colored::Colorize; use inquire::*; use crate::rules::match_pattern; -use crate::shell::{shell_evaluated_commands, Data, module_output}; +use crate::shell::{module_output, shell_evaluated_commands, Data}; use crate::style::highlight_difference; pub fn suggest_candidates(data: &mut Data) { @@ -20,10 +20,12 @@ pub fn suggest_candidates(data: &mut Data) { match_pattern("_PR_general", data); let modules = &data.modules.clone(); + eprintln!("modules: {modules:?}"); for module in modules { let candidates = module_output(data, module); - if !candidates.is_empty() { - data.add_candidates(&candidates); + eprintln!("runtime: {candidates:?}"); + if candidates.is_some() { + data.add_candidates(&candidates.unwrap()); } } diff --git a/pay-respects/src/system.rs b/pay-respects/src/system.rs index 2d04dd8..812d6dc 100644 --- a/pay-respects/src/system.rs +++ b/pay-respects/src/system.rs @@ -52,7 +52,10 @@ pub fn get_packages( } } "dnf" | "yum" => { - let result = command_output(shell, &format!("{} provides '/usr/bin/{}'", package_manager, executable)); + let result = command_output( + shell, + &format!("{} provides '/usr/bin/{}'", package_manager, executable), + ); if result.is_empty() { return None; } @@ -163,7 +166,9 @@ pub fn get_packages( pub fn install_package(data: &mut Data, package_manager: &str, package: &str) -> bool { let shell = &data.shell.clone(); let mut install = match package_manager { - "apt" | "dnf" | "pkg" | "yum" | "zypper" => format!("{} install {}", package_manager, package), + "apt" | "dnf" | "pkg" | "yum" | "zypper" => { + format!("{} install {}", package_manager, package) + } "emerge" => format!("emerge {}", package), "nix" => format!("nix profile install nixpkgs#{}", package), "pacman" => format!("pacman -S {}", package),