Autocomplete fonctionne

This commit is contained in:
Justine
2023-01-31 12:01:33 +01:00
parent a249d4d788
commit 6583128022
6 changed files with 168 additions and 46 deletions

View File

@ -8,11 +8,10 @@ pub mod shell {
use std::env;
use std::str;
use std::process::Stdio;
use users::{get_user_by_uid, get_current_uid};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::{color, cursor};
use termion::cursor;
use unicode_segmentation::UnicodeSegmentation;
mod history;
@ -21,6 +20,8 @@ pub mod shell {
mod autocomplete;
use crate::shell::autocomplete::*;
mod prompt;
use crate::shell::prompt::*;
//used for home directory
extern crate dirs;
@ -54,6 +55,11 @@ pub mod shell {
Err(e) => eprintln!(" Err: {}", e),
}
},
"ssh" => {
let ssh_args = args.peekable().peek().map_or(" ", |x| *x);
let mut child = Command::new("ssh").arg(ssh_args).spawn().unwrap();
let _ = child.wait().unwrap();
},
command => {
if commands.peek().is_some() {
let stdin = match previous_command {
@ -104,44 +110,6 @@ pub mod shell {
return resultat;
}
fn build_prompt(curr_number: &i32) -> String {
let user = String::from(
get_user_by_uid(get_current_uid())
.unwrap()
.name()
.to_str()
.unwrap()
);
let host = String::from(
gethostname::gethostname()
.to_str()
.unwrap()
);
let current_dir = String::from(
env::current_dir()
.unwrap()
.to_str()
.unwrap()
);
let s = current_dir.split('/');
let dir_last = String::from(
s.last()
.unwrap()
);
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
color::Fg(color::LightBlack),
curr_number,
color::Fg(color::LightCyan),
user, host, dir_last,
color::Fg(color::Reset)));
return prompt;
}
fn replace_signs(line: &String) -> String {
let mut ayo = String::from(line);
if ayo.contains('~') {
@ -242,9 +210,12 @@ pub mod shell {
match c.unwrap() {
Key::Char('\t') => {
let res = autocomplete(&mycommand);
let (res, list) = autocomplete(&mycommand);
write!(stdout, "\r\n{}\r\n", list);
mycommand.clear();
mycommand.push_str(&res);
max_pos = res.len();
current_pos = max_pos;
write!(stdout, "\r\n{}{}", prompt, res);
stdout.flush();
}
@ -281,7 +252,13 @@ pub mod shell {
},
Key::Ctrl('c') => {
continue;
current_pos = 0;
max_pos = 0;
mycommand.clear();
write!(stdout, "\r\n--CANCEL--\r\n");
current_number = get_curr_history_number();
prompt = build_prompt(&current_number);
write!(stdout, "{}", prompt);
},
Key::Ctrl('t') => {

View File

@ -1,8 +1,50 @@
pub fn autocomplete(input: &String) -> String {
use which::which_re;
use regex::Regex;
use std::path::PathBuf;
pub fn autocomplete(input: &String) -> (String, String) {
let faketab = format!(" --This is a fake output for autocomplete from {input}-- ");
//In reality, we would keep the input and append to it
let aaa = String::from(faketab);
return aaa;
let found_bins = find_bin(input);
if found_bins.len() == 1 {
let aaa = found_bins.clone();
let bbb = &aaa[0]
.iter()
.last()
.unwrap();
let res_string = String::from(bbb.to_str().unwrap());
let res_list = res_string.clone();
return (res_string, res_list);
} else if found_bins.len() == 0 {
let list = String::from("Nothing adequate.");
let res = String::from(input);
return (res, list);
} else {
let mut all_res = String::new();
for path in found_bins {
let buff = String::from(path
.iter()
.last()
.unwrap()
.to_str()
.unwrap());
let res_line = format!("* {}\r\n", buff);
all_res.push_str(&res_line);
}
let first_res = String::from(input);
return(first_res, all_res)
}
}
///Takes a string and returns a Vector of PathBuf containing all matchings
///commands
fn find_bin(command: &String) -> Vec<PathBuf> {
let re = format!("^{}.*", command);
let re = Regex::new(&re).unwrap();
let binaries: Vec<PathBuf> = which_re(re).unwrap().collect();
return binaries;
}

View File

@ -53,6 +53,7 @@ pub fn get_history() -> Result<String, std::io::Error> {
let myline = format!("\r\n{}", line.unwrap());
res.push_str(&myline);
}
res.push_str("\r\n");
return Ok(res);
}

42
src/shell/prompt.rs Normal file
View File

@ -0,0 +1,42 @@
use users::{get_user_by_uid, get_current_uid};
use termion::color;
use std::env;
pub fn build_prompt(curr_number: &i32) -> String {
let user = String::from(
get_user_by_uid(get_current_uid())
.unwrap()
.name()
.to_str()
.unwrap()
);
let host = String::from(
gethostname::gethostname()
.to_str()
.unwrap()
);
let current_dir = String::from(
env::current_dir()
.unwrap()
.to_str()
.unwrap()
);
let s = current_dir.split('/');
let dir_last = String::from(
s.last()
.unwrap()
);
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
color::Fg(color::LightBlack),
curr_number,
color::Fg(color::LightCyan),
user, host, dir_last,
color::Fg(color::Reset)));
return prompt;
}