cmdtree

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

commit 6e938b9a190ae6c41614b2377f44d4e0ffc28b78
parent cf9ffbdb247bc3c5b31fbc7a645429dedf3d97f1
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  9 Jul 2018 18:35:11 -0700

cleanup

Diffstat:
Mcfg.def.h | 8--------
Mcmdtree.c | 11+++++------
Mcommand.c | 69+++++++++++++++++++++++++++++++++------------------------------------
Mcommand.h | 9++++-----
4 files changed, 42 insertions(+), 55 deletions(-)

diff --git a/cfg.def.h b/cfg.def.h @@ -39,11 +39,3 @@ static struct scheme schemes[SchemeLast] = { }, }; -/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ -static unsigned int lines = 0; - -/* - * Characters not considered part of a word while deleting words - * for example: " /?\"&[]" - */ -static const char worddelimiters[] = " "; diff --git a/cmdtree.c b/cmdtree.c @@ -23,6 +23,7 @@ enum { }; /* color schemes */ static struct command *rootcmds; +static int ncmds; static Window root, parentwin, win; static int screen; static Display *display; @@ -187,7 +188,6 @@ static void draw_tree_vertical(Drw *drw, struct command *cmds, int x, int y, int w, int h) { int i; int colw = 0; - int ncmds = tal_count(cmds); x += xpad; y += ypad; @@ -198,7 +198,6 @@ draw_tree_vertical(Drw *drw, struct command *cmds, int x, int y, int w, int h) { int c = '0'; - for (i = 0; i < ncmds; ++i, ++c, y += bh) { struct command *cmd = &cmds[i]; if (y >= mh) { @@ -254,7 +253,6 @@ draw_tree(Drw *drw, int x, int y, int w, int h) { static void cleanup(Drw *drw, int code) { drw_free(drw); - tal_free(rootcmds); exit(0); } @@ -285,11 +283,12 @@ run(Drw *drw) { break; } - cmd = command_lookup(rootcmds, buf); + cmd = command_lookup(rootcmds, ncmds, buf); if (cmd) { if (command_is_prefix(cmd)) { rootcmds = cmd->children; + ncmds = cmd->nchildren; draw_tree(drw, 0, 0, mw, mh); } else { @@ -323,7 +322,7 @@ int main(void) { /* parentwin = root; */ parentwin = root; - rootcmds = test_root_commands(NULL); + rootcmds = test_root_commands(NULL, &ncmds); if (!XGetWindowAttributes(display, parentwin, &wa)) die("could not get embedding window attributes: 0x%lx", @@ -334,7 +333,7 @@ int main(void) { if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) die("no fonts could be loaded."); - grabkeyboard(); + /* grabkeyboard(); */ setup(drw); run(drw); diff --git a/command.c b/command.c @@ -6,18 +6,13 @@ #include "ccan/str/str.h" #include <err.h> #include <unistd.h> +#include <stdio.h> #include "util.h" -void -command_init(struct command *cmd) { - cmd->children = NULL; -} - int command_is_prefix(struct command *cmd) { - size_t count = tal_count(cmd->children); - return count > 0; + return cmd->nchildren > 0; } void @@ -26,10 +21,14 @@ command_exec(struct command *cmd) { err(1, "executing command %s", cmd->name); } +int +command_num_children(struct command *cmd) { + return tal_count(cmd) || cmd->nchildren; +} + struct command * -command_lookup(struct command *cmd, const char *binding) { - size_t len = tal_count(cmd); - for (size_t i = 0; i < len; ++i) { +command_lookup(struct command *cmd, int ncmds, const char *binding) { + for (int i = 0; i < ncmds; ++i) { if (streq(binding, cmd[i].bind)) return &cmd[i]; } @@ -37,37 +36,35 @@ command_lookup(struct command *cmd, const char *binding) { return NULL; } +static struct command emacs_commands[] = { + { .bind = "d", .name = "emacs-dev", .nchildren = 0, .children = NULL }, +}; + static const struct command examples[] = { - { .bind = "f", .name = "firefox" }, - { .bind = "m", .name = "misc" }, - { .bind = "e", .name = "emacs" }, - { .bind = "N", .name = "networking" }, + { .bind = "f", .name = "firefox", .nchildren = 0, .children = NULL }, + + { .bind = "e", + .name = "emacs", + .children = emacs_commands, + .nchildren = LENGTH(emacs_commands) + }, + + { .bind = "N", .name = "networking", .nchildren = 0, .children = NULL }, }; struct command * -test_root_commands(tal_t *ctx) { - unsigned long i, j; +test_root_commands(tal_t *ctx, int *ncmds) { + unsigned long i; struct command *cmds = NULL; - struct command *child = NULL; - - cmds = tal_arr(ctx, struct command, 10); - - const unsigned long c = 'a'; - for (i = 0; i < tal_count(cmds); i++) { - if (i < LENGTH(examples)) { - cmds[i].name = examples[i].name; - cmds[i].bind = examples[i].bind; - } - else { - cmds[i].name = tal_fmt(cmds, "example-%d", (int)i); - cmds[i].bind = tal_fmt(cmds, "%c", (int)(c+i)); - } - child = cmds[i].children = tal_arr(cmds, struct command, i % 2); - for (j = 0; j < tal_count(child); j++) { - child[j].name = "sayhi"; - child[j].bind = tal_fmt(child, "%c", (int)(c+j)); - child[j].children = NULL; - } + + cmds = tal_arr(ctx, struct command, LENGTH(examples)); + *ncmds = LENGTH(examples); + + for (i = 0; i < LENGTH(examples); ++i) { + cmds[i].children = examples[i].children; + cmds[i].name = examples[i].name; + cmds[i].bind = examples[i].bind; + cmds[i].nchildren = examples[i].nchildren; } return cmds; diff --git a/command.h b/command.h @@ -7,8 +7,8 @@ struct command { char *name; char *bind; - char *exec; struct command *children; + int nchildren; }; @@ -18,14 +18,13 @@ command_init(struct command *cmd); void command_exec(struct command *cmd); -struct command * -command_lookup(struct command *cmd, const char *binding); - int command_is_prefix(struct command *cmd); +struct command * +command_lookup(struct command *cmd, int ncmds, const char *binding); struct command * -test_root_commands(tal_t *ctx); +test_root_commands(tal_t *ctx, int *ncmds); #endif /* CMDTREE_COMMAND_H */