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

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