refactor: less nesting

This commit is contained in:
iff 2024-10-19 16:48:34 +02:00
parent 76ac397890
commit 78c7d37b92
2 changed files with 31 additions and 27 deletions

View file

@ -48,35 +48,39 @@ fn main() {
let mut last_command = shell::last_command_expanded_alias(&shell); let mut last_command = shell::last_command_expanded_alias(&shell);
let mut error_msg = command_output(&shell, &last_command); let mut error_msg = command_output(&shell, &last_command);
loop { loop {
let corrected_command = suggestions::suggest_command(&shell, &last_command, &error_msg); let suggestion = {
let command = suggestions::suggest_command(&shell, &last_command, &error_msg);
if command.is_none() {
break;
};
command.unwrap()
};
if let Some(corrected_command) = corrected_command { let highlighted_suggestion = {
let command_difference = let deffirence = highlight_difference(&shell, &suggestion, &last_command);
highlight_difference(&shell, &corrected_command, &last_command); if deffirence.is_none() {
if let Some(highlighted_command) = command_difference { break;
let execution = suggestions::confirm_suggestion( };
&shell, deffirence.unwrap()
&corrected_command, };
&highlighted_command,
); let execution =
suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
if execution.is_ok() { if execution.is_ok() {
return; return;
} else { } else {
last_command = corrected_command; last_command = suggestion;
error_msg = execution.err().unwrap(); error_msg = execution.err().unwrap();
let retry_message = format!("{}...", t!("retry")); let retry_message = format!("{}...", t!("retry"));
// println!("\n{} {}", "ERROR:".red().bold(), msg);
eprintln!("\n{}\n", retry_message.cyan().bold()); eprintln!("\n{}\n", retry_message.cyan().bold());
} }
} else {
break;
}
} else {
break;
}
} }
eprintln!("{}: {}\n", t!("no-suggestion"), last_command.red()); eprintln!("{}: {}\n", t!("no-suggestion"), last_command.red());
eprintln!("{}\n{}", t!("contribute"), "https://github.com/iffse/pay-respects"); eprintln!(
"{}\n{}",
t!("contribute"),
"https://github.com/iffse/pay-respects"
);
} }

View file

@ -29,7 +29,7 @@ pub fn command_output(shell: &str, command: &str) -> String {
Ok(output) => match output.stderr.is_empty() { Ok(output) => match output.stderr.is_empty() {
true => String::from_utf8_lossy(&output.stdout).to_lowercase(), true => String::from_utf8_lossy(&output.stdout).to_lowercase(),
false => String::from_utf8_lossy(&output.stderr).to_lowercase(), false => String::from_utf8_lossy(&output.stderr).to_lowercase(),
} },
Err(_) => { Err(_) => {
use colored::*; use colored::*;
eprintln!("Timeout while executing command: {}", command.red()); eprintln!("Timeout while executing command: {}", command.red());