]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/color: add colorscheme parser
authorKarel Zak <kzak@redhat.com>
Mon, 10 Feb 2014 13:50:12 +0000 (14:50 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Feb 2014 13:50:12 +0000 (14:50 +0100)
include/colors.h
lib/colors.c

index d2bd7b2aa56318e482c4721c882971cef6c57430..16db01075dc3a93565523c66e0f0ad199e4cd07f 100644 (file)
@@ -72,6 +72,7 @@ static inline void color_disable(void)
        color_fdisable(stdout);
 }
 
+extern const char *colorscheme_from_string(const char *str);
 
 
 #endif /* UTIL_LINUX_COLORS_H */
index 51faa010ad88bfa3ade4bc8b3e182c5e79649307..a197823b46c24f24bd5fbabc33fab030cd0a77e5 100644 (file)
@@ -78,6 +78,46 @@ int colormode_or_err(const char *str, const char *errmsg)
        return colormode;
 }
 
+struct colorscheme {
+       const char *name, *scheme;
+};
+
+static int cmp_colorscheme_name(const void *a0, const void *b0)
+{
+       struct colorscheme *a = (struct colorscheme *) a0,
+                          *b = (struct colorscheme *) b0;
+       return strcmp(a->name, b->name);
+}
+
+const char *colorscheme_from_string(const char *str)
+{
+       static const struct colorscheme basic_schemes[] = {
+               { "black",      UL_COLOR_BLACK           },
+               { "blue",       UL_COLOR_BLUE            },
+               { "brown",      UL_COLOR_BROWN           },
+               { "cyan",       UL_COLOR_CYAN            },
+               { "darkgray",   UL_COLOR_DARK_GRAY       },
+               { "gray",       UL_COLOR_GRAY            },
+               { "green",      UL_COLOR_GREEN           },
+               { "lightblue",  UL_COLOR_BOLD_BLUE       },
+               { "lightcyan",  UL_COLOR_BOLD_CYAN       },
+               { "lightgray,", UL_COLOR_GRAY            },
+               { "lightgreen", UL_COLOR_BOLD_GREEN      },
+               { "lightmagenta", UL_COLOR_BOLD_MAGENTA  },
+               { "lightred",   UL_COLOR_BOLD_RED        },
+               { "magenta",    UL_COLOR_MAGENTA         },
+               { "red",        UL_COLOR_RED             },
+               { "yellow",     UL_COLOR_BOLD_YELLOW     },
+       };
+       struct colorscheme key = { .name = str }, *res;
+       if (!str)
+               return NULL;
+
+       res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
+                               sizeof(struct colorscheme),
+                               cmp_colorscheme_name);
+       return res ? res->scheme : NULL;
+}
 
 #ifdef TEST_PROGRAM
 # include <getopt.h>
@@ -85,22 +125,27 @@ int main(int argc, char *argv[])
 {
        static const struct option longopts[] = {
                { "colors", optional_argument, 0, 'c' },
+               { "scheme", required_argument, 0, 's' },
                { NULL, 0, 0, 0 }
        };
        int c, mode = UL_COLORMODE_NEVER;       /* default */
+       const char *scheme = "red";
 
-       while ((c = getopt_long(argc, argv, "c::", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "c::s:", longopts, NULL)) != -1) {
                switch (c) {
                case 'c':
                        mode = UL_COLORMODE_AUTO;
                        if (optarg)
                                mode = colormode_or_err(optarg, "unsupported color mode");
                        break;
+               case 's':
+                       scheme = optarg;
+                       break;
                }
        }
 
        colors_init(mode);
-       color_enable(UL_COLOR_RED);
+       color_enable(colorscheme_from_string(scheme));
        printf("Hello World!");
        color_disable();
        return EXIT_SUCCESS;