Skip to content

Commit 1b45248

Browse files
committed
Merge branch 'fix/prompt-erased-by-backspace-in-dumbmode' into 'master'
fix(console): bug where backspace erases the prompt in dumb mode Closes IDFGH-12508 See merge request espressif/esp-idf!30313
2 parents 0c64e48 + ada38e8 commit 1b45248

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

components/console/linenoise/linenoise.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ enum KEY_ACTION{
178178
CTRL_U = 21, /* Ctrl+u */
179179
CTRL_W = 23, /* Ctrl+w */
180180
ESC = 27, /* Escape */
181+
UNIT_SEP = 31, /* ctrl-_ */
181182
BACKSPACE = 127 /* Backspace */
182183
};
183184

@@ -1131,15 +1132,23 @@ static int linenoiseDumb(char* buf, size_t buflen, const char* prompt) {
11311132
int c = fgetc(stdin);
11321133
if (c == '\n') {
11331134
break;
1134-
} else if (c >= 0x1c && c <= 0x1f){
1135-
continue; /* consume arrow keys */
1136-
} else if (c == BACKSPACE || c == 0x8) {
1135+
} else if (c == BACKSPACE || c == CTRL_H) {
11371136
if (count > 0) {
11381137
buf[count - 1] = 0;
1139-
count --;
1138+
count--;
1139+
1140+
/* Only erase symbol echoed from stdin. */
1141+
fputs("\x08 ", stdout); /* Windows CMD: erase symbol under cursor */
1142+
flushWrite();
1143+
} else {
1144+
/* Consume backspace if the command line is empty to avoid erasing the prompt */
1145+
continue;
11401146
}
1141-
fputs("\x08 ", stdout); /* Windows CMD: erase symbol under cursor */
1142-
flushWrite();
1147+
1148+
} else if (c <= UNIT_SEP) {
1149+
/* Consume all character that are non printable (the backspace
1150+
* case is handled above) */
1151+
continue;
11431152
} else {
11441153
buf[count] = c;
11451154
++count;

0 commit comments

Comments
 (0)