pay-respects/src/main.rs

103 lines
2.2 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/>.
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;
2024-11-23 22:51:43 +01:00
mod modes;
mod rules;
2023-07-30 18:40:18 +02:00
mod shell;
mod style;
2023-08-01 20:29:02 +02:00
mod suggestions;
mod system;
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-11-19 14:59:15 +01:00
#[cfg(feature = "request-ai")]
mod requests;
2024-09-25 17:55:55 +02:00
#[macro_use]
extern crate rust_i18n;
i18n!("i18n", fallback = "en", minify_key = true);
2024-12-06 19:12:40 +01:00
fn main() -> Result<(), std::io::Error> {
2023-08-12 22:39:00 +02:00
colored::control::set_override(true);
2024-12-06 19:12:40 +01:00
let init = init();
let mut data = if let Err(status) = init {
match status {
args::Status::Exit => {
return Ok(());
}
args::Status::Error => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid input",
));
}
_ => {
unreachable!()
}
2024-12-06 17:35:48 +01:00
}
2024-12-06 19:12:40 +01:00
} else {
init.ok().unwrap()
2024-12-06 17:35:48 +01:00
};
data.expand_command();
use shell::Mode;
match data.mode {
Mode::Suggestion => modes::suggestion(&mut data),
2024-12-06 19:12:40 +01:00
Mode::Cnf => modes::cnf(&mut data),
2024-12-06 17:35:48 +01:00
}
Ok(())
}
2024-12-06 19:12:40 +01:00
fn init() -> Result<shell::Data, args::Status> {
2024-11-23 00:02:34 +01:00
let locale = {
let sys_locale = get_locale().unwrap_or("en-US".to_string());
if sys_locale.len() < 2 {
"en_US".to_string()
} else {
sys_locale
}
};
2024-09-26 03:02:00 +02:00
rust_i18n::set_locale(&locale[0..2]);
2023-08-12 22:39:00 +02:00
2024-12-06 19:12:40 +01:00
let status = args::handle_args();
match status {
args::Status::Exit => {
return Err(status);
}
args::Status::Error => {
return Err(status);
}
_ => {}
2024-12-06 17:35:48 +01:00
}
2024-11-19 14:59:15 +01:00
#[cfg(feature = "request-ai")]
{
2024-11-20 21:16:14 +01:00
if std::env::var("_PR_AI_LOCALE").is_err() {
std::env::set_var("_PR_AI_LOCALE", &locale);
}
2024-11-19 14:59:15 +01:00
}
2024-12-06 19:12:40 +01:00
Ok(shell::Data::init())
2023-07-30 18:40:18 +02:00
}