diff --git a/src/main.rs b/src/main.rs index bd22383..bbb5876 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use crate::style::highlight_difference; +use crate::{shell::command_output, style::highlight_difference}; use colored::Colorize; mod args; @@ -14,8 +14,9 @@ fn main() { "No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?", ); let mut last_command = shell::last_command_expanded_alias(&shell); + let mut error_msg = command_output(&shell, &last_command); loop { - let corrected_command = suggestions::suggest_command(&shell, &last_command); + let corrected_command = suggestions::suggest_command(&shell, &last_command, &error_msg); if let Some(corrected_command) = corrected_command { let command_difference = @@ -33,6 +34,7 @@ fn main() { format!("{}", "Looking for new suggestion...".cyan().bold()); println!("\n{}\n", retry_message); last_command = corrected_command; + error_msg = execution.err().unwrap(); } } else { break; diff --git a/src/suggestions.rs b/src/suggestions.rs index b753cbd..5cd682a 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -1,13 +1,13 @@ +use std::process::Stdio; + use regex_lite::Regex; use rule_parser::parse_rules; use crate::files::{get_best_match_file, get_path_files}; -use crate::shell::{command_output, PRIVILEGE_LIST}; - -pub fn suggest_command(shell: &str, last_command: &str) -> Option { - let err = command_output(shell, last_command); +use crate::shell::{PRIVILEGE_LIST}; +pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Option { let split_command = split_command(last_command); let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) { true => split_command.get(1).expect("No command found.").as_str(), @@ -15,12 +15,12 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option { }; if !PRIVILEGE_LIST.contains(&executable) { - let suggest = match_pattern("privilege", last_command, &err, shell); + let suggest = match_pattern("privilege", last_command, error_msg, shell); if suggest.is_some() { return suggest; } } - let suggest = match_pattern(executable, last_command, &err, shell); + let suggest = match_pattern(executable, last_command, error_msg, shell); if let Some(suggest) = suggest { if PRIVILEGE_LIST.contains(&executable) { return Some(format!("{} {}", split_command[0], suggest)); @@ -28,7 +28,7 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option { return Some(suggest); } - let suggest = match_pattern("general", last_command, &err, shell); + let suggest = match_pattern("general", last_command, error_msg, shell); if let Some(suggest) = suggest { return Some(suggest); } @@ -46,26 +46,21 @@ fn match_pattern( 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() - } + "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 { @@ -190,7 +185,7 @@ fn compare_string(a: &str, b: &str) -> usize { matrix[a.chars().count()][b.chars().count()] } -pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), ()> { +pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), String> { println!("{}\n", highlighted); println!("Press enter to execute the suggestion. Or press Ctrl+C to exit."); std::io::stdin().read_line(&mut String::new()).unwrap(); @@ -203,15 +198,15 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu .arg(shell) .arg("-c") .arg(command) - .spawn() - .expect("failed to execute process") - .wait() - .expect("failed to wait on process"); + .stdout(Stdio::piped()) + .output() + .expect("failed to execute process"); - if process.success() { + if process.status.success() { return Ok(()); } else { - return Err(()); + let error_msg = String::from_utf8_lossy(&process.stderr); + return Err(error_msg.to_string()); } } } @@ -219,14 +214,14 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu let process = std::process::Command::new(shell) .arg("-c") .arg(command) - .spawn() - .expect("failed to execute process") - .wait() - .expect("failed to wait on process"); + .stdout(Stdio::piped()) + .output() + .expect("failed to execute process"); - if process.success() { + if process.status.success() { Ok(()) } else { - Err(()) + let error_msg = String::from_utf8_lossy(&process.stderr); + Err(error_msg.to_string()) } }