Blame SOURCES/0007-Fix-backspace-when-editing-a-multiline-cmdline.patch

03c2f3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
03c2f3
From: Robbie Harwood <rharwood@redhat.com>
03c2f3
Date: Thu, 21 Oct 2021 15:15:33 -0400
03c2f3
Subject: [PATCH] Fix backspace when editing a multiline cmdline
03c2f3
03c2f3
Once the cmdline had passed the width of the screen, adding additional
03c2f3
characters introduced a spurious newline, and another newline at the
03c2f3
width of input.  Furthermore, hitting backspace would not start
03c2f3
redrawing at the end of input, but rather at the beginning of the
03c2f3
current line - resulting in extra duplicate lines scrolling the console.
03c2f3
03c2f3
First, fix the assumption that the length of cmdline is the width - it
03c2f3
needs to include the length of the prompt (i.e., length of input and
03c2f3
space).
03c2f3
03c2f3
Second, fix the behavior of single-line redraw (i.e., redraw == 1) to
03c2f3
move the cursor to the row the line begins at.
03c2f3
03c2f3
Third, don't scroll the cursor down when a line wrap would occur - it's
03c2f3
not necessary since line wrap is enabled, and results in the extra blank
03c2f3
line.
03c2f3
03c2f3
Finally, comment all used escape sequences so that I don't need to look
03c2f3
them up again.
03c2f3
03c2f3
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
03c2f3
---
03c2f3
 com32/elflink/ldlinux/cli.c | 33 ++++++++++++++++++---------------
03c2f3
 1 file changed, 18 insertions(+), 15 deletions(-)
03c2f3
03c2f3
diff --git a/com32/elflink/ldlinux/cli.c b/com32/elflink/ldlinux/cli.c
03c2f3
index 3119b11f..4913f038 100644
03c2f3
--- a/com32/elflink/ldlinux/cli.c
03c2f3
+++ b/com32/elflink/ldlinux/cli.c
03c2f3
@@ -135,6 +135,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
     struct cli_command *comm_counter = NULL;
03c2f3
     clock_t kbd_to = kbdtimeout;
03c2f3
     clock_t tto = totaltimeout;
03c2f3
+    int prompt_len = 1 + strlen(input);
03c2f3
 
03c2f3
     if (!width) {
03c2f3
 	int height;
03c2f3
@@ -144,7 +145,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 
03c2f3
     len = cursor = 0;
03c2f3
     prev_len = 0;
03c2f3
-    x = y = 0;
03c2f3
+    y = 0;
03c2f3
 
03c2f3
     /*
03c2f3
      * Before we start messing with the x,y coordinates print 'input'
03c2f3
@@ -152,6 +153,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
      * previously.
03c2f3
      */
03c2f3
     printf("%s ", input);
03c2f3
+    x = prompt_len;
03c2f3
 
03c2f3
     while (!done) {
03c2f3
 	if (redraw > 1) {
03c2f3
@@ -162,8 +164,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 	    if (pDraw_Menu)
03c2f3
 		    (*pDraw_Menu) (-1, top, 1);
03c2f3
 	    prev_len = 0;
03c2f3
-	    printf("\033[2J\033[H");
03c2f3
-	    // printf("\033[0m\033[2J\033[H");
03c2f3
+	    printf("\033[2J\033[H"); /* Clear entire screen; move to 0, 0. */
03c2f3
 	}
03c2f3
 
03c2f3
 	if (redraw > 0) {
03c2f3
@@ -172,10 +173,14 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 	    prev_len = max(len, prev_len);
03c2f3
 
03c2f3
 	    /* Redraw the command line */
03c2f3
-	    printf("\033[?25l");
03c2f3
-	    printf("\033[1G%s ", input);
03c2f3
+	    printf("\033[?25l"); /* Hide cursor. */
03c2f3
+	    printf("\033[1G"); /* Column 1. */
03c2f3
+	    if (y > 0)
03c2f3
+		printf("\033[%dA", y); /* Directly up. */
03c2f3
 
03c2f3
-	    x = strlen(input);
03c2f3
+	    printf("%s ", input);
03c2f3
+
03c2f3
+	    x = prompt_len;
03c2f3
 	    y = 0;
03c2f3
 	    at = 0;
03c2f3
 	    while (at < prev_len) {
03c2f3
@@ -183,23 +188,22 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 		at++;
03c2f3
 		x++;
03c2f3
 		if (x >= width) {
03c2f3
-		    printf("\r\n");
03c2f3
 		    x = 0;
03c2f3
 		    y++;
03c2f3
 		}
03c2f3
 	    }
03c2f3
-	    printf("\033[K\r");
03c2f3
+	    printf("\033[K\r"); /* Clear to end of line; go to beginning. */
03c2f3
 
03c2f3
-	    dy = y - (cursor + strlen(input) + 1) / width;
03c2f3
-	    x = (cursor + strlen(input) + 1) % width;
03c2f3
+	    dy = y - (cursor + prompt_len) / width;
03c2f3
+	    x = (cursor + prompt_len) % width;
03c2f3
 
03c2f3
 	    if (dy) {
03c2f3
-		printf("\033[%dA", dy);
03c2f3
+		printf("\033[%dA", dy); /* Cursor directly up. */
03c2f3
 		y -= dy;
03c2f3
 	    }
03c2f3
 	    if (x)
03c2f3
-		printf("\033[%dC", x);
03c2f3
-	    printf("\033[?25h");
03c2f3
+		printf("\033[%dC", x); /* Cursor forward. */
03c2f3
+	    printf("\033[?25h"); /* Show cursor. */
03c2f3
 	    prev_len = len;
03c2f3
 	    redraw = 0;
03c2f3
 	}
03c2f3
@@ -439,7 +443,6 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 		    cursor++;
03c2f3
 		    x++;
03c2f3
 		    if (x >= width) {
03c2f3
-			printf("\r\n\033[K");
03c2f3
 			y++;
03c2f3
 			x = 0;
03c2f3
 		    }
03c2f3
@@ -459,7 +462,7 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
03c2f3
 	}
03c2f3
     }
03c2f3
 
03c2f3
-    printf("\033[?7h");
03c2f3
+    printf("\033[?7h"); /* Enable line wrap. */
03c2f3
 
03c2f3
     /* Add the command to the history if its length is larger than 0 */
03c2f3
     len = strlen(ret);