From 49fe61dcf2cda9450387ab060bdf030722d2b727 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Mon, 9 Sep 2024 13:14:52 -0400 Subject: bugfix to inline backspace and delete on multiline buffer --- src/lush.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index c117fc2..83b5f08 100644 --- a/src/lush.c +++ b/src/lush.c @@ -284,6 +284,7 @@ size_t get_stripped_length(const char *str) { } static void reprint_buffer(char *buffer, int *last_lines, int *pos, int history_pos) { + static size_t old_buffer_len = 0; char *prompt = get_prompt(); int width = get_terminal_width(); size_t prompt_length = get_stripped_length(prompt); @@ -305,15 +306,29 @@ 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) + prompt_length + 1) % width == 0) { + // compensate for if we have just filled a line + if ((strlen(buffer) + prompt_length + 1) % width == 0 && + old_buffer_len < strlen(buffer)) { printf("\033[A"); } + // compense for if we have just emptied a line + if ((strlen(buffer) + prompt_length + 1) % width == width - 1 && + old_buffer_len > strlen(buffer)) { + printf("\033[B"); + } } - for (int i = 0; i < *last_lines; i++) { - printf("\r\033[K"); - if (i > 0) - printf("\033[A"); + if (old_buffer_len < strlen(buffer) || history_pos >= 0) { + for (int i = 0; i < *last_lines; i++) { + printf("\r\033[K"); + if (i > 0) + printf("\033[A"); + } + } else { + for (int i = 0; i < num_lines; i++) { + printf("\r\033[K"); + if (i > 0) + printf("\033[A"); + } } *last_lines = num_lines; @@ -331,6 +346,7 @@ static void reprint_buffer(char *buffer, int *last_lines, int *pos, printf("\033[%dA", num_lines - cursor_line); free(prompt); + old_buffer_len = strlen(buffer); } char *lush_read_line() { @@ -415,7 +431,7 @@ char *lush_read_line() { int width = get_terminal_width(); char *prompt = get_prompt(); size_t prompt_length = get_stripped_length(prompt); - if ((prompt_length + pos) % width == width - 2 && + if ((prompt_length + pos + 1) % width == width - 1 && pos < strlen(buffer)) { printf("\033[A"); } -- cgit v1.2.3-59-g8ed1b