:: commit 5c903dc5bfca1bc127362b7c1951c7ca48e069f3

Ryan Cohen <rcohenprogramming@gmail.com> — 2022-10-23 19:56

parents: 18100db81a

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.
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
+                    }
                 }
             }
         }
tab: 248 wrap: offon