Define return codes for nextsel() and use a switch

This commit is contained in:
lostd 2014-10-10 10:06:31 +03:00
parent a75021c2d2
commit 1cdfa84f69

54
noice.c
View file

@ -185,11 +185,12 @@ printerr(int ret, char *prefix)
/* /*
* Returns 0 normally * Returns 0 normally
* On movement it updates *cur * On movement it updates *cur
* Returns 1 on quit * Returns SEL_{QUIT,BACK,GOIN,FLTR} otherwise
* Returns 2 on go in
* Returns 3 on go up
* Returns 4 on search
*/ */
#define SEL_QUIT 1
#define SEL_BACK 2
#define SEL_GOIN 3
#define SEL_FLTR 4
int int
nextsel(int *cur, int max) nextsel(int *cur, int max)
{ {
@ -198,22 +199,22 @@ nextsel(int *cur, int max)
c = getch(); c = getch();
switch (c) { switch (c) {
case 'q': case 'q':
return 1; return SEL_QUIT;
/* go up */ /* back */
case KEY_BACKSPACE: case KEY_BACKSPACE:
case KEY_LEFT: case KEY_LEFT:
case 'h': case 'h':
return 2; return SEL_BACK;
/* go in */ /* inside */
case KEY_ENTER: case KEY_ENTER:
case '\r': case '\r':
case KEY_RIGHT: case KEY_RIGHT:
case 'l': case 'l':
return 3; return SEL_GOIN;
/* search */ /* filter */
case '/': case '/':
case '&': case '&':
return 4; return SEL_FLTR;
/* next */ /* next */
case 'j': case 'j':
case KEY_DOWN: case KEY_DOWN:
@ -389,6 +390,14 @@ begin:
int nlines; int nlines;
int maxlen; int maxlen;
int odd; int odd;
char *pathnew;
char *name;
char *bin;
pid_t pid;
int fd;
char *dir;
char *tmp;
regex_t re;
redraw: redraw:
nlines = MIN(LINES - 4, n); nlines = MIN(LINES - 4, n);
@ -434,17 +443,15 @@ redraw:
nochange: nochange:
ret = nextsel(&cur, n); ret = nextsel(&cur, n);
if (ret == 1) { switch (ret) {
case SEL_QUIT:
free(path); free(path);
return; return;
} case SEL_BACK:
if (ret == 2) {
/* Handle root case */ /* Handle root case */
if (strcmp(path, "") == 0) { if (strcmp(path, "") == 0) {
goto nochange; goto nochange;
} else { } else {
char *dir, *tmp;
dir = dirname(path); dir = dirname(path);
tmp = malloc(strlen(dir) + 1); tmp = malloc(strlen(dir) + 1);
strlcpy(tmp, dir, strlen(dir) + 1); strlcpy(tmp, dir, strlen(dir) + 1);
@ -454,14 +461,7 @@ nochange:
filter = strdup(ifilter); /* Reset filter */ filter = strdup(ifilter); /* Reset filter */
goto out; goto out;
} }
} case SEL_GOIN:
if (ret == 3) {
char *pathnew;
char *name;
char *bin;
pid_t pid;
int fd;
/* Cannot descend in empty directories */ /* Cannot descend in empty directories */
if (n == 0) if (n == 0)
goto nochange; goto nochange;
@ -524,11 +524,7 @@ nochange:
printmsg("Unsupported file"); printmsg("Unsupported file");
free(pathnew); free(pathnew);
goto nochange; goto nochange;
} case SEL_FLTR:
if (ret == 4) {
char *tmp;
regex_t re;
/* Read filter */ /* Read filter */
move(LINES - 1, 0); move(LINES - 1, 0);
printw("filter: "); printw("filter: ");