From 29c052c9470cfde967c5a3965cd079c5ee8dbb66 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Sat, 7 Sep 2024 12:35:15 -0400 Subject: fixed bug with cursor alignment when adding text within the buffer --- src/lush.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lush.c b/src/lush.c index 33f7beb..1f11482 100644 --- a/src/lush.c +++ b/src/lush.c @@ -210,6 +210,10 @@ static void reprint_buffer(char *buffer, int *last_lines, int *pos, // move cursor down if it is up a number of lines first if (num_lines - cursor_line > 0) { printf("\033[%dB", num_lines - cursor_line); + // compensate for if the we have just filled a line + if ((strlen(buffer) + strlen(prompt) + 1) % width == 0) { + printf("\033[A"); + } } for (int i = 0; i < *last_lines; i++) { printf("\r\033[K"); @@ -327,7 +331,13 @@ char *lush_read_line() { strlen(&buffer[pos]) + 1); buffer[pos] = c; pos++; - + // handle edge case where cursor should be moved down + int width = get_terminal_width(); + char *prompt = get_prompt(); + if ((strlen(prompt) + pos) % width == width - 1 && + pos < strlen(buffer)) { + printf("\033[B"); + } // if modifying text reset history history_pos = -1; reprint_buffer(buffer, &last_lines, &pos, history_pos); -- cgit v1.2.3-59-g8ed1b