aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-08-07 20:44:34 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-08-07 20:44:34 +0100
commitc0f1d73df3fef83cb6a60825c08620de0ded39d5 (patch)
treee469bf39d935ac82f5645b7449612ac5c0814886
parente889f417c7edd3d47a54b9464e1b6b72c3f7ae1c (diff)
downloadimv-c0f1d73df3fef83cb6a60825c08620de0ded39d5.tar.gz
Make aliases smarter
-rw-r--r--src/commands.c37
-rw-r--r--src/imv.c20
2 files changed, 47 insertions, 10 deletions
diff --git a/src/commands.c b/src/commands.c
index ca40244..3808929 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -1,12 +1,41 @@
#include "commands.h"
#include "list.h"
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
struct command {
char* command;
void (*handler)(struct list *args, const char *argstr, void *data);
char* alias;
};
+static char *join_str_list(struct list *list, const char *sep, size_t start)
+{
+ size_t len = 0;
+ size_t cap = 512;
+ char *buf = malloc(cap);
+ buf[0] = 0;
+
+ size_t sep_len = strlen(sep);
+ for (size_t i = start; i < list->len; ++i) {
+ size_t item_len = strlen(list->items[i]);
+ if (len + item_len + sep_len >= cap) {
+ cap *= 2;
+ buf = realloc(buf, cap);
+ assert(buf);
+ }
+
+ strncat(buf, list->items[i], cap);
+ len += item_len;
+
+ strncat(buf, sep, cap);
+ len += sep_len;
+ }
+ return buf;
+}
+
struct imv_commands *imv_commands_create(void)
{
struct imv_commands *cmds = malloc(sizeof *cmds);
@@ -61,7 +90,13 @@ int imv_command_exec(struct imv_commands *cmds, const char *command, void *data)
cmd->handler(args, argstr, data);
ret = 0;
} else if(cmd->alias) {
- ret = imv_command_exec(cmds, cmd->alias, data);
+ char *new_args = join_str_list(args, " ", 1);
+ size_t cmd_len = strlen(cmd->alias) + 1 + strlen(new_args) + 1;
+ char *new_cmd = malloc(cmd_len);
+ snprintf(new_cmd, cmd_len, "%s %s", cmd->alias, new_args);
+ ret = imv_command_exec(cmds, new_cmd, data);
+ free(new_args);
+ free(new_cmd);
}
break;
}
diff --git a/src/imv.c b/src/imv.c
index d0467eb..40831fb 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -510,18 +510,12 @@ struct imv *imv_create(void)
);
imv_command_register(imv->commands, "quit", &command_quit);
- imv_command_register(imv->commands, "q", &command_quit);
imv_command_register(imv->commands, "pan", &command_pan);
imv_command_register(imv->commands, "next", &command_next);
- imv_command_register(imv->commands, "n", &command_next);
imv_command_register(imv->commands, "prev", &command_prev);
- imv_command_register(imv->commands, "p", &command_prev);
imv_command_register(imv->commands, "goto", &command_goto);
- imv_command_register(imv->commands, "g", &command_goto);
imv_command_register(imv->commands, "zoom", &command_zoom);
- imv_command_register(imv->commands, "z", &command_zoom);
imv_command_register(imv->commands, "open", &command_open);
- imv_command_register(imv->commands, "o", &command_open);
imv_command_register(imv->commands, "close", &command_close);
imv_command_register(imv->commands, "fullscreen", &command_fullscreen);
imv_command_register(imv->commands, "overlay", &command_overlay);
@@ -533,7 +527,15 @@ struct imv *imv_create(void)
imv_command_register(imv->commands, "scaling", &command_set_scaling_mode);
imv_command_register(imv->commands, "slideshow", &command_set_slideshow_duration);
imv_command_register(imv->commands, "background", &command_set_background);
- imv_command_register(imv->commands, "bg", &command_set_background);
+
+ imv_command_alias(imv->commands, "q", "quit");
+ imv_command_alias(imv->commands, "n", "next");
+ imv_command_alias(imv->commands, "p", "prev");
+ imv_command_alias(imv->commands, "g", "goto");
+ imv_command_alias(imv->commands, "z", "zoom");
+ imv_command_alias(imv->commands, "o", "open");
+ imv_command_alias(imv->commands, "bg", "background");
+ imv_command_alias(imv->commands, "ss", "slideshow");
add_bind(imv, "q", "quit");
add_bind(imv, "<Left>", "prev");
@@ -563,8 +565,8 @@ struct imv *imv_create(void)
add_bind(imv, "r", "reset");
add_bind(imv, "<period>", "next_frame");
add_bind(imv, "<space>", "toggle_playing");
- add_bind(imv, "t", "slideshow_duration +1");
- add_bind(imv, "<Shift+T>", "slideshow_duration -1");
+ add_bind(imv, "t", "slideshow +1");
+ add_bind(imv, "<Shift+T>", "slideshow -1");
return imv;
}