diff options
author | Karel Zak <kzak@redhat.com> | 2014-02-10 14:50:12 +0100 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2014-02-10 14:50:12 +0100 |
commit | 68f7b572bac36d4d1777e34e5b7a1107255b1e34 (patch) | |
tree | 1417884d322410e5e50dfa3464e307d30c13cd6f | |
parent | 5c6be570767091ff7843c90ffb2fb470e0876c03 (diff) | |
download | util-linux-68f7b572bac36d4d1777e34e5b7a1107255b1e34.tar.gz |
lib/color: add colorscheme parser
-rw-r--r-- | include/colors.h | 1 | ||||
-rw-r--r-- | lib/colors.c | 49 |
2 files changed, 48 insertions, 2 deletions
diff --git a/include/colors.h b/include/colors.h index d2bd7b2a..16db0107 100644 --- a/include/colors.h +++ b/include/colors.h @@ -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 */ diff --git a/lib/colors.c b/lib/colors.c index 51faa010..a197823b 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -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; |