summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRory Dudley2024-08-28 17:08:30 -0600
committerRory Dudley2024-08-28 17:08:30 -0600
commit398631015cac0174aeec5fd351fcfcaf51eb6ef0 (patch)
tree3d46cada885e06d10cc4915dbb783e8fe5ca26de
parent3c3cea0c7c494c998f05f21317a7c1bfa078a80e (diff)
downloaddwarvish-398631015cac0174aeec5fd351fcfcaf51eb6ef0.tar.gz
Keep track of position in getline()
Keep track of the cursor position in getline(), this way it is not possible to backspace the prompt.
-rw-r--r--src/buffer.rs10
1 files changed, 10 insertions, 0 deletions
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<Mutex<Vec<u8>>>) -> 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<Mutex<Vec<u8>>>) -> usize {
// tab
b'\t' => {
+ pos += 1;
print!(" ");
buffer.lock().unwrap().push(b' ');
}
@@ -41,12 +46,17 @@ pub fn getline(buffer: &mut Arc<Mutex<Vec<u8>>>) -> 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);
}