Code

screen_keydef: show command descriptions
authorJonathan Neuschäfer <j.neuschaefer@gmx.net>
Sat, 24 Dec 2011 17:28:06 +0000 (18:28 +0100)
committerJonathan Neuschäfer <j.neuschaefer@gmx.net>
Sat, 24 Dec 2011 17:28:06 +0000 (18:28 +0100)
NEWS
src/command.c
src/command.h
src/screen_keydef.c

diff --git a/NEWS b/NEWS
index 5ca49b188d05d4f63b72cda0ab995ae96e7d6d71..f435e1ce05bfd1f7a835925dd7ef3981bd40fc59 100644 (file)
--- 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)
index 656bb18e389db0bd89a1389be818a861d2a7a3e1..85e499a00873ad2c985dbfbb1241c26b7b1006f2 100644 (file)
@@ -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 *
index 225d76e5fb4bfa5e4a7c00a9ea26eaf6a89d4083..847d5ed7c22c2edf466fc6b58da11897c484ce96 100644 (file)
@@ -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);
index 8174c0426e3a0f4c582a0e8edbdcb0b3c65804ca..22797eab10b8c5bd83b257db9c3cfaa8059960a9 100644 (file)
@@ -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 "[..]";