mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2026-02-02 07:35:10 +01:00
feat: match file
This commit is contained in:
parent
5858b5b88b
commit
7cfe075da6
5 changed files with 94 additions and 25 deletions
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
colored = "2.0"
|
colored = "2.0"
|
||||||
rule_parser = { path = "rule_parser" }
|
rule_parser = { path = "rule_parser" }
|
||||||
|
regex = "1.0"
|
||||||
|
|
|
||||||
8
rules/rm.toml
Normal file
8
rules/rm.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
command = "rm"
|
||||||
|
|
||||||
|
[[match_err]]
|
||||||
|
pattern = [ "no such file or directory", ]
|
||||||
|
suggest = [
|
||||||
|
'''
|
||||||
|
{{command[:-2]}} {{typo[-1](file)}}'''
|
||||||
|
]
|
||||||
|
|
@ -8,13 +8,13 @@ use crate::style::highlight_difference;
|
||||||
pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
|
pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
|
||||||
let err = command_output(shell, last_command);
|
let err = command_output(shell, last_command);
|
||||||
|
|
||||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
let split_command = split_command(last_command);
|
||||||
let executable = match PRIVILEGE_LIST.contains(&split_command[0]) {
|
let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
|
||||||
true => split_command.get(1).expect("No command found."),
|
true => split_command.get(1).expect("No command found.").as_str(),
|
||||||
false => split_command.first().expect("No command found."),
|
false => split_command.first().expect("No command found.").as_str(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !PRIVILEGE_LIST.contains(executable) {
|
if !PRIVILEGE_LIST.contains(&executable) {
|
||||||
let suggest = match_pattern("privilege", last_command, &err);
|
let suggest = match_pattern("privilege", last_command, &err);
|
||||||
if let Some(suggest) = suggest {
|
if let Some(suggest) = suggest {
|
||||||
let suggest = eval_suggest(&suggest, last_command);
|
let suggest = eval_suggest(&suggest, last_command);
|
||||||
|
|
@ -24,7 +24,7 @@ pub fn correct_command(shell: &str, last_command: &str) -> Option<String> {
|
||||||
let suggest = match_pattern(executable, last_command, &err);
|
let suggest = match_pattern(executable, last_command, &err);
|
||||||
if let Some(suggest) = suggest {
|
if let Some(suggest) = suggest {
|
||||||
let suggest = eval_suggest(&suggest, last_command);
|
let suggest = eval_suggest(&suggest, last_command);
|
||||||
if PRIVILEGE_LIST.contains(executable) {
|
if PRIVILEGE_LIST.contains(&executable) {
|
||||||
return Some(format!("{} {}", split_command[0], suggest));
|
return Some(format!("{} {}", split_command[0], suggest));
|
||||||
}
|
}
|
||||||
return Some(suggest);
|
return Some(suggest);
|
||||||
|
|
@ -116,6 +116,7 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
|
||||||
if suggest.contains("{{command}}") {
|
if suggest.contains("{{command}}") {
|
||||||
suggest = suggest.replace("{{command}}", last_command);
|
suggest = suggest.replace("{{command}}", last_command);
|
||||||
}
|
}
|
||||||
|
println!("eval suggest: {}", suggest);
|
||||||
while suggest.contains("{{command") {
|
while suggest.contains("{{command") {
|
||||||
let placeholder_start = "{{command";
|
let placeholder_start = "{{command";
|
||||||
let placeholder_end = "}}";
|
let placeholder_end = "}}";
|
||||||
|
|
@ -128,19 +129,32 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
|
||||||
let placeholder = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
let placeholder = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
||||||
let range = suggest[placeholder.to_owned()].trim_matches(|c| c == '[' || c == ']');
|
let range = suggest[placeholder.to_owned()].trim_matches(|c| c == '[' || c == ']');
|
||||||
if let Some((start, end)) = range.split_once(':') {
|
if let Some((start, end)) = range.split_once(':') {
|
||||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
let split_command = split_command(last_command);
|
||||||
let start = start.parse::<usize>().unwrap_or(0);
|
let start = {
|
||||||
let end = end.parse::<usize>().unwrap_or(split_command.len() - 1) + 1;
|
let mut start = start.parse::<i32>().unwrap_or(0);
|
||||||
|
if start < 0 {
|
||||||
|
start = split_command.len() as i32 + start;
|
||||||
|
}
|
||||||
|
start as usize
|
||||||
|
};
|
||||||
|
let end = {
|
||||||
|
let mut end = end.parse::<i32>().unwrap_or(split_command.len() as i32 - 1) + 1;
|
||||||
|
if end < 0 {
|
||||||
|
end = split_command.len() as i32 + end;
|
||||||
|
}
|
||||||
|
end as usize
|
||||||
|
};
|
||||||
let command = split_command[start..end].join(" ");
|
let command = split_command[start..end].join(" ");
|
||||||
|
|
||||||
suggest = suggest.replace(&suggest[start_index..end_index], &command);
|
suggest = suggest.replace(&suggest[start_index..end_index], &command);
|
||||||
} else {
|
} else {
|
||||||
let range = range.parse::<usize>().unwrap_or(0);
|
let range = range.parse::<usize>().unwrap_or(0);
|
||||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
let split_command = split_command(last_command);
|
||||||
let command = split_command[range].to_owned();
|
let command = split_command[range].to_owned();
|
||||||
suggest = suggest.replace(&suggest[start_index..end_index], &command);
|
suggest = suggest.replace(&suggest[start_index..end_index], &command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println!("eval suggest: {}", suggest);
|
||||||
|
|
||||||
while suggest.contains("{{typo") {
|
while suggest.contains("{{typo") {
|
||||||
let placeholder_start = "{{typo";
|
let placeholder_start = "{{typo";
|
||||||
|
|
@ -153,13 +167,18 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
|
||||||
|
|
||||||
let placeholder = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
let placeholder = start_index + placeholder_start.len()..end_index - placeholder_end.len();
|
||||||
|
|
||||||
let mut command_index = 0;
|
let mut command_index;
|
||||||
let mut match_list = vec![];
|
let mut match_list = vec![];
|
||||||
if suggest.contains('[') {
|
if suggest.contains('[') {
|
||||||
let split = suggest[placeholder.to_owned()]
|
let split = suggest[placeholder.to_owned()]
|
||||||
.split(&['[', ']'])
|
.split(&['[', ']'])
|
||||||
.collect::<Vec<&str>>();
|
.collect::<Vec<&str>>();
|
||||||
command_index = split[1].parse::<usize>().unwrap();
|
command_index = split[1].parse::<i32>().unwrap();
|
||||||
|
if command_index < 0 {
|
||||||
|
command_index = split_command(last_command).len() as i32 + command_index;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unreachable!("Typo suggestion must have a command index");
|
||||||
}
|
}
|
||||||
if suggest.contains('(') {
|
if suggest.contains('(') {
|
||||||
let split = suggest[placeholder.to_owned()]
|
let split = suggest[placeholder.to_owned()]
|
||||||
|
|
@ -168,19 +187,32 @@ fn eval_suggest(suggest: &str, last_command: &str) -> String {
|
||||||
match_list = split[1].split(',').collect::<Vec<&str>>();
|
match_list = split[1].split(',').collect::<Vec<&str>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
let command = last_command.split_whitespace().collect::<Vec<&str>>()[command_index];
|
let command = split_command(last_command);
|
||||||
let match_list = match_list
|
let match_list = match_list
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.to_string())
|
.map(|s| s.to_string())
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
let suggestion = suggest_typo(command, match_list.clone());
|
let command_index = command_index as usize;
|
||||||
|
let suggestion = suggest_typo(&command[command_index], match_list.clone());
|
||||||
|
|
||||||
suggest = suggest.replace(&suggest[start_index..end_index], &suggestion);
|
suggest = suggest.replace(&suggest[start_index..end_index], &suggestion);
|
||||||
}
|
}
|
||||||
|
println!("eval suggest: {}", suggest);
|
||||||
|
|
||||||
suggest
|
suggest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn split_command(command: &str) -> Vec<String> {
|
||||||
|
use regex::Regex;
|
||||||
|
let regex = r#"([^\s"\\]+|"(?:\\.|[^"\\])*"|\\.)+"#;
|
||||||
|
let regex = Regex::new(regex).unwrap();
|
||||||
|
let split_command = regex
|
||||||
|
.find_iter(command)
|
||||||
|
.map(|cap| cap.as_str().to_owned())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
split_command
|
||||||
|
}
|
||||||
|
|
||||||
fn suggest_typo(typo: &str, candidates: Vec<String>) -> String {
|
fn suggest_typo(typo: &str, candidates: Vec<String>) -> String {
|
||||||
let mut suggestion = typo.to_owned();
|
let mut suggestion = typo.to_owned();
|
||||||
|
|
||||||
|
|
@ -193,7 +225,10 @@ fn suggest_typo(typo: &str, candidates: Vec<String>) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"file" => {
|
"file" => {
|
||||||
unimplemented!();
|
let files = get_directory_files(typo);
|
||||||
|
if let Some(suggest) = find_fimilar(typo, files) {
|
||||||
|
suggestion = suggest;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +242,6 @@ fn suggest_typo(typo: &str, candidates: Vec<String>) -> String {
|
||||||
fn get_path_files() -> Vec<String> {
|
fn get_path_files() -> Vec<String> {
|
||||||
let path = std::env::var("PATH").unwrap();
|
let path = std::env::var("PATH").unwrap();
|
||||||
let path = path.split(':').collect::<Vec<&str>>();
|
let path = path.split(':').collect::<Vec<&str>>();
|
||||||
// get all executable files in $PATH
|
|
||||||
let mut all_executable = vec![];
|
let mut all_executable = vec![];
|
||||||
for p in path {
|
for p in path {
|
||||||
let files = match std::fs::read_dir(p) {
|
let files = match std::fs::read_dir(p) {
|
||||||
|
|
@ -223,6 +257,28 @@ fn get_path_files() -> Vec<String> {
|
||||||
all_executable
|
all_executable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_directory_files(input: &str) -> Vec<String> {
|
||||||
|
let mut input = input
|
||||||
|
.trim_matches(|c| c == '\'' || c == '"')
|
||||||
|
.to_owned();
|
||||||
|
let files = loop {
|
||||||
|
match std::fs::read_dir(&input) {
|
||||||
|
Ok(files) => break files,
|
||||||
|
Err(_) => {
|
||||||
|
input = input.rsplit('/').nth(1).unwrap().to_owned();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut all_files = vec![];
|
||||||
|
for file in files {
|
||||||
|
let file = file.unwrap();
|
||||||
|
let file_name = file.path().to_str().unwrap().to_owned();
|
||||||
|
all_files.push(file_name);
|
||||||
|
}
|
||||||
|
all_files
|
||||||
|
}
|
||||||
|
|
||||||
fn find_fimilar(typo: &str, candidates: Vec<String>) -> Option<String> {
|
fn find_fimilar(typo: &str, candidates: Vec<String>) -> Option<String> {
|
||||||
let mut min_distance = 10;
|
let mut min_distance = 10;
|
||||||
let mut min_distance_index = None;
|
let mut min_distance_index = None;
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,15 @@ fn main() {
|
||||||
let corrected_command = corrections::correct_command(&shell, &last_command);
|
let corrected_command = corrections::correct_command(&shell, &last_command);
|
||||||
|
|
||||||
if let Some(corrected_command) = corrected_command {
|
if let Some(corrected_command) = corrected_command {
|
||||||
|
if corrected_command != last_command {
|
||||||
corrections::confirm_correction(&shell, &corrected_command, &last_command);
|
corrections::confirm_correction(&shell, &corrected_command, &last_command);
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"No correction found for the command: {}\n",
|
"No correction found for the command: {}\n",
|
||||||
last_command.red().bold()
|
last_command.red().bold()
|
||||||
);
|
);
|
||||||
println!("If you think there should be a correction, please open an issue or send a pull request!");
|
println!("If you think there should be a correction, please open an issue or send a pull request!");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
use crate::corrections::split_command;
|
||||||
|
|
||||||
pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String {
|
pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String {
|
||||||
let mut highlighted_command = String::new();
|
let mut highlighted_command = String::new();
|
||||||
|
|
||||||
let split_corrected_command = corrected_command.split(' ');
|
let split_corrected_command = split_command(corrected_command);
|
||||||
let split_last_command = last_command.split(' ');
|
let split_last_command = split_command(last_command);
|
||||||
|
|
||||||
for new in split_corrected_command {
|
for new in split_corrected_command {
|
||||||
if new == "" {
|
if new == "" {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue