Key bindings to run user-defined commands

This commit is contained in:
lostd 2015-03-12 16:12:01 +02:00
parent bc11d7b201
commit 3dc40acbff
3 changed files with 33 additions and 15 deletions

View file

@ -45,11 +45,14 @@ struct key bindings[] = {
/* Page up */ /* Page up */
{ KEY_PPAGE, SEL_PGUP }, { KEY_PPAGE, SEL_PGUP },
{ CONTROL('U'), SEL_PGUP }, { CONTROL('U'), SEL_PGUP },
/* Shell */
{ '!', SEL_SH },
/* Change dir */ /* Change dir */
{ 'c', SEL_CD }, { 'c', SEL_CD },
/* Toggle sort by time */ /* Toggle sort by time */
{ 't', SEL_MTIME }, { 't', SEL_MTIME },
{ CONTROL('L'), SEL_REDRAW }, { CONTROL('L'), SEL_REDRAW },
/* Run command */
{ '!', SEL_RUN, "sh" },
{ 'z', SEL_RUN, "top" },
/* Run command with argument */
{ 'e', SEL_RUNARG, "vi" },
}; };

View file

@ -1,4 +1,4 @@
.Dd January 27, 2015 .Dd March 12, 2015
.Dt NOICE 1 .Dt NOICE 1
.Os .Os
.Sh NAME .Sh NAME
@ -52,7 +52,11 @@ Toggle sort by time modified.
.It Ic l .It Ic l
Force a redraw. Force a redraw.
.It Ic ! .It Ic !
Spawn shell in current directory. Spawn an sh shell in current directory.
.It Ic z
Run the system top utility.
.It Ic e
Open selected entry with the vi editor.
.It Ic q .It Ic q
Quit. Quit.
.El .El

33
noice.c
View file

@ -54,15 +54,17 @@ enum action {
SEL_PREV, SEL_PREV,
SEL_PGDN, SEL_PGDN,
SEL_PGUP, SEL_PGUP,
SEL_SH,
SEL_CD, SEL_CD,
SEL_MTIME, SEL_MTIME,
SEL_REDRAW, SEL_REDRAW,
SEL_RUN,
SEL_RUNARG,
}; };
struct key { struct key {
int sym; /* Key pressed */ int sym; /* Key pressed */
enum action act; /* Action */ enum action act; /* Action */
char *run; /* Program to run */
}; };
#include "config.h" #include "config.h"
@ -296,17 +298,20 @@ printprompt(char *str)
printw(str); printw(str);
} }
/* Returns SEL_* if key is bound and 0 otherwise */ /* Returns SEL_* if key is bound and 0 otherwise
Also modifies the run pointer (used on SEL_{RUN,RUNARG}) */
int int
nextsel(void) nextsel(char **run)
{ {
int c, i; int c, i;
c = getch(); c = getch();
for (i = 0; i < LEN(bindings); i++) for (i = 0; i < LEN(bindings); i++)
if (c == bindings[i].sym) if (c == bindings[i].sym) {
*run = bindings[i].run;
return bindings[i].act; return bindings[i].act;
}
return 0; return 0;
} }
@ -571,7 +576,7 @@ browse(const char *ipath, const char *ifilter)
regex_t filter_re, re; regex_t filter_re, re;
char *cwd, *newpath, *oldpath = NULL; char *cwd, *newpath, *oldpath = NULL;
struct stat sb; struct stat sb;
char *name, *bin, *dir, *tmp; char *name, *bin, *dir, *tmp, *run;
int nowtyping = 0; int nowtyping = 0;
begin: begin:
@ -640,7 +645,7 @@ begin:
goto moretyping; goto moretyping;
nochange: nochange:
switch (nextsel()) { switch (nextsel(&run)) {
case SEL_QUIT: case SEL_QUIT:
free(path); free(path);
free(filter); free(filter);
@ -789,11 +794,6 @@ moretyping:
if (cur > 0) if (cur > 0)
cur -= MIN((LINES - 4) / 2, cur); cur -= MIN((LINES - 4) / 2, cur);
break; break;
case SEL_SH:
exitcurses();
spawn("/bin/sh", NULL, path);
initcurses();
break;
case SEL_CD: case SEL_CD:
/* Read target dir */ /* Read target dir */
printprompt("chdir: "); printprompt("chdir: ");
@ -820,6 +820,17 @@ moretyping:
goto out; goto out;
case SEL_REDRAW: case SEL_REDRAW:
goto out; goto out;
case SEL_RUN:
exitcurses();
spawn(run, NULL, path);
initcurses();
break;
case SEL_RUNARG:
name = dents[cur].name;
exitcurses();
spawn(run, name, path);
initcurses();
break;
} }
} }