From a2726d45574cf4549975be3adf5659d4d04f4ce7 Mon Sep 17 00:00:00 2001 From: Justine Date: Sun, 5 Feb 2023 00:14:55 +0100 Subject: [PATCH] Fixed crashes on some permissions issues --- src/shell/autocomplete.rs | 57 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/shell/autocomplete.rs b/src/shell/autocomplete.rs index d84a396..aad587d 100644 --- a/src/shell/autocomplete.rs +++ b/src/shell/autocomplete.rs @@ -30,7 +30,7 @@ impl Search { ///Returns a Search instance, taking the command line input. pub fn build(input: &String) -> std::io::Result { - let search_type = Self::discriminate_search_type(input); + let search_type = Self::discriminate_search_type(&input); //Getting cmd and searched string let mut words = input.split(' ').collect::>(); @@ -92,23 +92,8 @@ impl Search { Ok(t) => t, Err(_) => (String::new(), String::new()), } - }; - match &self.search_type { - SearchType::FileSearch => { - let xxx = &self.search_path.clone().into_os_string().into_string().unwrap(); - if !&self.local_search && !res.contains(&xxx.as_str()) { - let return_val = format!("{} {}{}", &self.command, &self.search_path.display(), res); - return(return_val, res_lines); - } else { - let return_val = format!("{} {}", &self.command, res); - return(return_val, res_lines); - } - }, - SearchType::CmdSearch => { - return (res, res_lines); - }, - } + return (res, res_lines); } fn discriminate_search_type(input: &String) -> SearchType { @@ -163,25 +148,37 @@ fn autocomplete_file(search: &Search) -> std::io::Result<(String, String)> { //if what we found is a folder, we add an / after let fullpath = format!("{}/{}", &search.search_path.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("/"); - } - + match fs::metadata(fullpath) { + Ok(e) => { + //Will be added to the end of the result + if e.is_dir() { + *&mut ending.push_str("/"); + } + }, + Err(_) => (), + }; *&mut last_found = format!("{}{}", 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 { + + //Handle returning the longest sequence of characters between two or more matching files + if nbr_found > 1 { let longest = find_common_chars(results); - return Ok((longest, all_res)); + *&mut last_found = longest; } + + //Handle the path when tabbing in a long path, zsh-like + //then return the value + let xxx = &search.search_path.clone().into_os_string().into_string().unwrap(); + if !&search.local_search && !&last_found.contains(&xxx.as_str()) { + let return_val = format!("{} {}{}", &search.command, &search.search_path.display(), last_found); + return Ok((return_val, all_res)); + } else { + let return_val = format!("{} {}", &search.command, last_found); + return Ok((return_val, all_res)); + } + }