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') => {