Flèche gauche fonctionne, en mode remplacement

This commit is contained in:
Justine 2022-12-25 17:45:46 +01:00
parent 19771dbd13
commit e9d9db1f08

View File

@ -8,6 +8,7 @@ pub mod shell {
use std::env; use std::env;
use std::str; use std::str;
use std::process::Stdio; use std::process::Stdio;
use std::num::Wrapping;
use users::{get_user_by_uid, get_current_uid}; use users::{get_user_by_uid, get_current_uid};
use termion::event::Key; use termion::event::Key;
use termion::input::TermRead; use termion::input::TermRead;
@ -104,7 +105,11 @@ pub mod shell {
return resultat; return resultat;
} }
fn build_prompt(curr_number: &i32) -> String { fn build_prompt(curr_number: &i32) -> (String, usize) {
let mut length: usize = 9;
length += curr_number.to_string().len();
let user = String::from( let user = String::from(
get_user_by_uid(get_current_uid()) get_user_by_uid(get_current_uid())
.unwrap() .unwrap()
@ -113,12 +118,16 @@ pub mod shell {
.unwrap() .unwrap()
); );
length += user.chars().count();
let host = String::from( let host = String::from(
gethostname::gethostname() gethostname::gethostname()
.to_str() .to_str()
.unwrap() .unwrap()
); );
length += host.chars().count();
let current_dir = String::from( let current_dir = String::from(
env::current_dir() env::current_dir()
.unwrap() .unwrap()
@ -132,13 +141,15 @@ pub mod shell {
.unwrap() .unwrap()
); );
length += dir_last.chars().count();
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ", let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
color::Fg(color::LightBlack), color::Fg(color::LightBlack),
curr_number, curr_number,
color::Fg(color::LightCyan), color::Fg(color::LightCyan),
user, host, dir_last, user, host, dir_last,
color::Fg(color::Reset))); color::Fg(color::Reset)));
return prompt; return (prompt, length);
} }
fn replace_signs(line: &String) -> String { fn replace_signs(line: &String) -> String {
@ -205,7 +216,7 @@ pub mod shell {
//Used in conjunction with Up arrow to go back in history //Used in conjunction with Up arrow to go back in history
//Resetted by pressing Enter //Resetted by pressing Enter
let mut current_number = get_curr_history_number(); let mut current_number = get_curr_history_number();
let mut prompt = build_prompt(&current_number); let (mut prompt, mut pr_len) = build_prompt(&current_number);
//Initialize //Initialize
write!(stdout, "\r\n SquiShell (sqish)--- \r\n{}", prompt); write!(stdout, "\r\n SquiShell (sqish)--- \r\n{}", prompt);
@ -224,7 +235,7 @@ pub mod shell {
Key::Char('\n') => { Key::Char('\n') => {
current_number = get_curr_history_number(); current_number = get_curr_history_number();
prompt = build_prompt(&current_number); (prompt, pr_len) = build_prompt(&current_number);
if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) { if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) {
let comm = replace_signs(&mycommand); let comm = replace_signs(&mycommand);
let res = handle_input(&comm); let res = handle_input(&comm);
@ -234,7 +245,7 @@ pub mod shell {
write!(stdout, "\r\n{}", line); write!(stdout, "\r\n{}", line);
} }
current_number = get_curr_history_number(); current_number = get_curr_history_number();
prompt = build_prompt(&current_number); (prompt, pr_len) = build_prompt(&current_number);
write!(stdout, "{}", prompt).unwrap(); write!(stdout, "{}", prompt).unwrap();
stdout.flush(); stdout.flush();
} else if mycommand == String::from("exit") { } else if mycommand == String::from("exit") {
@ -294,10 +305,9 @@ pub mod shell {
Key::Left => { Key::Left => {
//La taille du prompt est incorrecte à cause des couleurs ! //La taille du prompt est incorrecte à cause des couleurs !
let pr_length = prompt.chars().count();
let curs_pos = stdout.cursor_pos().unwrap().0; let curs_pos = stdout.cursor_pos().unwrap().0;
if usize::from(curs_pos) > pr_length { if usize::from(curs_pos) > pr_len {
write!(stdout, "1{:?}/2{}-{}", curs_pos, pr_length, cursor::Left(10)); write!(stdout, "{}", cursor::Left(1));
stdout.flush(); stdout.flush();
} }
}, },