From: Max Kellermann Date: Fri, 3 Oct 2008 12:24:28 +0000 (+0200) Subject: colors: color id is the index of the "colors" array X-Git-Tag: v0.12_alpha1~87 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7a12f9e8754a10634fa43732712dc60b87fa2c47;p=ncmpc.git 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. --- diff --git a/src/colors.c b/src/colors.c index 690b9f2..68c5b60 100644 --- a/src/colors.c +++ b/src/colors.c @@ -20,6 +20,7 @@ #include "i18n.h" #include "options.h" +#include #include #include #include @@ -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 */ @@ -85,30 +84,14 @@ 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); diff --git a/src/colors.h b/src/colors.h index df03355..b612a33 100644 --- a/src/colors.h +++ b/src/colors.h @@ -15,6 +15,7 @@ enum color { COLOR_STATUS_BOLD, COLOR_STATUS_TIME, COLOR_STATUS_ALERT, + COLOR_END }; short colors_str2color(const char *str);