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 error_msg = command_output(&shell, &last_command);
loop {
let corrected_command = suggestions::suggest_command(&shell, &last_command, &error_msg);
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 {
last_command = corrected_command;
error_msg = execution.err().unwrap();
let retry_message = format!("{}...", t!("retry"));
// println!("\n{} {}", "ERROR:".red().bold(), msg);
eprintln!("\n{}\n", retry_message.cyan().bold());
}
} else {
let suggestion = {
let command = suggestions::suggest_command(&shell, &last_command, &error_msg);
if command.is_none() {
break;
}
};
command.unwrap()
};
let highlighted_suggestion = {
let deffirence = highlight_difference(&shell, &suggestion, &last_command);
if deffirence.is_none() {
break;
};
deffirence.unwrap()
};
let execution =
suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
if execution.is_ok() {
return;
} else {
break;
last_command = suggestion;
error_msg = execution.err().unwrap();
let retry_message = format!("{}...", t!("retry"));
eprintln!("\n{}\n", retry_message.cyan().bold());
}
}
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() {
true => String::from_utf8_lossy(&output.stdout).to_lowercase(),
false => String::from_utf8_lossy(&output.stderr).to_lowercase(),
}
},
Err(_) => {
use colored::*;
eprintln!("Timeout while executing command: {}", command.red());