cmdtree

A trie command launcher for X11
git clone git://jb55.com/cmdtree
Log | Files | Refs | README | LICENSE

commit 22e403ca533419e1d19bf5c4dad8b2bbc4eb2173
parent e4230b572ef598ee38da4334f0482b81496fbf0c
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  9 Jul 2018 17:27:37 -0700

subtree nav works

Diffstat:
MTODO.org | 19+++++++++++++++++--
Mcfg.def.h | 2+-
Mcmdtree.c | 27++++++++++++++++++++-------
Mcommand.c | 13+++++++++++++
Mcommand.h | 3+++
5 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/TODO.org b/TODO.org @@ -1,7 +1,22 @@ -* TODO command struct test data - * TODO column layout + + +* DONE redraw prefix with subtree +CLOSED: [2018-07-09 Mon 17:26] * DONE vertical layout CLOSED: [2018-07-09 Mon 15:25] +* DONE command struct test data +CLOSED: [2018-07-09 Mon 17:10] +* DONE draw command +CLOSED: [2018-07-09 Mon 10:39] +* DONE close window on escape +CLOSED: [2018-07-08 Sun 12:20] +* DONE popup window +CLOSED: [2018-07-08 Sun 12:15] +* DONE draw key as well +CLOSED: [2018-07-09 Mon 14:30] +* DONE command/bind different color +CLOSED: [2018-07-09 Mon 14:30] + diff --git a/cfg.def.h b/cfg.def.h @@ -11,7 +11,7 @@ enum position { static int xpad = 6; static int ypad = 2; -static enum position position = POSITION_RIGHT; /* -b option; if 0, dmenu appears at bottom */ +static enum position position = POSITION_TOP; /* -b option; if 0, dmenu appears at bottom */ static const char *separator = " → "; diff --git a/cmdtree.c b/cmdtree.c @@ -118,7 +118,6 @@ setup(Drw *drw) XSelectInput(display, win, ExposureMask | KeyPressMask); XMapWindow(display, win); - XSetInputFocus(display, win, RevertToParent, CurrentTime); // XXXembed @@ -133,7 +132,7 @@ setup(Drw *drw) /* } */ /* drw_resize(drw, mw, mh); */ - /* drawmenu(); */ + /* draw_tree(drw); */ } static int @@ -145,8 +144,6 @@ draw_command(Drw *drw, int x, int y, struct command *cmd) { int invert = 0; int isprefix = 0; char *name = cmd->name; - struct scheme *prefix = &schemes[SchemePrefix]; - struct scheme *norm = &schemes[SchemeNorm]; struct scheme *scheme = NULL; //drw_fontset_getwidth(drw, binding) + 1; @@ -165,9 +162,9 @@ draw_command(Drw *drw, int x, int y, struct command *cmd) { isprefix = command_is_prefix(cmd); if (isprefix) - scheme = prefix; + scheme = &schemes[SchemePrefix]; else - scheme = norm; + scheme = &schemes[SchemeNorm]; drw_setscheme(drw, &scheme->name_clr, &scheme->bg_clr); @@ -269,8 +266,10 @@ run(Drw *drw) { KeySym ksym = NoSymbol; Status status; int code = 0; + struct command *cmd = NULL; while (!done) { + cmd = NULL; XNextEvent(display, &e); switch (e.type) { case Expose: @@ -279,10 +278,24 @@ run(Drw *drw) { case KeyPress: XmbLookupString(xic, (XKeyEvent*)&e, buf, sizeof buf, &ksym, &status); - if (ksym == XK_q || ksym == XK_Escape) { + + if (ksym == XK_Escape) { code = 1; done = 1; + break; + } + + cmd = command_lookup(rootcmds, buf); + + if (cmd) { + if (command_is_prefix(cmd)) { + rootcmds = cmd->children; + draw_tree(drw, 0, 0, mw, mh); + } + else // TODO: launch command + done = 1; } + break; } } diff --git a/command.c b/command.c @@ -3,6 +3,7 @@ #include "ccan/tal/tal.h" #include "ccan/tal/str/str.h" +#include "ccan/str/str.h" #include "util.h" @@ -17,6 +18,18 @@ command_is_prefix(struct command *cmd) { return count > 0; } + +struct command * +command_lookup(struct command *cmd, const char *binding) { + size_t len = tal_count(cmd); + for (size_t i = 0; i < len; ++i) { + if (streq(binding, cmd[i].bind)) + return &cmd[i]; + } + + return NULL; +} + static const struct command examples[] = { { .bind = "f", .name = "firefox" }, { .bind = "m", .name = "misc" }, diff --git a/command.h b/command.h @@ -15,6 +15,9 @@ struct command { void command_init(struct command *cmd); +struct command * +command_lookup(struct command *cmd, const char *binding); + int command_is_prefix(struct command *cmd);