diff --git a/src/main.rs b/src/main.rs index 6b269fe..b3e5a05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,17 +13,28 @@ fn main() { let shell = std::env::var("_PR_SHELL").expect( "No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?", ); - let last_command = shell::last_command_expanded_alias(&shell); - let corrected_command = suggestions::suggest_command(&shell, &last_command); + let mut last_command = shell::last_command_expanded_alias(&shell); + loop { - if let Some(corrected_command) = corrected_command { - let command_difference = highlight_difference(&shell, &corrected_command, &last_command); - if let Some(highlighted_command) = command_difference { - suggestions::confirm_suggestion(&shell, &corrected_command, &highlighted_command); - return; + let corrected_command = suggestions::suggest_command(&shell, &last_command); + + if let Some(corrected_command) = corrected_command { + let command_difference = highlight_difference(&shell, &corrected_command, &last_command); + if let Some(highlighted_command) = command_difference { + let execution = suggestions::confirm_suggestion(&shell, &corrected_command, &highlighted_command); + if execution.is_ok() { + return; + } + else { + let retry_message = format!("{}", "Looking for new suggestion...".cyan().bold()); + println!("\n{}\n", retry_message); + last_command = corrected_command; + } + } + } else { + break; } } - println!( "No correction found for the command: {}\n", last_command.red() diff --git a/src/style.rs b/src/style.rs index 4a897a3..fd05d52 100644 --- a/src/style.rs +++ b/src/style.rs @@ -35,7 +35,7 @@ pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: } for old in &old_entries { if old == entry { - *entry = entry.cyan().to_string(); + *entry = entry.blue().to_string(); continue 'next; } } diff --git a/src/suggestions.rs b/src/suggestions.rs index 4e9fd4b..32fbf62 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -166,7 +166,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) { +pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), ()> { 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(); @@ -175,7 +175,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) { let _p = p.to_owned() + " "; if command.starts_with(&_p) { let command = command.replace(p, ""); - std::process::Command::new(p) + let process = std::process::Command::new(p) .arg(shell) .arg("-c") .arg(command) @@ -183,15 +183,26 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) { .expect("failed to execute process") .wait() .expect("failed to wait on process"); - return; + + if process.success() { + return Ok(()); + } else { + return Err(()); + } } } - std::process::Command::new(shell) + let process = std::process::Command::new(shell) .arg("-c") .arg(command) .spawn() .expect("failed to execute process") .wait() .expect("failed to wait on process"); + + if process.success() { + return Ok(()); + } else { + return Err(()); + } }