From 8c92b82656cdb24200268dd38990b080452187e8 Mon Sep 17 00:00:00 2001 From: Justine Date: Fri, 23 Dec 2022 15:00:54 +0100 Subject: [PATCH] =?UTF-8?q?Fleche=20haut=20marche=20un=20peu,=20=C3=A0=20f?= =?UTF-8?q?inir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 1 + src/lib.rs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) 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; },