Commit 8087db33d870e64a3413dda04d7c742c924bd831

antirez 2023-03-27T10:07:44

Multiline: just remember last num of rows, not max. For some reason, the old code was always cleaning the maximum number of rows used so far while editing in multi line mode. Actually we need to clean just the number of rows used by the last line. The old behavior created problems in multiplexing mode, where the line is refreshed at a different row, if the user used linenoiseHide() / show() in order to print something. With the new behavior, all looks fine, so far.

diff --git a/linenoise.c b/linenoise.c
index 2039da7..5e8aee5 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -175,7 +175,7 @@ FILE *lndebug_fp = NULL;
             fprintf(lndebug_fp, \
             "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
             (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
-            (int)l->maxrows,old_rows); \
+            (int)l->oldrows,old_rows); \
         } \
         fprintf(lndebug_fp, ", " __VA_ARGS__); \
         fflush(lndebug_fp); \
@@ -600,12 +600,11 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
     int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */
     int rpos2; /* rpos after refresh. */
     int col; /* colum position, zero-based. */
-    int old_rows = l->maxrows;
+    int old_rows = l->oldrows;
     int fd = l->ofd, j;
     struct abuf ab;
 
-    /* Update maxrows if needed. */
-    if (rows > (int)l->maxrows) l->maxrows = rows;
+    l->oldrows = rows;
 
     /* First step: clear all the lines used before. To do so start by
      * going to the last row. */
@@ -657,7 +656,7 @@ static void refreshMultiLine(struct linenoiseState *l, int flags) {
             snprintf(seq,64,"\r");
             abAppend(&ab,seq,strlen(seq));
             rows++;
-            if (rows > (int)l->maxrows) l->maxrows = rows;
+            if (rows > (int)l->oldrows) l->oldrows = rows;
         }
 
         /* Move cursor to right position. */
@@ -882,7 +881,7 @@ int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, ch
     l->oldpos = l->pos = 0;
     l->len = 0;
     l->cols = getColumns(stdin_fd, stdout_fd);
-    l->maxrows = 0;
+    l->oldrows = 0;
     l->history_index = 0;
 
     /* Buffer starts empty. */
diff --git a/linenoise.h b/linenoise.h
index fdb4319..3f0270e 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -64,7 +64,7 @@ struct linenoiseState {
     size_t oldpos;      /* Previous refresh cursor position. */
     size_t len;         /* Current edited line length. */
     size_t cols;        /* Number of columns in terminal. */
-    size_t maxrows;     /* Maximum num of rows used so far (multiline mode) */
+    size_t oldrows;     /* Rows used by last refrehsed line (multiline mode) */
     int history_index;  /* The history index we are currently editing. */
 };