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};
|
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("./") {
|
if !input.contains(' ') && !input.contains("./") {
|
||||||
return autocomplete_cmd(input);
|
return autocomplete_cmd(input);
|
||||||
} else {
|
} else {
|
||||||
match autocomplete_file(input, prev_match) {
|
match autocomplete_file(input) {
|
||||||
Ok(t) => return t,
|
Ok(t) => return t,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("\n\r{}", 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,
|
///Look for a file using the string after the last space in the input,
|
||||||
///in the current folder.
|
///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...
|
//Keep the last part of the cmd...
|
||||||
let mut input_searchee = match input
|
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 ?
|
//Where do we search ?
|
||||||
let mut folder_to_search = PathBuf::new();
|
let mut folder_to_search = PathBuf::new();
|
||||||
//Display folder in search result ?
|
//Display folder in search result ?
|
||||||
let mut searching_current = false;
|
let mut searching_not_current = false;
|
||||||
//Searching somewhere else...
|
//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('/') {
|
if input_searchee.contains('/') {
|
||||||
//Remove the last part
|
//Correctly taking first and last part...
|
||||||
let toremove = &input_searchee.split('/').last().unwrap();
|
let splitted_searchstring = &input_searchee.split('/');
|
||||||
let tosearch = &input_searchee.replace(toremove, "");
|
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);
|
folder_to_search.push(tosearch);
|
||||||
*&mut input_searchee = &toremove;
|
*&mut input_searchee = &trimmed_searchee;
|
||||||
*&mut searching_current = true;
|
*&mut searching_not_current = true;
|
||||||
//Searching the current folder
|
//Searching the current folder
|
||||||
} else {
|
} else {
|
||||||
folder_to_search = env::current_dir()?;
|
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 re = format!("^{}.*$", input_searchee);
|
||||||
let regex = Regex::new(&re).unwrap();
|
let regex = Regex::new(&re).unwrap();
|
||||||
if regex.is_match(filename) {
|
if regex.is_match(filename) {
|
||||||
if !searching_current {
|
if !searching_not_current {
|
||||||
*&mut results.push(format!("{}", filename));
|
*&mut results.push(format!("{}", filename));
|
||||||
} else {
|
} else {
|
||||||
*&mut results.push(format!("{}{}", &folder_to_search.display(), filename));
|
*&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;
|
*&mut nbr_found += 1;
|
||||||
let matchline = &format!("\r\n* {}", filename);
|
let matchline = &format!("\r\n* {}", filename);
|
||||||
&mut all_res.push_str(&matchline);
|
&mut all_res.push_str(&matchline);
|
||||||
//No need to find things we already found
|
|
||||||
if *prev_match != filename {
|
//if what we found is a folder, we add an / after
|
||||||
if searching_current {
|
let fullpath = format!("{}/{}", &folder_to_search.display(), filename);
|
||||||
*&mut last_found = format!("{}{}{}", &input_origin, &folder_to_search.display(), filename);
|
let md = fs::metadata(fullpath).unwrap();
|
||||||
} else {
|
//Will be added to the end of the result
|
||||||
*&mut last_found = format!("{}{}", &input_origin, filename);
|
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
|
//Found one or zero, use what has been found directly
|
||||||
if nbr_found < 2 {
|
if nbr_found < 2 {
|
||||||
|
|
||||||
return Ok((last_found, String::new()));
|
return Ok((last_found, String::new()));
|
||||||
//Otherwise, just display a list and use what all the results have in common
|
//Otherwise, just display a list and use what all the results have in common
|
||||||
} else {
|
} else {
|
||||||
@ -154,11 +176,11 @@ fn find_common_chars(mut strvec: Vec<String>) -> String {
|
|||||||
strvec.remove(0);
|
strvec.remove(0);
|
||||||
let mut answer = String::new();
|
let mut answer = String::new();
|
||||||
let mut counter: usize = 0;
|
let mut counter: usize = 0;
|
||||||
|
let mut still_ok = true;
|
||||||
|
|
||||||
for letter in first_word.chars() {
|
for letter in first_word.chars() {
|
||||||
let mut still_ok = true;
|
|
||||||
for word in &strvec {
|
for word in &strvec {
|
||||||
if still_ok == true {
|
if *&still_ok == true {
|
||||||
match word.chars().nth(counter) {
|
match word.chars().nth(counter) {
|
||||||
Some(l) => {
|
Some(l) => {
|
||||||
if l != letter {
|
if l != letter {
|
||||||
@ -176,7 +198,7 @@ fn find_common_chars(mut strvec: Vec<String>) -> String {
|
|||||||
*&mut counter += 1;
|
*&mut counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
answer.pop();
|
//answer.pop();
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user