From c51ad81d3cf5998e9eacf655a704ff21d154ae0a Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Mon, 30 Sep 2024 21:55:33 -0600 Subject: Keep track of position in getline() Keep track of the cursor position in getline(), this way it is not possible to backspace the prompt. Signed-off-by: Rory Dudley --- src/buffer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/buffer.rs b/src/buffer.rs index 48e2d85..eedb694 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -24,6 +24,10 @@ fn getchar() -> u8 { /// that (ICANON and ECHO) are off. See the beginning of [crate::repl] /// for more details. pub fn getline(buffer: &mut Arc>>) -> usize { + // Keep track of position for backspaces + let mut pos: usize = 0; + + // Loop over characters until there is a newline loop { let c = getchar(); match c { @@ -32,6 +36,7 @@ pub fn getline(buffer: &mut Arc>>) -> usize { // tab b'\t' => { + pos += 1; print!(" "); buffer.lock().unwrap().push(b' '); } @@ -41,12 +46,17 @@ pub fn getline(buffer: &mut Arc>>) -> usize { // backspace 127 => { + if pos == 0 { + continue; + } + pos -= 1; buffer.lock().unwrap().pop(); print!("\u{8} \u{8}"); } // everything else _ => { + pos += 1; print!("{}", c as char); buffer.lock().unwrap().push(c); } -- cgit v1.2.3