feat: command not found hook

This commit is contained in:
iff 2024-11-23 22:51:43 +01:00
parent f80fbaa8d3
commit e7f3eca47f
4 changed files with 59 additions and 11 deletions

View file

@ -7,6 +7,7 @@ pub fn handle_args() {
}
let mut auto_aliasing = String::new();
let mut shell = String::new();
let mut cnf = true;
let mut index = 1;
while index < args.len() {
match args[index].as_str() {
@ -29,6 +30,10 @@ pub fn handle_args() {
}
index += 1;
}
"--noncf" => {
cnf = false;
index += 1
}
_ => {
shell = args[index].clone();
index += 1
@ -43,7 +48,7 @@ pub fn handle_args() {
let binary_path = &args[0];
initialization(&shell, binary_path, &auto_aliasing);
initialization(&shell, binary_path, &auto_aliasing, cnf);
}
fn print_help() {

View file

@ -18,11 +18,11 @@ use sys_locale::get_locale;
mod args;
mod files;
mod modes;
mod rules;
mod shell;
mod style;
mod suggestions;
mod modes;
#[cfg(feature = "runtime-rules")]
mod replaces;
@ -60,15 +60,12 @@ fn main() {
let mode = match std::env::var("_PR_MODE") {
Ok(mode) => mode,
Err(_) => {
"suggestion".to_string()
}
Err(_) => "suggestion".to_string(),
};
match mode.as_str() {
"suggestion" => modes::suggestion(),
"cnf" => modes::cnf(),
_ => {
}
_ => {}
}
}

View file

@ -1,7 +1,7 @@
use crate::shell::{command_output, get_shell, PRIVILEGE_LIST};
use crate::style::highlight_difference;
use crate::{shell, suggestions};
use crate::suggestions::{split_command, suggest_typo};
use crate::{shell, suggestions};
use colored::Colorize;
pub fn suggestion() {
@ -72,8 +72,8 @@ pub fn cnf() {
let best_match = suggest_typo(&[executable.to_owned()], vec!["path".to_string()]);
if best_match == executable {
eprintln!("{}: no command found", shell);
return
eprintln!("{}: command not found: {}", shell, executable);
return;
}
match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
true => {

View file

@ -146,7 +146,7 @@ pub fn expand_alias_multiline(shell: &str, full_command: &str) -> String {
expanded
}
pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) {
pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str, cnf: bool) {
let last_command;
let alias;
@ -227,11 +227,57 @@ end
}
}
if cnf {
match shell {
"bash" | "zsh" => {
init = format!(
r#"
command_not_found_handler() {{
eval $(_PR_LAST_COMMAND="$@" _PR_SHELL="{}" _PR_MODE=cnf "{}")
}}
{}
"#,
shell, binary_path, init
);
}
"fish" => {
init = format!(
r#"
function fish_command_not_found --on-event fish_command_not_found
eval $(_PR_LAST_COMMAND="$argv" _PR_SHELL="{}" _PR_MODE=cnf "{}")
end
{}
"#,
shell, binary_path, init
);
}
_ => {
println!("Unsupported shell: {}", shell);
exit(1);
}
}
}
println!("{}", init);
std::process::exit(0);
}
pub fn get_shell() -> String {
match std::env::var("_PR_SHELL") {
Ok(shell) => shell,
Err(_) => {
eprintln!(
"{}",
t!("no-env-setup", var = "_PR_SHELL", help = "pay-respects -h")
);
std::process::exit(1);
}
}
}
pub fn shell_syntax(shell: &str, command: &mut String) {
#[allow(clippy::single_match)]
match shell {