From 2c4fc4eaf97bbcf9bb86a46a1089079d0cadeced Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Tue, 24 Nov 2009 19:57:06 +0000 Subject: [PATCH] colors: Change config to use attribute list Colors can now be specified with a comma separated list of attributes, for example: `blue,bold,underline'. Putting multiple colors in the list is probably a bad idea.. Existing `bright' colors should be converted to bold colors. e.g. brightblue => blue,bold --- NEWS | 1 + doc/config.sample | 23 ++++----- doc/ncmpc.1 | 26 +++++----- src/colors.c | 119 +++++++++++++++++++++++++++------------------- 4 files changed, 93 insertions(+), 76 deletions(-) diff --git a/NEWS b/NEWS index c79ec76..588bf5e 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ ncmpc 0.16 - not yet released * status_bar: fix display_remaining_time while seeking * status_bar: display elapsed time event when total time is unknown * charset: added support for zero-width characters +* colors: added support for 256 colors and text attributes ncmpc 0.15 - 2009-09-24 diff --git a/doc/config.sample b/doc/config.sample index 7dcfaea..75ab0f1 100644 --- a/doc/config.sample +++ b/doc/config.sample @@ -105,38 +105,35 @@ #second-column = yes ############## Colors ####################### +## colors: none, black, red, green, yellow, blue, magenta, cyan, white +## attributes: standout, underline, reverse, blink, dim, bold ## -## base colors: black, red, green, yellow, blue, magenta, cyan, white -## +## Colors can also be given as an integer representing a terminal specific +## color code. The special color, none, represents the terminals default color. ## Enable/disable colors. #enable-colors = no -## Set the background color. If the background color is assigned -## to the keyword none, ncmpc will not change the background color. +## Set the background color. #color background = black -## text colors: black, red, green, yellow, blue, magenta, cyan, white, -## brightblack, brightred, brightgreen, brightyellow, brightblue, -## brightmagenta, brightcyan, brightwhite - ## Set the text color for the title row. #color title = yellow ## Set the text color for the title row (the bold part). -#color title-bold = brightyellow +#color title-bold = yellow,bold ## Set the color of the line on the second row. #color line = white ## Set the text color used to indicate mpd flags on the second row. -#color line-flags = brightwhite +#color line-flags = white,bold ## Set the text color in the main area of ncmpc. #color list = green ## Set the bold text color in the main area of ncmpc. -#color list-bold = brightgreen +#color list-bold = green,bold ## Sets the text color of directories in the browser #color browser-directory = yellow @@ -148,7 +145,7 @@ #color progressbar = white ## Set the text color used to display mpd status in the status window. -#color status-state = brightyellow +#color status-state = yellow,bold ## Set the text color used to display song names in the status window. #color status-song = yellow @@ -157,7 +154,7 @@ #color status-time = red ## Text color used to display alerts in the status window. -#color alert = brightred +#color alert = red,bold ## Redefine any of the base colors. ## The RGB values must be an integer value between 0 and 1000. diff --git a/doc/ncmpc.1 b/doc/ncmpc.1 index e09a65c..7c4e8df 100644 --- a/doc/ncmpc.1 +++ b/doc/ncmpc.1 @@ -176,39 +176,39 @@ The format used to for the xterm title when ncmpc is playing. Enable/disable colors. .TP .B color background = COLOR -Set the background color. If the background color is assigned to the keyword \fBnone\fR, ncmpc will not change the background color. Valid background colors are the base colors: black, red, green, yellow, blue, magenta, cyan and white. +Set the background color. If the background color is assigned to the keyword \fBnone\fR, ncmpc will not change the background color. Standard colors are: black, red, green, yellow, blue, magenta, cyan and white. Terminal specific colors can also be given as integers. .TP -.B color title = TEXTCOLOR -Set the text color for the title row. Possible text colors are the base colors plus grey, brightred, brightgreen, brightyellow, brightblue, brightmagenta, brightcyan and brightwhite. +.B color title = COLOR[,ATTRIBUTE]... +Set the text color and attributes for the title row. Text colors are the same as for the background. Valid attributes are: standout, underline, reverse, blink, dim, and bold. .TP -.B color title\-bold = TEXTCOLOR +.B color title\-bold = COLOR[,ATTRIBUTE]... Set the text color for the title row (the bold part). .TP -.B color line = TEXTCOLOR +.B color line = COLOR Set the color of the line on the second row. .TP -.B color line\-flags = TEXTCOLOR +.B color line\-flags = COLOR[,ATTRIBUTE]... Set the text color used to indicate mpd flags on the second row. .TP -.B color list = TEXTCOLOR +.B color list = COLOR[,ATTRIBUTE]... Set the text color in the main area of ncmpc. .TP -.B color list\-bold = TEXTCOLOR +.B color list\-bold = COLOR[,ATTRIBUTE]... Set the bold text color in the main area of ncmpc. .TP -.B color progressbar = TEXTCOLOR +.B color progressbar = COLOR[,ATTRIBUTE]... Set the color of the progress indicator. .TP -.B color status\-state = TEXTCOLOR +.B color status\-state = COLOR[,ATTRIBUTE]... Set the text color used to display mpd status in the status window. .TP -.B color status\-song = TEXTCOLOR +.B color status\-song = COLOR[,ATTRIBUTE]... Set the text color used to display song names in the status window. .TP -.B color status\-time = TEXTCOLOR +.B color status\-time = COLOR[,ATTRIBUTE]... Set the text color used to display time the status window. .TP -.B color alert = TEXTCOLOR +.B color alert = COLOR[,ATTRIBUTE]... Text color used to display alerts in the status window. .TP .B colordef COLOR = R, G, B diff --git a/src/colors.c b/src/colors.c index 2635329..dfaa84f 100644 --- a/src/colors.c +++ b/src/colors.c @@ -29,7 +29,7 @@ #include #include -#define COLOR_NONE -1 +#define COLOR_NONE G_MININT /* left most bit only */ #define COLOR_ERROR -2 #ifdef ENABLE_COLORS @@ -82,66 +82,85 @@ colors_lookup_by_name(const char *name) static int colors_update_pair(enum color id) { - short fg, bg; + int fg, bg; assert(id > 0 && id < COLOR_END); fg = colors[id].color; bg = colors[COLOR_BACKGROUND].color; - /* COLOR_NONE is negative, which - * results in a default colors */ - init_pair(id, fg, bg); + /* If color == COLOR_NONE (negative), + * pass -1 to avoid cast errors */ + init_pair(id, + (fg < 0 ? -1 : fg), + (bg < 0 ? -1 : bg)); return 0; } int colors_str2color(const char *str) { - int color; - char *endptr; - - if (!strcasecmp(str, "black")) - return COLOR_BLACK; - else if (!strcasecmp(str, "red")) - return COLOR_RED; - else if (!strcasecmp(str, "green")) - return COLOR_GREEN; - else if (!strcasecmp(str, "yellow")) - return COLOR_YELLOW; - else if (!strcasecmp(str, "blue")) - return COLOR_BLUE; - else if (!strcasecmp(str, "magenta")) - return COLOR_MAGENTA; - else if (!strcasecmp(str, "cyan")) - return COLOR_CYAN; - else if (!strcasecmp(str, "white")) - return COLOR_WHITE; - else if (!strcasecmp(str, "brightred")) - return COLOR_RED | A_BOLD; - else if (!strcasecmp(str, "brightgreen")) - return COLOR_GREEN | A_BOLD; - else if (!strcasecmp(str, "brightyellow")) - return COLOR_YELLOW | A_BOLD; - else if (!strcasecmp(str, "brightblue")) - return COLOR_BLUE | A_BOLD; - else if (!strcasecmp(str, "brightmagenta")) - return COLOR_MAGENTA | A_BOLD; - else if (!strcasecmp(str, "brightcyan")) - return COLOR_CYAN | A_BOLD; - else if (!strcasecmp(str, "brightwhite")) - return COLOR_WHITE | A_BOLD; - else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray")) - return COLOR_BLACK | A_BOLD; - else if (!strcasecmp(str, "none")) - return COLOR_NONE; - - color = strtol(str, &endptr, 10); - if (str != endptr && endptr[0] == '\0') - return color; + int i, color = 0; + char **parts = g_strsplit(str, ",", 0); + for (i = 0; parts[i]; i++) { + char *cur = parts[i]; + + /* Legacy colors (brightblue,etc) */ + if (!strncasecmp(cur, "bright", 6)) { + color |= A_BOLD; + cur += 6; + } - fprintf(stderr,_("Warning: Unknown color - %s\n"), str); - return -2; + /* Colors */ + if (!strcasecmp(cur, "none")) + color |= COLOR_NONE; + else if (!strcasecmp(cur, "black")) + color |= COLOR_BLACK; + else if (!strcasecmp(cur, "red")) + color |= COLOR_RED; + else if (!strcasecmp(cur, "green")) + color |= COLOR_GREEN; + else if (!strcasecmp(cur, "yellow")) + color |= COLOR_YELLOW; + else if (!strcasecmp(cur, "blue")) + color |= COLOR_BLUE; + else if (!strcasecmp(cur, "magenta")) + color |= COLOR_MAGENTA; + else if (!strcasecmp(cur, "cyan")) + color |= COLOR_CYAN; + else if (!strcasecmp(cur, "white")) + color |= COLOR_WHITE; + else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray")) + color |= COLOR_BLACK | A_BOLD; + + /* Attributes */ + else if (!strcasecmp(cur, "standout")) + color |= A_STANDOUT; + else if (!strcasecmp(cur, "underline")) + color |= A_UNDERLINE; + else if (!strcasecmp(cur, "reverse")) + color |= A_REVERSE; + else if (!strcasecmp(cur, "blink")) + color |= A_BLINK; + else if (!strcasecmp(cur, "dim")) + color |= A_DIM; + else if (!strcasecmp(cur, "bold")) + color |= A_BOLD; + else { + /* Numerical colors */ + char *endptr; + int tmp = strtol(cur, &endptr, 10); + if (cur != endptr && endptr[0] == '\0') { + color |= tmp; + } else { + fprintf(stderr,_("Warning: Unknown color - %s\n"), str); + return COLOR_ERROR; + } + } + + } + g_strfreev(parts); + return color; } /* This function is called from conf.c before curses have been started, @@ -253,8 +272,8 @@ colors_use(WINDOW *w, enum color id) #ifdef ENABLE_COLORS if (options.enable_colors) { /* color mode */ - if ((int)attrs != entry->mono || (short)id != pair) - wattr_set(w, entry->mono, id, NULL); + if ((int)attrs != entry->color || (short)id != pair) + wattr_set(w, entry->color, id, NULL); } else { #endif /* mono mode */ -- 2.30.2