chore: cleanup

This commit is contained in:
iff 2024-11-18 20:40:41 +01:00
parent 508e61a451
commit 7871e943db
3 changed files with 48 additions and 45 deletions

View file

@ -9,9 +9,9 @@ touch src/rules.rs && cargo build
Runtime rules directories are searched, by the order or priority: Runtime rules directories are searched, by the order or priority:
- `XDG_CONFIG_HOME` - `XDG_CONFIG_HOME`, defaults to `$HOME/.config`
- `XDG_CONFIG_DIRS` - `XDG_CONFIG_DIRS`, defaults to `/etc/xdg`
- `XDG_DATA_DIRS` - `XDG_DATA_DIRS`, defaults to `/usr/local/share:/usr/share`
The actual rule file should be placed under `pay-respects/rules/`, for example: `~/.config/pay-respects/rules/cargo.toml`. Note that for runtime rules, the name of the file **MUST** match the command name. The actual rule file should be placed under `pay-respects/rules/`, for example: `~/.config/pay-respects/rules/cargo.toml`. Note that for runtime rules, the name of the file **MUST** match the command name.

View file

@ -18,44 +18,7 @@ pub fn runtime_match(
error_msg: &str, error_msg: &str,
shell: &str, shell: &str,
) -> Option<String> { ) -> Option<String> {
let xdg_config_home = std::env::var("XDG_CONFIG_HOME") let file = get_rule(executable);
.unwrap_or_else(|_| std::env::var("HOME").unwrap() + "/.config");
let xdg_config_dirs = std::env::var("XDG_CONFIG_DIRS").unwrap_or("/etc/xdg".to_owned());
let xdg_config_dirs = xdg_config_dirs.split(':').collect::<Vec<&str>>();
let xdg_data_dirs =
std::env::var("XDG_DATA_DIRS").unwrap_or("/usr/local/share:/usr/share".to_owned());
let xdg_data_dirs = xdg_data_dirs.split(':').collect::<Vec<&str>>();
let user_rule_dir = format!("{}/pay-respects/rules", xdg_config_home);
let user_rule_file = format!("{}/{}.toml", user_rule_dir, executable);
let mut file = Option::None;
if std::path::Path::new(&user_rule_file).exists() {
file = Some(user_rule_file);
}
let check_dirs = |dirs: Vec<&str>| -> Option<String> {
for dir in dirs {
let rule_dir = format!("{}/pay-respects/rules", dir);
let rule_file = format!("{}/{}.toml", rule_dir, executable);
if std::path::Path::new(&rule_file).exists() {
return Some(rule_file);
}
}
None
};
if file.is_none() {
file = check_dirs(xdg_config_dirs);
}
if file.is_none() {
file = check_dirs(xdg_data_dirs);
}
#[allow(clippy::question_mark)]
if file.is_none() { if file.is_none() {
return None; return None;
} }
@ -171,3 +134,43 @@ fn eval_suggest(suggest: &str, last_command: &str, error_msg: &str, shell: &str)
Some(suggest) Some(suggest)
} }
fn get_rule(executable: &str) -> Option<String> {
let xdg_config_home = std::env::var("XDG_CONFIG_HOME")
.unwrap_or_else(|_| std::env::var("HOME").unwrap() + "/.config");
let user_rule_dir = format!("{}/pay-respects/rules", xdg_config_home);
let user_rule_file = format!("{}/{}.toml", user_rule_dir, executable);
if std::path::Path::new(&user_rule_file).exists() {
return Some(user_rule_file);
}
let check_dirs = |dirs: Vec<&str>| -> Option<String> {
for dir in dirs {
let rule_dir = format!("{}/pay-respects/rules", dir);
let rule_file = format!("{}/{}.toml", rule_dir, executable);
if std::path::Path::new(&rule_file).exists() {
return Some(rule_file);
}
}
None
};
let xdg_config_dirs = std::env::var("XDG_CONFIG_DIRS").unwrap_or("/etc/xdg".to_owned());
let xdg_config_dirs = xdg_config_dirs.split(':').collect::<Vec<&str>>();
if let Some(file) = check_dirs(xdg_config_dirs) {
return Some(file);
}
let xdg_data_dirs =
std::env::var("XDG_DATA_DIRS").unwrap_or("/usr/local/share:/usr/share".to_owned());
let xdg_data_dirs = xdg_data_dirs.split(':').collect::<Vec<&str>>();
if let Some(file) = check_dirs(xdg_data_dirs) {
return Some(file);
}
None
}

View file

@ -278,7 +278,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
fn run_suggestion_p (shell: &str, p: &str, command: &str) -> std::process::ExitStatus { fn run_suggestion_p(shell: &str, p: &str, command: &str) -> std::process::ExitStatus {
use std::os::fd::AsFd; use std::os::fd::AsFd;
std::process::Command::new(p) std::process::Command::new(p)
.arg(shell) .arg(shell)
@ -293,7 +293,7 @@ fn run_suggestion_p (shell: &str, p: &str, command: &str) -> std::process::ExitS
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
fn run_suggestion (shell: &str, command: &str) -> std::process::ExitStatus { fn run_suggestion(shell: &str, command: &str) -> std::process::ExitStatus {
use std::os::fd::AsFd; use std::os::fd::AsFd;
std::process::Command::new(shell) std::process::Command::new(shell)
.arg("-c") .arg("-c")
@ -308,7 +308,7 @@ fn run_suggestion (shell: &str, command: &str) -> std::process::ExitStatus {
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn run_suggestion_p (shell: &str, p: &str, command: &str) -> std::process::ExitStatus { fn run_suggestion_p(shell: &str, p: &str, command: &str) -> std::process::ExitStatus {
use std::os::windows::io::AsHandle; use std::os::windows::io::AsHandle;
std::process::Command::new(p) std::process::Command::new(p)
.arg(shell) .arg(shell)
@ -323,7 +323,7 @@ fn run_suggestion_p (shell: &str, p: &str, command: &str) -> std::process::ExitS
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn run_suggestion (shell: &str, command: &str) -> std::process::ExitStatus { fn run_suggestion(shell: &str, command: &str) -> std::process::ExitStatus {
use std::os::windows::io::AsHandle; use std::os::windows::io::AsHandle;
std::process::Command::new(shell) std::process::Command::new(shell)
.arg("-c") .arg("-c")