From e9d9db1f083fe4a47262471da3ca55c751176237 Mon Sep 17 00:00:00 2001 From: Justine Date: Sun, 25 Dec 2022 17:45:46 +0100 Subject: [PATCH] =?UTF-8?q?Fl=C3=A8che=20gauche=20fonctionne,=20en=20mode?= =?UTF-8?q?=20remplacement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 34e59d1..13238cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub mod shell { use std::env; use std::str; use std::process::Stdio; + use std::num::Wrapping; use users::{get_user_by_uid, get_current_uid}; use termion::event::Key; use termion::input::TermRead; @@ -104,7 +105,11 @@ pub mod shell { 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( get_user_by_uid(get_current_uid()) .unwrap() @@ -113,12 +118,16 @@ pub mod shell { .unwrap() ); + length += user.chars().count(); + let host = String::from( gethostname::gethostname() .to_str() .unwrap() ); + length += host.chars().count(); + let current_dir = String::from( env::current_dir() .unwrap() @@ -132,13 +141,15 @@ pub mod shell { .unwrap() ); + length += dir_last.chars().count(); + 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; + return (prompt, length); } fn replace_signs(line: &String) -> String { @@ -205,7 +216,7 @@ pub mod shell { //Used in conjunction with Up arrow to go back in history //Resetted by pressing Enter let mut current_number = get_curr_history_number(); - let mut prompt = build_prompt(¤t_number); + let (mut prompt, mut pr_len) = build_prompt(¤t_number); //Initialize write!(stdout, "\r\n SquiShell (sqish)--- \r\n{}", prompt); @@ -224,7 +235,7 @@ pub mod shell { Key::Char('\n') => { current_number = get_curr_history_number(); - prompt = build_prompt(¤t_number); + (prompt, pr_len) = build_prompt(¤t_number); if (mycommand != String::from("\n")) && (mycommand != String::from("exit")) { let comm = replace_signs(&mycommand); let res = handle_input(&comm); @@ -234,7 +245,7 @@ pub mod shell { write!(stdout, "\r\n{}", line); } current_number = get_curr_history_number(); - prompt = build_prompt(¤t_number); + (prompt, pr_len) = build_prompt(¤t_number); write!(stdout, "{}", prompt).unwrap(); stdout.flush(); } else if mycommand == String::from("exit") { @@ -294,10 +305,9 @@ pub mod shell { Key::Left => { //La taille du prompt est incorrecte à cause des couleurs ! - let pr_length = prompt.chars().count(); let curs_pos = stdout.cursor_pos().unwrap().0; - if usize::from(curs_pos) > pr_length { - write!(stdout, "1{:?}/2{}-{}", curs_pos, pr_length, cursor::Left(10)); + if usize::from(curs_pos) > pr_len { + write!(stdout, "{}", cursor::Left(1)); stdout.flush(); } },