Replace multiple if with switch

This commit is contained in:
Arun Prakash Jana 2018-07-04 19:02:47 +05:30
parent cd318db1b2
commit 74a0dc0a97
No known key found for this signature in database
GPG key ID: A75979F35C080412

34
nnn.c
View file

@ -1213,35 +1213,30 @@ xreadline(char *fname, char *prompt)
if ((r = get_wch(ch)) != ERR) { if ((r = get_wch(ch)) != ERR) {
if (r == OK) { if (r == OK) {
if (*ch == KEY_ENTER || *ch == '\n' || *ch == '\r') switch (*ch) {
break; case KEY_ENTER: //fallthrough
case '\n': //fallthrough
if (*ch == '\b') { case '\r':
goto END;
case '\b': /* some old curses (e.g. rhel25) still send '\b' for backspace */
if (pos > 0) { if (pos > 0) {
memmove(buf + pos - 1, buf + pos, (len - pos) << 2); memmove(buf + pos - 1, buf + pos, (len - pos) << 2);
--len, --pos; --len, --pos;
} } //fallthrough
case '\t': /* TAB breaks cursor position, ignore it */
continue; continue;
} case CONTROL('L'):
if (*ch == CONTROL('L')) {
clearprompt(); clearprompt();
printprompt(prompt); printprompt(prompt);
len = pos = 0; len = pos = 0;
continue; continue;
} case CONTROL('A'):
if (*ch == CONTROL('A')) {
pos = 0; pos = 0;
continue; continue;
} case CONTROL('E'):
if (*ch == CONTROL('E')) {
pos = len; pos = len;
continue; continue;
} case CONTROL('U'):
if (*ch == CONTROL('U')) {
clearprompt(); clearprompt();
printprompt(prompt); printprompt(prompt);
memmove(buf, buf + pos, (len - pos) << 2); memmove(buf, buf + pos, (len - pos) << 2);
@ -1254,10 +1249,6 @@ xreadline(char *fname, char *prompt)
if (keyname(*ch)[0] == '^') if (keyname(*ch)[0] == '^')
continue; continue;
/* TAB breaks cursor position, ignore it */
if (*ch == '\t')
continue;
if (pos < NAME_MAX - 1) { if (pos < NAME_MAX - 1) {
memmove(buf + pos + 1, buf + pos, (len - pos) << 2); memmove(buf + pos + 1, buf + pos, (len - pos) << 2);
buf[pos] = *ch; buf[pos] = *ch;
@ -1293,6 +1284,7 @@ xreadline(char *fname, char *prompt)
} }
} }
END:
buf[len] = '\0'; buf[len] = '\0';
if (old_curs != ERR) if (old_curs != ERR)
curs_set(old_curs); curs_set(old_curs);