fix: cannot execute colored prompt

This commit is contained in:
iff 2023-07-30 23:48:04 +02:00
parent 58af6c17fc
commit 1f15d92f72
2 changed files with 16 additions and 13 deletions

View file

@ -2,12 +2,10 @@ use std::collections::HashMap;
use rule_parser::parse_rules; use rule_parser::parse_rules;
use crate::shell::{command_output, find_last_command, find_shell}; use crate::shell::{command_output};
use crate::style::highlight_difference; use crate::style::highlight_difference;
pub fn correct_command() -> Option<String> { pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
let shell = find_shell();
let last_command = find_last_command(&shell);
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>>();
@ -20,14 +18,16 @@ pub fn correct_command() -> Option<String> {
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(highlight_difference(&suggest, &last_command)); 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);
return Some(highlight_difference(&suggest, &last_command)); if split_command[0] == "sudo" {
return Some(format!("sudo {}", suggest));
}
return Some(suggest);
} }
None None
} }
@ -84,14 +84,15 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
suggest suggest
} }
pub fn confirm_correction(command: &str) { pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
println!("Did you mean {}?", 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();
let shell = find_shell();
println!("{}", command);
std::process::Command::new(shell) std::process::Command::new(shell)
.arg("-c") .arg("-c")
.arg(command) .arg(command.to_string())
.spawn() .spawn()
.expect("failed to execute process"); .expect("failed to execute process");
} }

View file

@ -5,9 +5,11 @@ mod style;
fn main() { fn main() {
std::env::set_var("LC_ALL", "C"); std::env::set_var("LC_ALL", "C");
let corrected_command = corrections::correct_command(); let shell = shell::find_shell();
let last_command = shell::find_last_command(&shell);
let corrected_command = corrections::correct_command(&shell, &last_command);
if let Some(corrected_command) = corrected_command { if let Some(corrected_command) = corrected_command {
corrections::confirm_correction(&corrected_command); corrections::confirm_correction(&shell, &corrected_command, &last_command);
} else { } else {
println!("No correction found."); println!("No correction found.");
} }