mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
feat: all typo candidates for executables
This commit is contained in:
parent
0a799db5e5
commit
bf16675d4e
9 changed files with 173 additions and 19 deletions
|
|
@ -173,6 +173,49 @@ pub fn typo(suggest: &mut String, split_command: &[String], executables: &[Strin
|
|||
}
|
||||
}
|
||||
|
||||
pub fn exes(suggest: &mut String, split_command: &[String], executables: &[String], exes_list: &mut Vec<String>) {
|
||||
if suggest.contains("{{exes") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{exes", "}}");
|
||||
|
||||
let index = if suggest.contains('[') {
|
||||
let split = suggest[args.to_owned()]
|
||||
.split(&['[', ']'])
|
||||
.collect::<Vec<&str>>();
|
||||
let command_index = split[1];
|
||||
if !command_index.contains(':') {
|
||||
let command_index = command_index.parse::<i32>().unwrap();
|
||||
|
||||
let index = if command_index < 0 {
|
||||
split_command.len() as i32 + command_index
|
||||
} else {
|
||||
command_index
|
||||
};
|
||||
index as usize
|
||||
} else {
|
||||
unreachable!("Exes suggestion does not support range");
|
||||
}
|
||||
} else {
|
||||
unreachable!("Exes suggestion must have a command index");
|
||||
};
|
||||
|
||||
let matches = {
|
||||
let res = best_matches_path(&split_command[index], executables);
|
||||
if res.is_none() {
|
||||
vec![split_command[index].clone()]
|
||||
} else {
|
||||
res.unwrap()
|
||||
}
|
||||
};
|
||||
for match_ in matches {
|
||||
exes_list.push(match_);
|
||||
}
|
||||
|
||||
let tag = "{{exes}}";
|
||||
let placeholder = suggest[placeholder.clone()].to_owned();
|
||||
*suggest = suggest.replace(&placeholder, &tag);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell(suggest: &mut String, shell: &str) {
|
||||
while suggest.contains("{{shell") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{shell", "}}");
|
||||
|
|
|
|||
|
|
@ -83,11 +83,11 @@ pub fn runtime_match(
|
|||
if pure_suggest.contains("{{command}}") {
|
||||
pure_suggest = pure_suggest.replace("{{command}}", last_command);
|
||||
}
|
||||
print!(
|
||||
"{}",
|
||||
eval_suggest(&pure_suggest, last_command, error_msg, executables, shell,)
|
||||
);
|
||||
print!("<_PR_BR>");
|
||||
let suggests = eval_suggest(&pure_suggest, last_command, error_msg, executables, shell);
|
||||
for suggest in suggests {
|
||||
print!("{}", suggest);
|
||||
print!("<_PR_BR>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -121,7 +121,7 @@ fn eval_suggest(
|
|||
error_msg: &str,
|
||||
executables: &[String],
|
||||
shell: &str,
|
||||
) -> String {
|
||||
) -> Vec<String> {
|
||||
let mut suggest = suggest.to_owned();
|
||||
if suggest.contains("{{command}}") {
|
||||
suggest = suggest.replace("{{command}}", "{last_command}");
|
||||
|
|
@ -139,11 +139,23 @@ fn eval_suggest(
|
|||
replaces::shell(&mut suggest, shell);
|
||||
replaces::typo(&mut suggest, &split_command, executables, shell);
|
||||
|
||||
let mut exes_list = Vec::new();
|
||||
replaces::exes(&mut suggest, &split_command, executables, &mut exes_list);
|
||||
|
||||
for (tag, value) in opt_list {
|
||||
suggest = suggest.replace(&tag, &value);
|
||||
}
|
||||
|
||||
suggest
|
||||
let mut suggests = vec![];
|
||||
if exes_list.is_empty() {
|
||||
suggests.push(suggest);
|
||||
} else {
|
||||
for exe in exes_list {
|
||||
let eval_suggest = suggest.clone().replace("{{exes}}", &exe);
|
||||
suggests.push(eval_suggest);
|
||||
}
|
||||
}
|
||||
suggests
|
||||
}
|
||||
|
||||
fn get_rule(executable: &str) -> Option<String> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue