Removed some backup file
This commit is contained in:
parent
05d6a40efb
commit
9d5fd0428c
@ -1,205 +0,0 @@
|
|||||||
use which::which_re;
|
|
||||||
use regex::Regex;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::{env, fs};
|
|
||||||
|
|
||||||
|
|
||||||
pub fn autocomplete(input: &String) -> (String, String) {
|
|
||||||
if !input.contains(' ') && !input.contains("./") {
|
|
||||||
return autocomplete_cmd(input);
|
|
||||||
} else {
|
|
||||||
match autocomplete_file(input) {
|
|
||||||
Ok(t) => return t,
|
|
||||||
Err(e) => {
|
|
||||||
println!("\n\r{}", e);
|
|
||||||
return (String::from(input), String::new());
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Look for a file using the string after the last space in the input,
|
|
||||||
///in the current folder.
|
|
||||||
fn autocomplete_file(input: &String) -> std::io::Result<(String, String)> {
|
|
||||||
|
|
||||||
//Keep the last part of the cmd...
|
|
||||||
let mut input_searchee = match input
|
|
||||||
.split(' ')
|
|
||||||
.collect::<Vec<&str>>()
|
|
||||||
.last()
|
|
||||||
.copied() {
|
|
||||||
Some(c) => c,
|
|
||||||
None => {
|
|
||||||
return Ok((String::from(""), String::from("")));
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
//And also the beginning !
|
|
||||||
let mut input_buff = input
|
|
||||||
.split(' ')
|
|
||||||
.collect::<Vec<&str>>();
|
|
||||||
_ = input_buff.pop();
|
|
||||||
let input_origin = format!("{} ", input_buff.join(" "));
|
|
||||||
|
|
||||||
//Store the results
|
|
||||||
let mut results = Vec::new();
|
|
||||||
|
|
||||||
//Where do we search ?
|
|
||||||
let mut folder_to_search = PathBuf::new();
|
|
||||||
//Display folder in search result ?
|
|
||||||
let mut searching_not_current = false;
|
|
||||||
//Searching somewhere else...
|
|
||||||
//Here we need to correctly divide the string into the folder
|
|
||||||
//and the searched file itself. The next 10 lines or so of code
|
|
||||||
//suck, but sometimes Rust pisses in your boots.
|
|
||||||
if input_searchee.contains('/') {
|
|
||||||
//Correctly taking first and last part...
|
|
||||||
let splitted_searchstring = &input_searchee.split('/');
|
|
||||||
let trimmed_searchee = &splitted_searchstring.clone().last().unwrap();
|
|
||||||
let splitted_searchstring = &mut input_searchee.clone().split('/').collect::<Vec<&str>>();
|
|
||||||
splitted_searchstring.pop();
|
|
||||||
let mut tosearch = splitted_searchstring.join("/");
|
|
||||||
tosearch.push_str("/");
|
|
||||||
folder_to_search.push(tosearch);
|
|
||||||
*&mut input_searchee = &trimmed_searchee;
|
|
||||||
*&mut searching_not_current = true;
|
|
||||||
//Searching the current folder
|
|
||||||
} else {
|
|
||||||
folder_to_search = env::current_dir()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Search happens 'round here
|
|
||||||
let mut all_res = String::new();
|
|
||||||
let mut nbr_found: u16 = 0;
|
|
||||||
let mut last_found = String::new();
|
|
||||||
|
|
||||||
for file in fs::read_dir(&folder_to_search)? {
|
|
||||||
let filepath = file.unwrap().path();
|
|
||||||
let filename = filepath
|
|
||||||
.iter()
|
|
||||||
.last()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
|
||||||
let re = format!("^{}.*$", input_searchee);
|
|
||||||
let regex = Regex::new(&re).unwrap();
|
|
||||||
if regex.is_match(filename) {
|
|
||||||
if !searching_not_current {
|
|
||||||
*&mut results.push(format!("{}", filename));
|
|
||||||
} else {
|
|
||||||
*&mut results.push(format!("{}{}", &folder_to_search.display(), filename));
|
|
||||||
}
|
|
||||||
*&mut nbr_found += 1;
|
|
||||||
let matchline = &format!("\r\n* {}", filename);
|
|
||||||
&mut all_res.push_str(&matchline);
|
|
||||||
|
|
||||||
//if what we found is a folder, we add an / after
|
|
||||||
let fullpath = format!("{}/{}", &folder_to_search.display(), filename);
|
|
||||||
let md = fs::metadata(fullpath).unwrap();
|
|
||||||
//Will be added to the end of the result
|
|
||||||
let mut ending = String::new();
|
|
||||||
if md.is_dir() {
|
|
||||||
*&mut ending.push_str("/");
|
|
||||||
}
|
|
||||||
|
|
||||||
if searching_not_current {
|
|
||||||
*&mut last_found = format!("{}{}{}{}",
|
|
||||||
&input_origin,
|
|
||||||
&folder_to_search.display(),
|
|
||||||
filename,
|
|
||||||
ending);
|
|
||||||
} else {
|
|
||||||
*&mut last_found = format!("{}{}{}",
|
|
||||||
&input_origin,
|
|
||||||
filename,
|
|
||||||
ending);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Found one or zero, use what has been found directly
|
|
||||||
if nbr_found < 2 {
|
|
||||||
|
|
||||||
return Ok((last_found, String::new()));
|
|
||||||
//Otherwise, just display a list and use what all the results have in common
|
|
||||||
} else {
|
|
||||||
let longest = find_common_chars(results);
|
|
||||||
let ret_string = format!("{}{}", input_origin, &longest);
|
|
||||||
return Ok((ret_string, all_res));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn autocomplete_cmd(input: &String) -> (String, String) {
|
|
||||||
let found_bins = find_bin(input);
|
|
||||||
if found_bins.len() == 1 {
|
|
||||||
let aaa = found_bins.clone();
|
|
||||||
let bbb = &aaa[0]
|
|
||||||
.iter()
|
|
||||||
.last()
|
|
||||||
.unwrap();
|
|
||||||
let res_string = String::from(bbb.to_str().unwrap());
|
|
||||||
let res_list = res_string.clone();
|
|
||||||
return (res_string, res_list);
|
|
||||||
} else if found_bins.len() == 0 {
|
|
||||||
let list = String::from("Nothing adequate.");
|
|
||||||
let res = String::from(input);
|
|
||||||
return (res, list);
|
|
||||||
} else {
|
|
||||||
let mut all_res = String::new();
|
|
||||||
for path in found_bins {
|
|
||||||
let buff = String::from(path
|
|
||||||
.iter()
|
|
||||||
.last()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap());
|
|
||||||
let res_line = format!("* {}\r\n", buff);
|
|
||||||
all_res.push_str(&res_line);
|
|
||||||
}
|
|
||||||
let first_res = String::from(input);
|
|
||||||
return(first_res, all_res)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Takes a string and returns a Vector of PathBuf containing all matchings
|
|
||||||
///commands
|
|
||||||
fn find_bin(command: &String) -> Vec<PathBuf> {
|
|
||||||
let re = format!("^{}.*", command);
|
|
||||||
let re = Regex::new(&re).unwrap();
|
|
||||||
let binaries: Vec<PathBuf> = which_re(re).unwrap().collect();
|
|
||||||
return binaries;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_common_chars(mut strvec: Vec<String>) -> String {
|
|
||||||
let first_word = strvec[0].clone();
|
|
||||||
strvec.remove(0);
|
|
||||||
let mut answer = String::new();
|
|
||||||
let mut counter: usize = 0;
|
|
||||||
let mut still_ok = true;
|
|
||||||
|
|
||||||
for letter in first_word.chars() {
|
|
||||||
for word in &strvec {
|
|
||||||
if *&still_ok == true {
|
|
||||||
match word.chars().nth(counter) {
|
|
||||||
Some(l) => {
|
|
||||||
if l != letter {
|
|
||||||
*&mut still_ok = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
*&mut still_ok = false;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if still_ok == true {
|
|
||||||
*&mut answer.push(letter);
|
|
||||||
*&mut counter += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//answer.pop();
|
|
||||||
return answer;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user