commit 6b7c2506db2844d718be714e88cb631b0c721ad2
parent ef3bfef45449a2d660a0ee8880a8888a5aef63fc
Author: lostd <lostd@2f30.org>
Date: Thu, 6 Nov 2014 13:46:37 +0200
Expose key bindings in the configuration header
Diffstat:
M | config.def.h | | | 35 | +++++++++++++++++++++++++++++++++++ |
M | noice.c | | | 78 | ++++++++++++++++++++++++------------------------------------------------------ |
2 files changed, 59 insertions(+), 54 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -10,3 +10,38 @@ struct assoc assocs[] = {
{ "\\.sh$", "sh" },
{ ".*", "less" },
};
+
+struct key bindings[] = {
+ /* Quit */
+ { 'q', SEL_QUIT },
+ /* Back */
+ { KEY_BACKSPACE, SEL_BACK },
+ { KEY_LEFT, SEL_BACK },
+ { 'h', SEL_BACK },
+ /* Inside */
+ { KEY_ENTER, SEL_GOIN },
+ { '\r', SEL_GOIN },
+ { KEY_RIGHT, SEL_GOIN },
+ { 'l', SEL_GOIN },
+ /* Filter */
+ { '/', SEL_FLTR },
+ { '&', SEL_FLTR },
+ /* Next */
+ { 'j', SEL_NEXT },
+ { KEY_DOWN, SEL_NEXT },
+ { CONTROL('N'), SEL_NEXT },
+ /* Previous */
+ { 'k', SEL_PREV },
+ { KEY_UP, SEL_PREV },
+ { CONTROL('P'), SEL_PREV },
+ /* Page down */
+ { KEY_NPAGE, SEL_PGDN },
+ { CONTROL('D'), SEL_PGDN },
+ /* Page up */
+ { KEY_PPAGE, SEL_PGUP },
+ { CONTROL('U'), SEL_PGUP },
+ /* Shell */
+ { '!', SEL_SH },
+ /* Change dir */
+ { 'c', SEL_CD },
+};
diff --git a/noice.c b/noice.c
@@ -42,6 +42,25 @@ struct assoc {
char *bin; /* Program */
};
+/* Supported actions */
+enum action {
+ SEL_QUIT = 1,
+ SEL_BACK,
+ SEL_GOIN,
+ SEL_FLTR,
+ SEL_NEXT,
+ SEL_PREV,
+ SEL_PGDN,
+ SEL_PGUP,
+ SEL_SH,
+ SEL_CD,
+};
+
+struct key {
+ int sym; /* Key pressed */
+ enum action act; /* Action */
+};
+
#include "config.h"
struct entry {
@@ -259,65 +278,16 @@ printprompt(char *str)
* Returns SEL_{QUIT,BACK,GOIN,FLTR,NEXT,PREV,PGDN,PGUP,SH,CD}
* Returns 0 otherwise
*/
-enum {
- SEL_QUIT = 1,
- SEL_BACK,
- SEL_GOIN,
- SEL_FLTR,
- SEL_NEXT,
- SEL_PREV,
- SEL_PGDN,
- SEL_PGUP,
- SEL_SH,
- SEL_CD,
-};
-
int
nextsel(void)
{
- int c;
+ int c, i;
c = getch();
- switch (c) {
- case 'q':
- return SEL_QUIT;
- /* Back */
- case KEY_BACKSPACE:
- case KEY_LEFT:
- case 'h':
- return SEL_BACK;
- /* Inside */
- case KEY_ENTER:
- case '\r':
- case KEY_RIGHT:
- case 'l':
- return SEL_GOIN;
- /* Filter */
- case '/':
- case '&':
- return SEL_FLTR;
- /* Next */
- case 'j':
- case KEY_DOWN:
- case CONTROL('N'):
- return SEL_NEXT;
- /* Previous */
- case 'k':
- case KEY_UP:
- case CONTROL('P'):
- return SEL_PREV;
- /* Page down */
- case KEY_NPAGE:
- case CONTROL('D'):
- return SEL_PGDN;
- /* Page up */
- case KEY_PPAGE:
- case CONTROL('U'):
- return SEL_PGUP;
- case '!':
- return SEL_SH;
- case 'c':
- return SEL_CD;
+
+ for (i = 0; i < LEN(bindings); i++) {
+ if (c == bindings[i].sym)
+ return bindings[i].act;
}
return 0;