Fiddled more with autocompletion

This commit is contained in:
Justine
2023-02-07 01:34:43 +01:00
parent ac775f8ce0
commit aa9cbb323d

View File

@ -2,6 +2,7 @@ use which::which_re;
use regex::Regex; use regex::Regex;
use std::path::PathBuf; use std::path::PathBuf;
use std::{env, fs}; use std::{env, fs};
use termion::color;
#[derive(Debug)] #[derive(Debug)]
enum SearchType { enum SearchType {
@ -36,7 +37,10 @@ impl Search {
let mut words = input.split(' ').collect::<Vec<&str>>(); let mut words = input.split(' ').collect::<Vec<&str>>();
let buff_searchee = String::from(*words.last().unwrap_or(&"")); let buff_searchee = String::from(*words.last().unwrap_or(&""));
words.pop(); words.pop();
let input_cmd = words.join(" "); let mut input_cmd = words.join(" ");
if input_cmd.len() > 0 {
input_cmd.push_str(" ");
}
//Are we looking in a specific path ? //Are we looking in a specific path ?
//eg. am I doing cat mystuff.txt or cat folder/subfolder/mystuff.txt ? //eg. am I doing cat mystuff.txt or cat folder/subfolder/mystuff.txt ?
@ -87,7 +91,7 @@ impl Search {
///A found file is preferred to a found command ///A found file is preferred to a found command
pub fn autocomplete(&self) -> (String, String) { pub fn autocomplete(&self) -> (String, String) {
let (mut res, res_lines) = match &self.search_type { let (mut res, res_lines) = match &self.search_type {
SearchType::CmdSearch => autocomplete_cmd(&self.searchee), SearchType::CmdSearch => autocomplete_cmd(&self.searchee, &self),
SearchType::FileSearch => match autocomplete_file(&self) { SearchType::FileSearch => match autocomplete_file(&self) {
Ok(t) => t, Ok(t) => t,
Err(_) => (String::new(), String::new()), Err(_) => (String::new(), String::new()),
@ -103,6 +107,11 @@ impl Search {
fn discriminate_search_type(input: &String) -> SearchType { fn discriminate_search_type(input: &String) -> SearchType {
let tamere = input.clone(); let tamere = input.clone();
//./Means we want to execute something in place
if input.starts_with("./") || input.starts_with(" ./") {
return SearchType::FileSearch;
}
let mut a = tamere.split(" ").collect::<Vec<&str>>(); let mut a = tamere.split(" ").collect::<Vec<&str>>();
let _y = String::from(a.pop().unwrap()); let _y = String::from(a.pop().unwrap());
let mut x = String::from(a.join(" ").trim()); let mut x = String::from(a.join(" ").trim());
@ -110,6 +119,7 @@ impl Search {
x.push_str(" ") x.push_str(" ")
}; };
if x.pop() == Some(' ') { if x.pop() == Some(' ') {
if x.pop() == Some('|') { if x.pop() == Some('|') {
return SearchType::CmdSearch; return SearchType::CmdSearch;
@ -148,21 +158,26 @@ fn autocomplete_file(search: &Search) -> std::io::Result<(String, String)> {
*&mut results.push(format!("{}{}", &search.search_path.display(), filename)); *&mut results.push(format!("{}{}", &search.search_path.display(), filename));
} }
*&mut nbr_found += 1; *&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 //if what we found is a folder, we add an / after
let fullpath = format!("{}/{}", &search.search_path.display(), filename); let fullpath = format!("{}/{}", &search.search_path.display(), filename);
let mut ending = String::new(); let mut ending = String::new();
let mut matchline = format!("\r\n{}", filename);
match fs::metadata(fullpath) { match fs::metadata(fullpath) {
Ok(e) => { Ok(e) => {
//Will be added to the end of the result //Will be added to the end of the result
if e.is_dir() { if e.is_dir() {
*&mut matchline = format!("{}{}/{}",
color::Fg(color::Green),
matchline,
color::Fg(color::Reset)
);
*&mut ending.push_str("/"); *&mut ending.push_str("/");
} }
}, },
Err(_) => (), Err(_) => (),
}; };
&mut all_res.push_str(&matchline);
*&mut last_found = format!("{}{}", filename, ending); *&mut last_found = format!("{}{}", filename, ending);
} }
} }
@ -176,18 +191,17 @@ fn autocomplete_file(search: &Search) -> std::io::Result<(String, String)> {
//Handle the path when tabbing in a long path, zsh-like //Handle the path when tabbing in a long path, zsh-like
//then return the value //then return the value
let xxx = &search.search_path.clone().into_os_string().into_string().unwrap(); let xxx = &search.search_path.clone().into_os_string().into_string().unwrap();
let mut return_val = String::new();
if !&search.local_search && !&last_found.contains(&xxx.as_str()) { if !&search.local_search && !&last_found.contains(&xxx.as_str()) {
let return_val = format!("{} {}{}", &search.command, &search.search_path.display(), last_found); *&mut return_val = format!("{}{}{}", &search.command, &search.search_path.display(), last_found);
return Ok((return_val, all_res));
} else { } else {
let return_val = format!("{} {}", &search.command, last_found); *&mut return_val = format!("{}{}", &search.command, last_found);
return Ok((return_val, all_res));
} }
return Ok((return_val, all_res));
} }
fn autocomplete_cmd(input: &String) -> (String, String) { fn autocomplete_cmd(input: &String, search: &Search) -> (String, String) {
let found_bins = find_bin(input); let found_bins = find_bin(input);
if found_bins.len() == 1 { if found_bins.len() == 1 {
let aaa = found_bins.clone(); let aaa = found_bins.clone();
@ -195,7 +209,7 @@ fn autocomplete_cmd(input: &String) -> (String, String) {
.iter() .iter()
.last() .last()
.unwrap(); .unwrap();
let res_string = String::from(bbb.to_str().unwrap()); let res_string = format!("{}{}", search.command, String::from(bbb.to_str().unwrap()));
let res_list = res_string.clone(); let res_list = res_string.clone();
return (res_string, res_list); return (res_string, res_list);
} else if found_bins.len() == 0 { } else if found_bins.len() == 0 {
@ -223,7 +237,8 @@ fn autocomplete_cmd(input: &String) -> (String, String) {
} }
} }
let first_res = String::from(input); let first_res = String::from(input);
return(first_res, all_res) let ret_line = format!("{}{}", &search.command, first_res);
return(ret_line, all_res);
} }
} }