fix: modules

This commit is contained in:
iff 2024-12-08 15:33:43 +01:00
parent 5aebf867c1
commit fedee1dc8d
12 changed files with 54 additions and 39 deletions

View file

@ -2,15 +2,24 @@ 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();
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<String> = {
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(())
}

View file

@ -183,4 +183,3 @@ pub fn shell(suggest: &mut String, shell: &str) {
suggest.replace_range(placeholder, &command.join("\n"));
}
}

View file

@ -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<String> {
None
}

View file

@ -207,7 +207,10 @@ pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
&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);

View file

@ -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<String> {
}
pub fn split_command(command: &str) -> Vec<String> {
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()]
}

View file

@ -164,4 +164,3 @@ fn path_env() -> String {
fn path_env_sep() -> &'static str {
":"
}

View file

@ -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;

View file

@ -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;

View file

@ -238,7 +238,7 @@ pub fn command_output(shell: &str, command: &str) -> String {
}
}
pub fn module_output(data: &Data, module: &str) -> Vec<String> {
pub fn module_output(data: &Data, module: &str) -> Option<Vec<String>> {
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<String> {
.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)
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::<Vec<String>>()
.collect::<Vec<String>>(),
)
}
pub fn last_command(shell: &str) -> String {

View file

@ -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)]

View file

@ -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());
}
}

View file

@ -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),