From 88a35d692c7e79bf8c32b13a2abb43c5bc6d547a Mon Sep 17 00:00:00 2001 From: Justine Date: Thu, 2 Feb 2023 11:32:01 +0100 Subject: [PATCH] Fixed character insertion --- src/lib.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5a79d24..3ebeb2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ pub mod shell { use std::process::Command; use std::io::stdin; use std::io::stdout; - use std::io::Stdout; use std::io::Write; use std::path::Path; use std::env; @@ -98,7 +97,7 @@ pub mod shell { .stdin(stdin) .spawn() { Ok(h) => h, - Err(e) => { + Err(_) => { let err_string = format!("Command not found : {}\r\n", command); return String::from(err_string); }, @@ -205,14 +204,19 @@ pub mod shell { return (max_pos, current_pos); } - fn clear_line(max_pos: &usize) { + fn clear_line(max_pos: &usize, current_pos: &usize) { //clears currently written command from the screen //...NOT from the variable ! let mut stdout = stdout().into_raw_mode().unwrap(); + let right_spaces: u16 = (*max_pos - *current_pos).try_into().unwrap(); + write!(stdout, "{}", cursor::Right(right_spaces)); for _i in 0..*max_pos { write!(stdout, "{}", cursor::Left(1)); write!(stdout, "\x1b[K").unwrap(); } + write!(stdout, "{}", cursor::Left(1)); + write!(stdout, "\x1b[K").unwrap(); + stdout.flush(); } @@ -296,7 +300,7 @@ pub mod shell { }, Key::Ctrl('t') => { - clear_line(&max_pos); + clear_line(&max_pos, ¤t_pos); max_pos = 0; current_pos = 0; mycommand.clear(); @@ -334,20 +338,19 @@ pub mod shell { write!(stdout, "{}", cursor::Restore); write!(stdout, "{}", cursor::Right(1)); max_pos += 1; + current_pos += 1; } }, Key::Backspace => { if current_pos > 0 { - max_pos -= 1; current_pos -= 1; + max_pos -= 1; mycommand.remove(current_pos); write!(stdout, "{}", cursor::Left(1)); - //Delete the end - write!(stdout, "\x1b[K").unwrap(); write!(stdout, "{}", cursor::Save); - clear_line(&max_pos); + clear_line(&max_pos, ¤t_pos); write!(stdout, "{}", mycommand); write!(stdout, "{}", cursor::Restore); stdout.flush(); @@ -380,11 +383,21 @@ pub mod shell { write!(stdout, "{}", mycommand); }, + Key::Ctrl('p') => { + print!("{}", current_pos); + } + + Key::Ctrl('x') => { + clear_line(&max_pos, ¤t_pos); + mycommand.clear(); + current_pos = 0; + max_pos = 0; + } + Key::Left => { if current_pos > 0 { current_pos -= 1; write!(stdout, "{}", cursor::Left(1)); - stdout.flush(); } },