Autocomplete over, I guess
This commit is contained in:
parent
c65438ed6c
commit
1b57352393
@ -4,11 +4,11 @@ use std::path::PathBuf;
|
||||
use std::{env, fs};
|
||||
|
||||
|
||||
pub fn autocomplete(input: &String, prev_match: &String) -> (String, String) {
|
||||
pub fn autocomplete(input: &String) -> (String, String) {
|
||||
if !input.contains(' ') && !input.contains("./") {
|
||||
return autocomplete_cmd(input);
|
||||
} else {
|
||||
match autocomplete_file(input, prev_match) {
|
||||
match autocomplete_file(input) {
|
||||
Ok(t) => return t,
|
||||
Err(e) => {
|
||||
println!("\n\r{}", e);
|
||||
@ -20,7 +20,7 @@ pub fn autocomplete(input: &String, prev_match: &String) -> (String, String) {
|
||||
|
||||
///Look for a file using the string after the last space in the input,
|
||||
///in the current folder.
|
||||
fn autocomplete_file(input: &String, prev_match: &String) -> std::io::Result<(String, String)> {
|
||||
fn autocomplete_file(input: &String) -> std::io::Result<(String, String)> {
|
||||
|
||||
//Keep the last part of the cmd...
|
||||
let mut input_searchee = match input
|
||||
@ -47,15 +47,22 @@ fn autocomplete_file(input: &String, prev_match: &String) -> std::io::Result<(St
|
||||
//Where do we search ?
|
||||
let mut folder_to_search = PathBuf::new();
|
||||
//Display folder in search result ?
|
||||
let mut searching_current = false;
|
||||
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('/') {
|
||||
//Remove the last part
|
||||
let toremove = &input_searchee.split('/').last().unwrap();
|
||||
let tosearch = &input_searchee.replace(toremove, "");
|
||||
//Correctly taking first and last part...
|
||||
let splitted_searchstring = &input_searchee.split('/');
|
||||
let mut 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 = &toremove;
|
||||
*&mut searching_current = true;
|
||||
*&mut input_searchee = &trimmed_searchee;
|
||||
*&mut searching_not_current = true;
|
||||
//Searching the current folder
|
||||
} else {
|
||||
folder_to_search = env::current_dir()?;
|
||||
@ -77,7 +84,7 @@ fn autocomplete_file(input: &String, prev_match: &String) -> std::io::Result<(St
|
||||
let re = format!("^{}.*$", input_searchee);
|
||||
let regex = Regex::new(&re).unwrap();
|
||||
if regex.is_match(filename) {
|
||||
if !searching_current {
|
||||
if !searching_not_current {
|
||||
*&mut results.push(format!("{}", filename));
|
||||
} else {
|
||||
*&mut results.push(format!("{}{}", &folder_to_search.display(), filename));
|
||||
@ -85,18 +92,33 @@ fn autocomplete_file(input: &String, prev_match: &String) -> std::io::Result<(St
|
||||
*&mut nbr_found += 1;
|
||||
let matchline = &format!("\r\n* {}", filename);
|
||||
&mut all_res.push_str(&matchline);
|
||||
//No need to find things we already found
|
||||
if *prev_match != filename {
|
||||
if searching_current {
|
||||
*&mut last_found = format!("{}{}{}", &input_origin, &folder_to_search.display(), filename);
|
||||
} else {
|
||||
*&mut last_found = format!("{}{}", &input_origin, filename);
|
||||
}
|
||||
|
||||
//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 {
|
||||
@ -154,11 +176,11 @@ fn find_common_chars(mut strvec: Vec<String>) -> String {
|
||||
strvec.remove(0);
|
||||
let mut answer = String::new();
|
||||
let mut counter: usize = 0;
|
||||
let mut still_ok = true;
|
||||
|
||||
for letter in first_word.chars() {
|
||||
let mut still_ok = true;
|
||||
for word in &strvec {
|
||||
if still_ok == true {
|
||||
if *&still_ok == true {
|
||||
match word.chars().nth(counter) {
|
||||
Some(l) => {
|
||||
if l != letter {
|
||||
@ -176,7 +198,7 @@ fn find_common_chars(mut strvec: Vec<String>) -> String {
|
||||
*&mut counter += 1;
|
||||
}
|
||||
}
|
||||
answer.pop();
|
||||
//answer.pop();
|
||||
return answer;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user