pay-respects/src/main.rs

54 lines
1.4 KiB
Rust
Raw Normal View History

2023-08-08 20:33:49 +02:00
use crate::{shell::command_output, style::highlight_difference};
2023-08-07 20:21:02 +02:00
use colored::Colorize;
2023-07-31 14:27:44 +02:00
mod args;
2023-08-03 21:26:41 +02:00
mod files;
2023-07-30 18:40:18 +02:00
mod shell;
mod style;
2023-08-01 20:29:02 +02:00
mod suggestions;
2023-07-30 18:40:18 +02:00
fn main() {
args::handle_args();
2023-07-30 18:40:18 +02:00
let shell = std::env::var("_PR_SHELL").expect(
"No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?",
);
2023-08-07 19:58:19 +02:00
let mut last_command = shell::last_command_expanded_alias(&shell);
2023-08-08 20:33:49 +02:00
let mut error_msg = command_output(&shell, &last_command);
2023-08-07 19:58:19 +02:00
loop {
2023-08-08 20:33:49 +02:00
let corrected_command = suggestions::suggest_command(&shell, &last_command, &error_msg);
2023-08-07 19:58:19 +02:00
if let Some(corrected_command) = corrected_command {
2023-08-07 20:21:02 +02:00
let command_difference =
highlight_difference(&shell, &corrected_command, &last_command);
2023-08-07 19:58:19 +02:00
if let Some(highlighted_command) = command_difference {
2023-08-07 20:21:02 +02:00
let execution = suggestions::confirm_suggestion(
&shell,
&corrected_command,
&highlighted_command,
);
2023-08-07 19:58:19 +02:00
if execution.is_ok() {
return;
2023-08-07 20:21:02 +02:00
} else {
let retry_message =
format!("{}", "Looking for new suggestion...".cyan().bold());
2023-08-07 19:58:19 +02:00
println!("\n{}\n", retry_message);
last_command = corrected_command;
2023-08-08 20:33:49 +02:00
error_msg = execution.err().unwrap();
2023-08-07 19:58:19 +02:00
}
2023-08-07 20:21:02 +02:00
} else {
break;
2023-08-07 19:58:19 +02:00
}
} else {
break;
2023-07-31 23:42:19 +02:00
}
2023-07-30 18:40:18 +02:00
}
2023-07-31 23:42:19 +02:00
println!(
"No correction found for the command: {}\n",
2023-08-01 20:29:02 +02:00
last_command.red()
2023-07-31 23:42:19 +02:00
);
2023-07-31 23:48:14 +02:00
println!(
"If you think there should be a correction, please open an issue or send a pull request!"
);
2023-07-30 18:40:18 +02:00
}