From 3766ebbd70a1931aa471945b0e5b07ce07eeed95 Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Wed, 17 Apr 2024 10:17:52 +0200 Subject: [PATCH 1/2] fix(console): bug where backspace erases the prompt in dumb mode --- components/console/linenoise/linenoise.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 3a94e81b00..ed031fb401 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -1120,9 +1120,15 @@ static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) { } else if (c == BACKSPACE || c == 0x8) { if (count > 0) { buf[count - 1] = 0; - count --; + count--; + + /* Only erase symbol echoed from stdin. */ + fputs("\x08 ", stdout); /* Windows CMD: erase symbol under cursor */ + flushWrite(); + } else { + /* Consume backspace if the command line is empty to avoid erasing the prompt */ + continue; } - fputs("\x08 ", stdout); /* Windows CMD: erase symbol under cursor */ } else { buf[count] = c; ++count; From c2aad3d28a546122a994d25d24604610a97073dd Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Wed, 17 Apr 2024 12:50:02 +0200 Subject: [PATCH 2/2] fix(linenoise): Skip 0x00 to 0x1F character in dump mode Skipping through the non printable character assures that in dumb mode, any special keys will not lead to the cursor movement. --- components/console/linenoise/linenoise.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index ed031fb401..780d6dc7d6 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -171,6 +171,7 @@ enum KEY_ACTION{ CTRL_U = 21, /* Ctrl+u */ CTRL_W = 23, /* Ctrl+w */ ESC = 27, /* Escape */ + UNIT_SEP = 31, /* ctrl-_ */ BACKSPACE = 127 /* Backspace */ }; @@ -1110,14 +1111,13 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) { /* dumb terminal, fall back to fgets */ fputs(prompt, stdout); + flushWrite(); size_t count = 0; while (count < buflen) { int c = fgetc(stdin); if (c == '\n') { break; - } else if (c >= 0x1c && c <= 0x1f){ - continue; /* consume arrow keys */ - } else if (c == BACKSPACE || c == 0x8) { + } else if (c == BACKSPACE || c == CTRL_H) { if (count > 0) { buf[count - 1] = 0; count--; @@ -1129,11 +1129,17 @@ static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) { /* Consume backspace if the command line is empty to avoid erasing the prompt */ continue; } + + } else if (c <= UNIT_SEP) { + /* Consume all character that are non printable (the backspace + * case is handled above) */ + continue; } else { buf[count] = c; ++count; } fputc(c, stdout); /* echo */ + flushWrite(); } fputc('\n', stdout); flushWrite();