Code

configure.ac: disable NLS in mini mode
[ncmpc.git] / src / colors.c
index 507be48cac88d547dce892a499c50874ccbd68c1..cc3ec50615ff7e09d312ba4b7759feb2c6ff787d 100644 (file)
 
 #include "colors.h"
 #include "i18n.h"
+#ifdef ENABLE_COLORS
 #include "options.h"
+#endif
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #define NAME_ALERT "alert"
 #define NAME_BGCOLOR "background"
 
+#ifdef ENABLE_COLORS
 typedef struct {
        short color;
        short r,g,b;
 } color_definition_entry_t;
+#endif
 
 typedef struct {
-       int 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 },
 };
 
+#ifdef ENABLE_COLORS
+
 /* background color */
 static short bg = COLOR_BLACK;
 
 static GList *color_definition_list = NULL;
 
-static color_entry_t *
-colors_lookup(int 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(int id)
+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 +118,7 @@ colors_update_pair(int id)
                fg = entry->fg;
        }
 
-       init_pair(entry->id, fg, bg);
+       init_pair(id, fg, bg);
        return 0;
 }
 
@@ -226,6 +214,7 @@ colors_assign(const char *name, const char *value)
        return 0;
 }
 
+
 int
 colors_start(void)
 {
@@ -246,19 +235,17 @@ colors_start(void)
                                list = list->next;
                        }
                } else if (color_definition_list && !can_change_color())
-                       fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
+                       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"));
+               fprintf(stderr, _("Terminal lacks color capabilities\n"));
                options.enable_colors = 0;
        }
 
@@ -277,28 +264,32 @@ colors_start(void)
 
        return 0;
 }
+#endif
 
 int
-colors_use(WINDOW *w, int id)
+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);
 
+#ifdef ENABLE_COLORS
        if (options.enable_colors) {
                /* color mode */
-               if (attrs != entry->attrs || id != pair)
+               if (attrs != entry->attrs || (short)id != pair)
                        wattr_set(w, entry->attrs, id, NULL);
        } else {
+#endif
                /* mono mode */
                if (attrs != entry->attrs)
                        wattrset(w, entry->attrs);
+#ifdef ENABLE_COLORS
        }
+#endif
 
        return 0;
 }