aboutsummaryrefslogtreecommitdiffstats
path: root/src/colors.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-10-03 14:24:28 +0200
committerMax Kellermann <max@duempel.org>2008-10-03 14:24:28 +0200
commit7a12f9e8754a10634fa43732712dc60b87fa2c47 (patch)
treeeee8b02c060839674ec6346697dde9d020a5e75e /src/colors.c
parent43dfcc89822e46657db0d2bd52721e4ce852a357 (diff)
downloadmpd-7a12f9e8754a10634fa43732712dc60b87fa2c47.tar.gz
mpd-7a12f9e8754a10634fa43732712dc60b87fa2c47.tar.xz
mpd-7a12f9e8754a10634fa43732712dc60b87fa2c47.zip
colors: color id is the index of the "colors" array
The color ids are sequential, and we can save some bytes if we use it for the array index.
Diffstat (limited to 'src/colors.c')
-rw-r--r--src/colors.c69
1 files changed, 24 insertions, 45 deletions
diff --git a/src/colors.c b/src/colors.c
index 690b9f20d..68c5b6007 100644
--- a/src/colors.c
+++ b/src/colors.c
@@ -20,6 +20,7 @@
#include "i18n.h"
#include "options.h"
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -58,26 +59,24 @@ typedef struct {
} color_definition_entry_t;
typedef struct {
- enum color id;
const char *name;
short fg;
attr_t attrs;
} color_entry_t;
-static color_entry_t colors[] = {
- /* color pair, field name, color, mono attribute */
- { COLOR_TITLE, NAME_TITLE, COLOR_YELLOW, A_NORMAL },
- { COLOR_TITLE_BOLD, NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
- { COLOR_LINE, NAME_LINE, COLOR_WHITE, A_NORMAL },
- { COLOR_LINE_BOLD, NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD },
- { COLOR_LIST, NAME_LIST, COLOR_GREEN, A_NORMAL },
- { COLOR_LIST_BOLD, NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD },
- { COLOR_PROGRESSBAR, NAME_PROGRESS, COLOR_WHITE, A_NORMAL },
- { COLOR_STATUS, NAME_STATUS, COLOR_YELLOW, A_NORMAL },
- { 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 },
- { 0, NULL, 0, 0 }
+static color_entry_t colors[COLOR_END] = {
+ /* color pair = field name, color, mono attribute */
+ [COLOR_TITLE] = { NAME_TITLE, COLOR_YELLOW, A_NORMAL },
+ [COLOR_TITLE_BOLD] = { NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
+ [COLOR_LINE] = { NAME_LINE, COLOR_WHITE, A_NORMAL },
+ [COLOR_LINE_BOLD] = { NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD },
+ [COLOR_LIST] = { NAME_LIST, COLOR_GREEN, A_NORMAL },
+ [COLOR_LIST_BOLD] = { NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD },
+ [COLOR_PROGRESSBAR] = { NAME_PROGRESS, COLOR_WHITE, A_NORMAL },
+ [COLOR_STATUS] = { NAME_STATUS, COLOR_YELLOW, A_NORMAL },
+ [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 },
};
/* background color */
@@ -86,29 +85,13 @@ static short bg = COLOR_BLACK;
static GList *color_definition_list = NULL;
static color_entry_t *
-colors_lookup(enum color id)
-{
- int i = 0;
-
- while (colors[i].name != NULL) {
- if (colors[i].id == id)
- return &colors[i];
- i++;
- }
-
- return NULL;
-}
-
-static color_entry_t *
colors_lookup_by_name(const char *name)
{
- int i = 0;
+ enum color i;
- while (colors[i].name != NULL) {
+ for (i = 1; i < COLOR_END; ++i)
if (!strcasecmp(colors[i].name, name))
return &colors[i];
- i++;
- }
return NULL;
}
@@ -116,11 +99,10 @@ colors_lookup_by_name(const char *name)
static int
colors_update_pair(enum color id)
{
- color_entry_t *entry = colors_lookup(id);
+ color_entry_t *entry = &colors[id];
short fg = -1;
- if (!entry)
- return -1;
+ assert(id > 0 && id < COLOR_END);
if (IS_BRIGHT(entry->fg)) {
entry->attrs = A_BOLD;
@@ -130,7 +112,7 @@ colors_update_pair(enum color id)
fg = entry->fg;
}
- init_pair(entry->id, fg, bg);
+ init_pair(id, fg, bg);
return 0;
}
@@ -249,13 +231,11 @@ colors_start(void)
fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
if (options.enable_colors) {
- int i = 0;
+ enum color i;
- while (colors[i].name) {
+ for (i = 1; i < COLOR_END; ++i)
/* update the color pairs */
- colors_update_pair(colors[i].id);
- i++;
- }
+ colors_update_pair(i);
}
} else if (options.enable_colors) {
fprintf(stderr, _("Terminal lacks color capabilities!\n"));
@@ -281,12 +261,11 @@ colors_start(void)
int
colors_use(WINDOW *w, enum color id)
{
- color_entry_t *entry = colors_lookup(id);
+ color_entry_t *entry = &colors[id];
short pair;
attr_t attrs;
- if (!entry)
- return -1;
+ assert(id > 0 && id < COLOR_END);
wattr_get(w, &attrs, &pair, NULL);