summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 43dfcc8)
raw | patch | inline | side by side (parent: 43dfcc8)
author | Max Kellermann <max@duempel.org> | |
Fri, 3 Oct 2008 12:24:28 +0000 (14:24 +0200) | ||
committer | Max Kellermann <max@duempel.org> | |
Fri, 3 Oct 2008 12:24:28 +0000 (14:24 +0200) |
The color ids are sequential, and we can save some bytes if we use it
for the array index.
for the array index.
src/colors.c | patch | blob | history | |
src/colors.h | patch | blob | history |
diff --git a/src/colors.c b/src/colors.c
index 690b9f20dc529d442a195484dc3b7350f80d444f..68c5b600753998d821a4209683cb1f0e08e2d1c9 100644 (file)
--- a/src/colors.c
+++ b/src/colors.c
#include "i18n.h"
#include "options.h"
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
} 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 */
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;
}
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;
fg = entry->fg;
}
- init_pair(entry->id, fg, bg);
+ init_pair(id, fg, bg);
return 0;
}
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"));
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 df03355611a4ba8c79cfb266b64105365daac093..b612a33ce0ba93a8b5a8ee4573a574ab899a5dd9 100644 (file)
--- a/src/colors.h
+++ b/src/colors.h
COLOR_STATUS_BOLD,
COLOR_STATUS_TIME,
COLOR_STATUS_ALERT,
+ COLOR_END
};
short colors_str2color(const char *str);