Merge branch 'fix/prompt-erased-by-backspace-in-dumbmode_v5.1' into 'release/v5.1'

fix(console): bug where backspace erases the prompt in dumb mode (backport v5.1)

See merge request espressif/esp-idf!30349
This commit is contained in:
Marius Vikhammer
2024-05-20 15:10:51 +08:00

View File

@@ -171,6 +171,7 @@ enum KEY_ACTION{
CTRL_U = 21, /* Ctrl+u */ CTRL_U = 21, /* Ctrl+u */
CTRL_W = 23, /* Ctrl+w */ CTRL_W = 23, /* Ctrl+w */
ESC = 27, /* Escape */ ESC = 27, /* Escape */
UNIT_SEP = 31, /* ctrl-_ */
BACKSPACE = 127 /* Backspace */ BACKSPACE = 127 /* Backspace */
}; };
@@ -1110,24 +1111,35 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
static int linenoiseDumb(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 */ /* dumb terminal, fall back to fgets */
fputs(prompt, stdout); fputs(prompt, stdout);
flushWrite();
size_t count = 0; size_t count = 0;
while (count < buflen) { while (count < buflen) {
int c = fgetc(stdin); int c = fgetc(stdin);
if (c == '\n') { if (c == '\n') {
break; break;
} else if (c >= 0x1c && c <= 0x1f){ } else if (c == BACKSPACE || c == CTRL_H) {
continue; /* consume arrow keys */
} else if (c == BACKSPACE || c == 0x8) {
if (count > 0) { if (count > 0) {
buf[count - 1] = 0; buf[count - 1] = 0;
count--; count--;
}
/* Only erase symbol echoed from stdin. */
fputs("\x08 ", stdout); /* Windows CMD: erase symbol under cursor */ 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;
}
} else if (c <= UNIT_SEP) {
/* Consume all character that are non printable (the backspace
* case is handled above) */
continue;
} else { } else {
buf[count] = c; buf[count] = c;
++count; ++count;
} }
fputc(c, stdout); /* echo */ fputc(c, stdout); /* echo */
flushWrite();
} }
fputc('\n', stdout); fputc('\n', stdout);
flushWrite(); flushWrite();