From ac2491acc56f8eac1438d0963a09cc92d34f2d6f 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 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 221c86c20ed0..46ae946c44df 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -1121,10 +1121,16 @@ 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 */ - flushWrite(); + } else { buf[count] = c; ++count; From 302881e6b72c9df990fcbedddd3f17f08480e44d 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 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 46ae946c44df..780d6dc7d6e8 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 */ }; @@ -1116,9 +1117,7 @@ static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) { 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--; @@ -1131,6 +1130,10 @@ static int linenoiseDumb(char* buf, size_t buflen, const char* 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;