fix: use command's STDOUT

This commit is contained in:
iff 2023-07-31 00:27:36 +02:00
parent 1f15d92f72
commit 54b81e0e0f
2 changed files with 32 additions and 13 deletions

View file

@ -2,11 +2,11 @@ use std::collections::HashMap;
use rule_parser::parse_rules; use rule_parser::parse_rules;
use crate::shell::{command_output}; use crate::shell::command_output;
use crate::style::highlight_difference; use crate::style::highlight_difference;
pub fn correct_command(shell: &str, last_command: &str) -> Option<String> { pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
let command_output = command_output(&shell, &last_command); let command_output = command_output(shell, last_command);
let split_command = last_command.split_whitespace().collect::<Vec<&str>>(); let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
let command = match split_command.first().expect("No command found.") { let command = match split_command.first().expect("No command found.") {
@ -17,13 +17,13 @@ pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
if split_command[0] != "sudo" { if split_command[0] != "sudo" {
let suggest = match_pattern("sudo", &command_output); let suggest = match_pattern("sudo", &command_output);
if let Some(suggest) = suggest { if let Some(suggest) = suggest {
let suggest = eval_suggest(&suggest, &last_command); let suggest = eval_suggest(&suggest, last_command);
return Some(suggest); return Some(suggest);
} }
} }
let suggest = match_pattern(command, &command_output); let suggest = match_pattern(command, &command_output);
if let Some(suggest) = suggest { if let Some(suggest) = suggest {
let suggest = eval_suggest(&suggest, &last_command); let suggest = eval_suggest(&suggest, last_command);
if split_command[0] == "sudo" { if split_command[0] == "sudo" {
return Some(format!("sudo {}", suggest)); return Some(format!("sudo {}", suggest));
} }
@ -85,14 +85,29 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
} }
pub fn confirm_correction(shell: &str, command: &str, last_command: &str) { pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
println!("Did you mean {}?", highlight_difference(command, last_command)); println!(
"Did you mean {}?",
highlight_difference(command, last_command)
);
println!("Press enter to execute the corrected command. Or press Ctrl+C to exit."); println!("Press enter to execute the corrected command. Or press Ctrl+C to exit.");
std::io::stdin().read_line(&mut String::new()).unwrap(); std::io::stdin().read_line(&mut String::new()).unwrap();
println!("{}", command); if command.starts_with("sudo") {
std::process::Command::new(shell) std::process::Command::new("sudo")
.arg("-c") .arg(shell)
.arg(command.to_string()) .arg("-c")
.spawn() .arg(command)
.expect("failed to execute process"); .spawn()
.expect("failed to execute process")
.wait()
.expect("failed to wait on process");
} else {
std::process::Command::new(shell)
.arg("-c")
.arg(command)
.spawn()
.expect("failed to execute process")
.wait()
.expect("failed to wait on process");
}
} }

View file

@ -50,11 +50,15 @@ pub fn find_last_command(shell: &str) -> String {
} }
pub fn command_output(shell: &str, command: &str) -> String { pub fn command_output(shell: &str, command: &str) -> String {
println!("Running command: {}", command);
let output = std::process::Command::new(shell) let output = std::process::Command::new(shell)
.arg("-c") .arg("-c")
.arg(command) .arg(command)
.output() .stderr(std::process::Stdio::piped())
.expect("failed to execute process"); .spawn()
.expect("failed to execute process")
.wait_with_output()
.expect("failed to wait on process");
String::from_utf8_lossy(&output.stderr) String::from_utf8_lossy(&output.stderr)
.to_string() .to_string()