Autocomplete works for files
This commit is contained in:
@ -1,10 +1,78 @@
|
||||
use which::which_re;
|
||||
use regex::Regex;
|
||||
use std::path::PathBuf;
|
||||
use std::{env, fs};
|
||||
|
||||
pub fn autocomplete(input: &String) -> (String, String) {
|
||||
let faketab = format!(" --This is a fake output for autocomplete from {input}-- ");
|
||||
|
||||
pub fn autocomplete(input: &String, prev_match: &String) -> (String, String) {
|
||||
if !input.contains(' ') && !input.contains("./") {
|
||||
return autocomplete_cmd(input);
|
||||
} else {
|
||||
return autocomplete_file(input, prev_match).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
///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)> {
|
||||
|
||||
//Keep the last part of the cmd...
|
||||
let input_searchee = input
|
||||
.split(' ')
|
||||
.collect::<Vec<&str>>()
|
||||
.last()
|
||||
.copied();
|
||||
|
||||
//And also the beginning !
|
||||
let mut input_buff = input
|
||||
.split(' ')
|
||||
.collect::<Vec<&str>>();
|
||||
_ = input_buff.pop();
|
||||
let mut input_origin = format!("{} ", input_buff.join(" "));
|
||||
|
||||
match input_searchee {
|
||||
Some(s) => {
|
||||
//Search happens 'round here
|
||||
let current_dir = env::current_dir()?;
|
||||
let mut all_res = String::new();
|
||||
let mut nbr_found: u16 = 0;
|
||||
let mut last_found = String::new();
|
||||
|
||||
for file in fs::read_dir(current_dir)? {
|
||||
let filepath = file.unwrap().path();
|
||||
let filename = filepath
|
||||
.iter()
|
||||
.last()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
let re = format!("^{}.*$", s);
|
||||
let regex = Regex::new(&re).unwrap();
|
||||
if regex.is_match(filename) {
|
||||
*&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 {
|
||||
*&mut last_found = format!("{}{}", &input_origin, filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
//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
|
||||
} else {
|
||||
return Ok((String::from(input), all_res));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Ok((String::from(""), String::from("")));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn autocomplete_cmd(input: &String) -> (String, String) {
|
||||
let found_bins = find_bin(input);
|
||||
if found_bins.len() == 1 {
|
||||
let aaa = found_bins.clone();
|
||||
|
Reference in New Issue
Block a user