From 604b148d94b2b48cf2ad1608b5a2d322e341de3c Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Thu, 12 Dec 2024 22:45:53 -0700 Subject: More buffer bug fixes Always append a newline to the end of the buffer. This ensures that the shell will not quit if the user simply presses the return key without any other input. Fix a disrepency between the buffer length, and text width in the comp function. The length of the buffer is the number of indices, whereas the width of the buffer is the summed width of each character within the buffer. We need the length when operating on the buffer's indicies, but the width when dealing with what actually gets outputted to the shell. In this case, the the width was being used where the length (bpos) should've been, and was causing an index out of bounds error for any strings that contained a character wider than 1. This rectifies the issue, as well as refactors some code that was (appropriately) using the width. Signed-off-by: Rory Dudley --- src/buffer.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index bd5ab60..08c8653 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -165,13 +165,21 @@ fn comp( *bpos -= *len; } + let ori_path: String = buffer[*bpos..].into_iter().collect::(); + let mut width = UnicodeWidthStr::width(ori_path.as_str()); + // Remove the last autocomplete value from the buffer while *len > 0 { buffer.pop(); - print!("\u{8} \u{8}"); *len -= 1; } + // Remove the last autocomplete value from the shell + while width > 0 { + print!("\u{8} \u{8}"); + width -= 1; + } + // Reverse the buffer, which will make our while loop further down much easier let mut rev = buffer.iter().rev(); @@ -285,7 +293,7 @@ fn comp( // Get the path (or only part of the path if we matched with word) let path = paths[*pos].path(); - let mut path = if paths[*pos].path().is_dir() { + let path = if paths[*pos].path().is_dir() { (path.file_name().unwrap().to_string_lossy()[word.len()..].to_string() + "/").to_string() } else { path.file_name().unwrap().to_string_lossy()[word.len()..].to_string() @@ -324,7 +332,7 @@ fn comp( } // Update the length of the last comp - *len = UnicodeWidthStr::width(path.as_str()); + *len += path.chars().collect::>().len(); // Update the buffer position *bpos += *len; @@ -523,6 +531,7 @@ pub fn getline( let trunc_width = UnicodeWidthStr::width(trunc.as_str()); buffer.insert(*pos.lock().unwrap(), c); *pos.lock().unwrap() += 1; + // *pos.lock().unwrap() += UnicodeWidthChar::width(c).unwrap_or(1); if *pos.lock().unwrap() == buffer.len() { print!("{}", c); } else { @@ -586,6 +595,7 @@ pub fn getline( } println!(); + buffer.lock().unwrap().push('\n'); let mut bytes = 0; buffer -- cgit v1.2.3