feat: multi-directory typo suggestion

This commit is contained in:
iff 2023-08-07 17:12:38 +02:00
parent 77b0793076
commit b9943fb777
2 changed files with 29 additions and 15 deletions

View file

@ -1,3 +1,5 @@
use crate::suggestions::find_similar;
pub fn get_path_files() -> Vec<String> {
let path = std::env::var("PATH").unwrap();
let path = path.split(':').collect::<Vec<&str>>();
@ -16,26 +18,40 @@ pub fn get_path_files() -> Vec<String> {
all_executable
}
pub fn get_directory_files(input: &str) -> Vec<String> {
pub fn get_best_match_file(input: &str) -> Option<String> {
let mut input = input.trim_matches(|c| c == '\'' || c == '"').to_owned();
let files = loop {
let mut exit_dirs = Vec::new();
let mut files = loop {
match std::fs::read_dir(&input) {
Ok(files) => break files,
Err(_) => {
if let Some((dirs, _)) = input.rsplit_once('/') {
if let Some((dirs, exit_dir)) = input.rsplit_once('/') {
exit_dirs.push(exit_dir.to_owned());
input = dirs.to_owned();
} else {
exit_dirs.push(input.to_owned());
input = ".".to_owned();
break std::fs::read_dir("./").unwrap();
}
}
}
};
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);
while let Some(exit_dir) = exit_dirs.pop() {
let dir_files = files.filter_map(|file| {
let file = file.unwrap();
let file_name = file.file_name().into_string().unwrap();
Some(file_name)
}).collect::<Vec<String>>();
let best_match = find_similar(&exit_dir, dir_files);
input = format!("{}/{}", input, best_match.unwrap());
files = match std::fs::read_dir(&input) {
Ok(files) => files,
Err(_) => return Some(input),
};
}
all_files
Some(input)
}