Code

screen_browser: added custom list painting function
authorMax Kellermann <max@duempel.org>
Sun, 11 Oct 2009 12:55:23 +0000 (14:55 +0200)
committerMax Kellermann <max@duempel.org>
Sun, 11 Oct 2009 12:55:23 +0000 (14:55 +0200)
Side effect: playlists and directories are rendered with different
colors (configuration "browser-directory" and "browser-playlist").

NEWS
doc/config.sample
src/colors.c
src/colors.h
src/screen_browser.c

diff --git a/NEWS b/NEWS
index 4ce914c5decf67f3758fdd3307840b7f5a80a2fa..15ed5c3ed3da214cab19d742380e8b35f3563267 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,7 @@ ncmpc 0.16 - not yet released
 * screen_play: repaint after the "select-playing" command
 * screen_text: start searching at window origin, not bottom
 * strfsong: support multiple values for a tag
+* screen_browser: different colors for directories and playlists
 
 
 ncmpc 0.15 - 2009-09-24
index e064ebbedbe72dd9fd8558247ae61291617ffc92..9bd61c75d3322370acd2f224b53faeb7253b8173 100644 (file)
 ## Set the bold text color in the main area of ncmpc.
 #color list-bold = brightgreen
 
+## Sets the text color of directories in the browser
+#color browser-directory = yellow
+
+## Sets the text color of playlists in the browser
+#color browser-playlist = red
+
 ## Set the color of the progress indicator.
 #color progressbar = white
 
index 14c7cc76277ae3064bfca6225b05c6335c68bc90..a6c7e2cca9c665a741596483ac385bdf66a84a18 100644 (file)
@@ -82,6 +82,16 @@ static color_entry_t colors[COLOR_END] = {
        [COLOR_STATUS_BOLD] = { NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
        [COLOR_STATUS_TIME] = { NAME_STATUS_TIME, COLOR_RED, A_NORMAL },
        [COLOR_STATUS_ALERT] = { NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD },
+       [COLOR_DIRECTORY] = {
+               .name = "browser-directory",
+               .fg = COLOR_YELLOW,
+               .attrs = A_NORMAL,
+       },
+       [COLOR_PLAYLIST] = {
+               .name = "browser-playlist",
+               .fg = COLOR_RED,
+               .attrs = A_NORMAL,
+       },
 };
 
 #ifdef ENABLE_COLORS
index 305e39678656f6afe816255ea72ef277786fdd20..d317760f23c421330c6750992b848eabeeff7224 100644 (file)
@@ -40,6 +40,8 @@ enum color {
        COLOR_STATUS_BOLD,
        COLOR_STATUS_TIME,
        COLOR_STATUS_ALERT,
+       COLOR_DIRECTORY,
+       COLOR_PLAYLIST,
        COLOR_END
 };
 
index 3b0fc00ccf2a5610880c06cde0ece8e8e93f4649..16a2f5e7045c48233741e19aada820c76b1cdf02 100644 (file)
@@ -30,6 +30,8 @@
 #include "strfsong.h"
 #include "mpdclient.h"
 #include "filelist.h"
+#include "colors.h"
+#include "paint.h"
 
 #include <mpd/client.h>
 
@@ -384,6 +386,10 @@ browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
 }
 #endif
 
+static void
+screen_browser_paint_callback(WINDOW *w, unsigned i, unsigned y,
+                             unsigned width, bool selected, void *data);
+
 bool
 browser_cmd(struct screen_browser *browser,
            struct mpdclient *c, command_t cmd)
@@ -406,7 +412,7 @@ browser_cmd(struct screen_browser *browser,
                return true;
        case CMD_LIST_JUMP:
                screen_jump(browser->lw, browser_lw_callback,
-                           NULL, browser->filelist);
+                           screen_browser_paint_callback, browser->filelist);
                return true;
 
 #ifdef HAVE_GETMOUSE
@@ -479,8 +485,107 @@ browser_cmd(struct screen_browser *browser,
        return false;
 }
 
+static void
+screen_browser_paint_directory(WINDOW *w, unsigned width,
+                              bool selected, const char *name)
+{
+       row_color(w, COLOR_DIRECTORY, selected);
+
+       waddch(w, '[');
+       waddstr(w, name);
+       waddch(w, ']');
+
+       /* erase the unused space after the text */
+       row_clear_to_eol(w, width, selected);
+}
+
+static void
+screen_browser_paint_song(WINDOW *w, unsigned width, bool selected,
+                         bool highlight, const struct mpd_song *song)
+{
+       char buffer[width * 4];
+
+       strfsong(buffer, sizeof(buffer), options.list_format, song);
+       row_paint_text(w, width, highlight ? COLOR_LIST_BOLD : COLOR_LIST,
+                      selected, buffer);
+}
+
+static void
+screen_browser_paint_playlist(WINDOW *w, unsigned width,
+                             bool selected, const char *name)
+{
+       row_paint_text(w, width, COLOR_PLAYLIST, selected, name);
+}
+
+static void
+screen_browser_paint_callback(WINDOW *w, unsigned i,
+                             G_GNUC_UNUSED unsigned y, unsigned width,
+                             bool selected, void *data)
+{
+       const struct filelist *fl = (const struct filelist *) data;
+       const struct filelist_entry *entry;
+       const struct mpd_entity *entity;
+       bool highlight;
+       const struct mpd_directory *directory;
+       const struct mpd_playlist *playlist;
+       char *p;
+
+       assert(fl != NULL);
+       assert(i < filelist_length(fl));
+
+       entry = filelist_get(fl, i);
+       assert(entry != NULL);
+
+       entity = entry->entity;
+       if (entity == NULL) {
+               screen_browser_paint_directory(w, width, selected, "..");
+               return;
+       }
+
+#ifndef NCMPC_MINI
+       highlight = (entry->flags & HIGHLIGHT) != 0;
+#else
+       highlight = false;
+#endif
+
+       if (highlight)
+               colors_use(w, COLOR_LIST_BOLD);
+       else
+               colors_use(w, COLOR_LIST);
+
+       switch (mpd_entity_get_type(entity)) {
+       case MPD_ENTITY_TYPE_DIRECTORY:
+               directory = mpd_entity_get_directory(entity);
+               p = utf8_to_locale(g_basename(mpd_directory_get_path(directory)));
+               screen_browser_paint_directory(w, width, selected, p);
+               g_free(p);
+               break;
+
+       case MPD_ENTITY_TYPE_SONG:
+               screen_browser_paint_song(w, width, selected, highlight,
+                                         mpd_entity_get_song(entity));
+               break;
+
+       case MPD_ENTITY_TYPE_PLAYLIST:
+               playlist = mpd_entity_get_playlist(entity);
+               p = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
+               screen_browser_paint_playlist(w, width, selected, p);
+               g_free(p);
+               break;
+
+       default:
+               waddstr(w, "<unknown>");
+       }
+
+       whline(w, ' ', width);
+
+       if (selected)
+               wattroff(w, A_REVERSE);
+}
+
 void
 screen_browser_paint(const struct screen_browser *browser)
 {
-       list_window_paint(browser->lw, browser_lw_callback, browser->filelist);
+       list_window_paint2(browser->lw, screen_browser_paint_callback,
+                          browser->filelist);
 }