Compare commits

...

3 Commits

Author SHA1 Message Date
6298235e22
Add actions for repeat/random/single/consume
Now the user can at least toggle them from the help tab,
or even bind them as necessary.
2018-10-29 09:58:43 +01:00
841e2f79c0
Make help tab items actionable 2018-10-29 09:46:45 +01:00
5ade0f082e
Show unbound actions in help 2018-10-29 09:19:34 +01:00

114
nncmpp.c
View File

@ -1548,6 +1548,10 @@ app_goto_tab (int tab_index)
XX( MPD_SEARCH, "Global search" ) \
XX( MPD_ADD, "Add selection to playlist" ) \
XX( MPD_REPLACE, "Replace playlist" ) \
XX( MPD_REPEAT, "Toggle repeat" ) \
XX( MPD_RANDOM, "Toggle random playback" ) \
XX( MPD_SINGLE, "Toggle single song playback" ) \
XX( MPD_CONSUME, "Toggle consume" ) \
XX( MPD_UPDATE_DB, "Update MPD database" ) \
XX( MPD_COMMAND, "Send raw command to MPD" ) \
\
@ -1698,6 +1702,14 @@ app_on_editor_end (bool confirmed)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
app_mpd_toggle (const char *name)
{
const char *s = str_map_find (&g.playback_info, name);
bool value = s && strcmp (s, "0");
return MPD_SIMPLE (name, value ? "0" : "1");
}
static bool
app_process_action (enum action action)
{
@ -1767,6 +1779,10 @@ app_process_action (enum action action)
case ACTION_MPD_NEXT: return MPD_SIMPLE ("next");
case ACTION_MPD_FORWARD: return MPD_SIMPLE ("seekcur", "+10");
case ACTION_MPD_BACKWARD: return MPD_SIMPLE ("seekcur", "-10");
case ACTION_MPD_REPEAT: return app_mpd_toggle ("repeat");
case ACTION_MPD_RANDOM: return app_mpd_toggle ("random");
case ACTION_MPD_SINGLE: return app_mpd_toggle ("single");
case ACTION_MPD_CONSUME: return app_mpd_toggle ("consume");
case ACTION_MPD_UPDATE_DB: return MPD_SIMPLE ("update");
case ACTION_MPD_VOLUME_UP: return app_setvol (g.volume + 10);
@ -3053,7 +3069,28 @@ info_tab_init (void)
// --- Help tab ----------------------------------------------------------------
static struct strv g_help_tab_lines;
static struct
{
struct tab super; ///< Parent class
ARRAY (enum action, actions) ///< Actions for content
struct strv lines; ///< Visible content
}
g_help_tab;
static bool
help_tab_on_action (enum action action)
{
struct tab *self = g.active_tab;
if (self->item_selected < 0
|| self->item_selected >= (int) g_help_tab.actions_len
|| action != ACTION_CHOOSE)
return false;
action = g_help_tab.actions[self->item_selected];
return action != ACTION_NONE
&& action != ACTION_CHOOSE // avoid recursion
&& app_process_action (action);
}
static void
help_tab_strfkey (const termo_key_t *key, struct strv *out)
@ -3071,7 +3108,20 @@ help_tab_strfkey (const termo_key_t *key, struct strv *out)
}
static void
help_tab_group (struct binding *keys, size_t len, struct strv *out)
help_tab_assign_action (enum action action)
{
hard_assert (g_help_tab.lines.len > g_help_tab.actions_len);
size_t to_push = g_help_tab.lines.len - g_help_tab.actions_len;
ARRAY_RESERVE (g_help_tab.actions, to_push);
for (size_t i = 1; i < to_push; i++)
g_help_tab.actions[g_help_tab.actions_len++] = ACTION_NONE;
g_help_tab.actions[g_help_tab.actions_len++] = action;
}
static void
help_tab_group (struct binding *keys, size_t len, struct strv *out,
bool bound[ACTION_COUNT])
{
for (enum action i = 0; i < ACTION_COUNT; i++)
{
@ -3085,39 +3135,71 @@ help_tab_group (struct binding *keys, size_t len, struct strv *out)
strv_append_owned (out, xstrdup_printf
(" %-30s %s", g_actions[i].description, joined));
free (joined);
bound[i] = true;
help_tab_assign_action (i);
}
strv_free (&ass);
}
}
static void
help_tab_unbound (struct strv *out, bool bound[ACTION_COUNT])
{
for (enum action i = 0; i < ACTION_COUNT; i++)
if (!bound[i])
{
strv_append_owned (out,
xstrdup_printf (" %-30s", g_actions[i].description));
help_tab_assign_action (i);
}
}
static void
help_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width)
{
(void) width;
hard_assert (item_index < g_help_tab_lines.len);
const char *line = g_help_tab_lines.vector[item_index];
hard_assert (item_index < g_help_tab.lines.len);
const char *line = g_help_tab.lines.vector[item_index];
row_buffer_append (buffer, line, *line == ' ' ? 0 : A_BOLD);
}
static struct tab *
help_tab_init (void)
{
g_help_tab_lines = strv_make ();
ARRAY_INIT (g_help_tab.actions);
struct strv *lines = &g_help_tab.lines;
*lines = strv_make ();
strv_append (&g_help_tab_lines, "Normal mode actions");
help_tab_group (g_normal_keys, g_normal_keys_len, &g_help_tab_lines);
strv_append (&g_help_tab_lines, "");
bool bound[ACTION_COUNT] = { [ACTION_NONE] = true };
strv_append (&g_help_tab_lines, "Editor mode actions");
help_tab_group (g_editor_keys, g_editor_keys_len, &g_help_tab_lines);
strv_append (&g_help_tab_lines, "");
strv_append (lines, "Normal mode actions");
help_tab_group (g_normal_keys, g_normal_keys_len, lines, bound);
strv_append (lines, "");
static struct tab super;
tab_init (&super, "Help");
super.on_item_draw = help_tab_on_item_draw;
super.item_count = g_help_tab_lines.len;
return &super;
strv_append (lines, "Editor mode actions");
help_tab_group (g_editor_keys, g_editor_keys_len, lines, bound);
strv_append (lines, "");
bool have_unbound = false;
for (enum action i = 0; i < ACTION_COUNT; i++)
if (!bound[i])
have_unbound = true;
if (have_unbound)
{
strv_append (lines, "Unbound actions");
help_tab_unbound (lines, bound);
strv_append (lines, "");
}
struct tab *super = &g_help_tab.super;
tab_init (super, "Help");
super->on_action = help_tab_on_action;
super->on_item_draw = help_tab_on_item_draw;
super->item_count = lines->len;
return super;
}
// --- Debug tab ---------------------------------------------------------------