pay-respects/src/main.rs

95 lines
2.5 KiB
Rust
Raw Normal View History

// pay-respects: Press F to correct your command
2023-08-09 00:14:54 +02:00
// 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;
2024-11-13 16:31:31 +01:00
use sys_locale::get_locale;
2023-07-31 14:27:44 +02:00
mod args;
2023-08-03 21:26:41 +02:00
mod files;
mod rules;
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
2024-11-15 15:43:04 +01:00
#[cfg(feature = "runtime-rules")]
mod replaces;
#[cfg(feature = "runtime-rules")]
mod runtime_rules;
2024-09-25 17:55:55 +02:00
#[macro_use]
extern crate rust_i18n;
i18n!("i18n", fallback = "en", minify_key = true);
2023-07-30 18:40:18 +02:00
fn main() {
2023-08-12 22:39:00 +02:00
colored::control::set_override(true);
2024-11-13 16:31:31 +01:00
// let locale = std::env::var("LANG").unwrap_or("en_US".to_string());
let locale = get_locale().unwrap_or("en_US".to_string());
2024-09-26 03:02:00 +02:00
rust_i18n::set_locale(&locale[0..2]);
2023-08-12 22:39:00 +02:00
args::handle_args();
2023-07-30 18:40:18 +02:00
let shell = match std::env::var("_PR_SHELL") {
Ok(shell) => shell,
Err(_) => {
2024-09-25 17:55:55 +02:00
eprintln!(
"{}",
t!("no-env-setup", var = "_PR_SHELL", help = "pay-respects -h")
);
std::process::exit(1);
}
};
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 {
2024-10-19 16:48:34 +02:00
let suggestion = {
let command = suggestions::suggest_command(&shell, &last_command, &error_msg);
if command.is_none() {
break;
};
command.unwrap()
};
2024-10-19 16:48:34 +02:00
let highlighted_suggestion = {
2024-10-28 12:01:59 +01:00
let difference = highlight_difference(&shell, &suggestion, &last_command);
if difference.is_none() {
2023-08-07 20:21:02 +02:00
break;
2024-10-19 16:48:34 +02:00
};
2024-10-28 12:01:59 +01:00
difference.unwrap()
2024-10-19 16:48:34 +02:00
};
let execution =
suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
if execution.is_ok() {
return;
2023-08-07 19:58:19 +02:00
} else {
2024-10-19 16:48:34 +02:00
last_command = suggestion;
error_msg = execution.err().unwrap();
let retry_message = format!("{}...", t!("retry"));
eprintln!("\n{}\n", retry_message.cyan().bold());
2023-07-31 23:42:19 +02:00
}
2023-07-30 18:40:18 +02:00
}
2024-09-25 18:07:02 +02:00
eprintln!("{}: {}\n", t!("no-suggestion"), last_command.red());
2024-10-19 16:48:34 +02:00
eprintln!(
"{}\n{}",
t!("contribute"),
"https://github.com/iffse/pay-respects"
);
2023-07-30 18:40:18 +02:00
}