From 07d8dd2c68aea2bd4a03e8b69a521c67abc1d618 Mon Sep 17 00:00:00 2001 From: Ryan Cohen Date: Sun, 23 Oct 2022 15:56:23 -0400 Subject: [PATCH] readline: Fix command line scrolling and cursor wrap-around Fixes `cursor_fwd` to wrap the cursor to the first column when it passes the bottom right corner of the screen. Fixes `readline` to update the command line's row position when the cursor wrap-around causes the screen to scroll up. --- common/lib/readline.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/common/lib/readline.c b/common/lib/readline.c index 3021511c..5d36a192 100644 --- a/common/lib/readline.c +++ b/common/lib/readline.c @@ -377,9 +377,11 @@ static void cursor_fwd(void) { term->get_cursor_pos(term, &x, &y); if (x < term->cols - 1) { x++; - } else if (y < term->rows - 1) { - y++; + } else { x = 0; + if (y < term->rows - 1) { + y++; + } } set_cursor_pos_helper(x, y); } @@ -457,8 +459,15 @@ void readline(const char *orig_str, char *buf, size_t limit) { } buf[i] = c; i++; + size_t prev_x, prev_y; + term->get_cursor_pos(term, &prev_x, &prev_y); cursor_fwd(); reprint_string(orig_x, orig_y, buf); + // If cursor has wrapped around, move the line start position up one row + if (prev_x == term->cols - 1 && prev_y == term->rows - 1) { + orig_y--; + print("\e[J"); // Clear the bottom line + } } } }