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

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