From 2cf7354d41c1f29ae6f7ad1625ea9925006c480e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 11 Oct 2009 14:55:23 +0200 Subject: [PATCH] screen_browser: added custom list painting function Side effect: playlists and directories are rendered with different colors (configuration "browser-directory" and "browser-playlist"). --- NEWS | 1 + doc/config.sample | 6 +++ src/colors.c | 10 ++++ src/colors.h | 2 + src/screen_browser.c | 109 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 126 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 4ce914c..15ed5c3 100644 --- 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 diff --git a/doc/config.sample b/doc/config.sample index e064ebb..9bd61c7 100644 --- a/doc/config.sample +++ b/doc/config.sample @@ -135,6 +135,12 @@ ## 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 diff --git a/src/colors.c b/src/colors.c index 14c7cc7..a6c7e2c 100644 --- a/src/colors.c +++ b/src/colors.c @@ -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 diff --git a/src/colors.h b/src/colors.h index 305e396..d317760 100644 --- a/src/colors.h +++ b/src/colors.h @@ -40,6 +40,8 @@ enum color { COLOR_STATUS_BOLD, COLOR_STATUS_TIME, COLOR_STATUS_ALERT, + COLOR_DIRECTORY, + COLOR_PLAYLIST, COLOR_END }; diff --git a/src/screen_browser.c b/src/screen_browser.c index 3b0fc00..16a2f5e 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -30,6 +30,8 @@ #include "strfsong.h" #include "mpdclient.h" #include "filelist.h" +#include "colors.h" +#include "paint.h" #include @@ -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, ""); + } + + 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); } -- 2.30.2