commit 321b23c68aa8e0c2445fdddee632693a06fd86e1
parent bb0f1e2ea23bb278f02c2a4ff19e5c6fa6946b92
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 21 Feb 2022 18:11:04 +0100
Added rerun command
Diffstat:
4 files changed, 91 insertions(+), 32 deletions(-)
diff --git a/src/cmd.c b/src/cmd.c
@@ -16,6 +16,9 @@
return false; \
} while (0)
+static const struct cmd *last_cmd;
+static wchar_t *last_args;
+
static bool cmd_save(const wchar_t *args);
static bool cmd_move(const wchar_t *args);
static bool cmd_add(const wchar_t *args);
@@ -30,20 +33,6 @@ const struct cmd commands[] = {
const size_t command_count = ARRLEN(commands);
-char *cmd_status;
-
-void
-cmd_init(void)
-{
- cmd_status = NULL;
-}
-
-void
-cmd_deinit(void)
-{
- free(cmd_status);
-}
-
bool
cmd_save(const wchar_t *args)
{
@@ -148,3 +137,71 @@ cmd_reindex(const wchar_t *name)
return true;
}
+
+void
+cmd_init(void)
+{
+ last_cmd = NULL;
+ last_args = NULL;
+}
+
+void
+cmd_deinit(void)
+{
+ free(last_args);
+}
+
+bool
+cmd_run(const wchar_t *query)
+{
+ const wchar_t *sep, *args;
+ int i, cmdlen;
+ bool success;
+
+ sep = wcschr(query, L' ');
+ cmdlen = sep ? sep - query : wcslen(query);
+ for (i = 0; i < command_count; i++) {
+ if (!wcsncmp(commands[i].name, query, cmdlen)) {
+ last_cmd = &commands[i];
+ args = sep ? sep + 1 : L"";
+ last_args = wcsdup(args);
+ return commands[i].func(args);
+ }
+ }
+
+ return false;
+}
+
+bool
+cmd_rerun(void)
+{
+ if (!last_cmd || !last_args)
+ CMD_ERROR("No command to repeat");
+ return last_cmd->func(last_args);
+}
+
+const struct cmd *
+cmd_get(const wchar_t *name)
+{
+ int i;
+
+ for (i = 0; i < command_count; i++) {
+ if (!wcscmp(commands[i].name, name))
+ return &commands[i];
+ }
+
+ return NULL;
+}
+
+const struct cmd *
+cmd_find(const wchar_t *name)
+{
+ int i;
+
+ for (i = 0; i < command_count; i++) {
+ if (wcsstr(commands[i].name, name))
+ return &commands[i];
+ }
+
+ return NULL;
+}
diff --git a/src/cmd.h b/src/cmd.h
@@ -19,6 +19,11 @@ struct cmd {
void cmd_init(void);
void cmd_deinit(void);
+bool cmd_run(const wchar_t *name);
+bool cmd_rerun(void);
+
+const struct cmd *cmd_get(const wchar_t *name);
+const struct cmd *cmd_find(const wchar_t *name);
+
extern const struct cmd commands[];
extern const size_t command_count;
-extern char *cmd_status;
diff --git a/src/tui.c b/src/tui.c
@@ -96,6 +96,7 @@ struct list *tracks_vis;
int track_show_playlist;
struct listnav tag_nav;
struct listnav track_nav;
+char *cmd_status;
const char imode_prefix[IMODE_COUNT] = {
[IMODE_EXECUTE] = ':',
@@ -400,22 +401,12 @@ track_pane_vis(struct pane *pane, int sel)
bool
run_cmd(const wchar_t *query)
{
- const wchar_t *sep;
- int i, cmdlen;
bool success;
- sep = wcschr(query, L' ');
- cmdlen = sep ? sep - query : wcslen(query);
- for (i = 0; i < command_count; i++) {
- if (!wcsncmp(commands[i].name, query, cmdlen)) {
- success = commands[i].func(sep ? sep + 1 : L"");
- if (!success && !cmd_status)
- CMD_SET_STATUS("Command Failed!");
- return true;
- }
- }
-
- return false;
+ success = cmd_run(query);
+ if (!success && !cmd_status)
+ CMD_SET_STATUS("Command Failed!");
+ return true;
}
bool
@@ -682,9 +673,7 @@ cmd_pane_vis(struct pane *pane, int sel)
}
} else if (cmd_status) {
pane_clearln(pane, 2);
- style_on(pane->win, STYLE_ERROR);
- mvwprintw(pane->win, 2, 0, ">%.*s", pane->w - 1, cmd_status);
- style_off(pane->win, STYLE_ERROR);
+ mvwprintw(pane->win, 2, 0, " %.*s", pane->w - 1, cmd_status);
}
}
@@ -779,6 +768,9 @@ main_input(wint_t c)
player_play();
}
break;
+ case L'.':
+ cmd_rerun();
+ break;
case L':':
cmd_input_mode = IMODE_EXECUTE;
pane_after_cmd = pane_sel;
@@ -905,6 +897,8 @@ tui_init(void)
quit = 0;
cmd_input_mode = IMODE_TRACK_SELECT;
+ cmd_status = NULL;
+
inputln_init(&completion_query);
completion_reset = 1;
@@ -937,6 +931,8 @@ tui_init(void)
void
tui_deinit(void)
{
+ free(cmd_status);
+
pane_free(&pane_left);
pane_free(&pane_right);
pane_free(&pane_bot);
diff --git a/src/tui.h b/src/tui.h
@@ -22,4 +22,5 @@ extern int track_show_playlist;
extern struct listnav tag_nav;
extern struct listnav track_nav;
+extern char *cmd_status;