From 62039f4a56c467d8903f682e4c944c24afc30d35 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jonathan=20Neusch=C3=A4fer?= Date: Sat, 24 Dec 2011 18:28:06 +0100 Subject: [PATCH] screen_keydef: show command descriptions --- NEWS | 1 + src/command.c | 24 ++++++++++++++++++++++++ src/command.h | 1 + src/screen_keydef.c | 19 ++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 5ca49b1..f435e1c 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ ncmpc 0.20 - not yet released configure script is called with --enable-documentation * screen_lyrics: You can now edit lyrics by pressing 'e' * configurable timeout for MPD connections +* screen_keydef: show command descriptions ncmpc 0.19 - (07/23/2011) diff --git a/src/command.c b/src/command.c index 656bb18..85e499a 100644 --- a/src/command.c +++ b/src/command.c @@ -262,6 +262,30 @@ get_command_definitions(void) { return cmds; } + +size_t +get_cmds_max_name_width(command_definition_t *c) +{ + static size_t max = 0; + + if (max != 0) + return max; + + size_t len; + command_definition_t *p; + + for (p = c; p->name != NULL; p++) { + /* + * width and length are considered the same here, as command + * names are not translated. + */ + len = (size_t) strlen(p->name); + if (len > max) + max = len; + } + + return max; +} #endif const char * diff --git a/src/command.h b/src/command.h index 225d76e..847d5ed 100644 --- a/src/command.h +++ b/src/command.h @@ -127,6 +127,7 @@ typedef struct { #ifdef ENABLE_KEYDEF_SCREEN command_definition_t *get_command_definitions(void); +size_t get_cmds_max_name_width(command_definition_t *cmds); #endif command_t find_key_command(int key, command_definition_t *cmds); diff --git a/src/screen_keydef.c b/src/screen_keydef.c index 8174c04..22797ea 100644 --- a/src/screen_keydef.c +++ b/src/screen_keydef.c @@ -241,7 +241,24 @@ list_callback(unsigned idx, G_GNUC_UNUSED void *data) assert(idx < (unsigned) command_n_commands); - return cmds[idx].name; + /* + * Format the lines in two aligned columnes for the key name and + * the description, like this: + * + * this-command - do this + * that-one - do that + */ + size_t len = strlen(cmds[idx].name); + strncpy(buf, cmds[idx].name, sizeof(buf)); + + if (len < get_cmds_max_name_width(cmds)) + memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len); + + g_snprintf(buf + get_cmds_max_name_width(cmds), + sizeof(buf) - get_cmds_max_name_width(cmds), + " - %s", _(cmds[idx].description)); + + return buf; } else { if (idx == subcmd_item_up) return "[..]"; -- 2.30.2