diff --git a/Cargo.toml b/Cargo.toml index 08b4e6d..00eb645 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ dirs = "4.0.0" users = "0.11.0" gethostname = "0.4.1" termion = "2.0.1" +unicode-segmentation = "1.6.0" diff --git a/src/lib.rs b/src/lib.rs index 9e8bf20..f4228d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ pub mod shell { use termion::input::TermRead; use termion::raw::IntoRawMode; use termion::color; + use unicode_segmentation::UnicodeSegmentation; mod history; use crate::shell::history::*; @@ -178,6 +179,20 @@ pub mod shell { } } + fn rewrite(current_cmd: String) -> String { + //Deletes currently written command + + let mut stdout = stdout().into_raw_mode().unwrap(); + let nbr_of_chars = current_cmd.graphemes(true).count(); + while nbr_of_chars > 0 { + write!(stdout, "\x1b[D").unwrap(); + write!(stdout, "\x1b[K").unwrap(); + } + return String::new(); + } + + + pub fn run_raw() { let stdin = stdin(); let mut stdout = stdout().into_raw_mode().unwrap(); @@ -243,6 +258,7 @@ pub mod shell { }, Key::Up => { + mycommand = rewrite(mycommand); let lastnumber = match get_history_number() { Ok(e) => e, Err(_) => continue, @@ -251,8 +267,9 @@ pub mod shell { Some(c) => c, None => continue }; - let prompt = build_prompt(); - write!(stdout, "\r\n{}{}", prompt, lastcmd.trim()); + mycommand = lastcmd.trim().to_string(); + //let prompt = build_prompt(); + write!(stdout, "{}", mycommand); mycommand = lastcmd; },