2023-08-09 00:14:54 +02:00
|
|
|
// pay-respect: Press F to correct your command
|
|
|
|
|
// Copyright (C) 2023 iff
|
|
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
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
|
|
|
|
2023-07-31 14:12:45 +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() {
|
2023-07-31 14:12:45 +02:00
|
|
|
args::handle_args();
|
2023-07-30 18:40:18 +02:00
|
|
|
|
2023-07-31 14:12:45 +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
|
|
|
}
|