]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/color-names.c
wall: make sure ut_line is not empty
[thirdparty/util-linux.git] / lib / color-names.c
1
2 #include "c.h"
3 #include "color-names.h"
4
5 struct ul_color_name {
6 const char *name;
7 const char *seq;
8 };
9
10 /*
11 * qsort/bsearch buddy
12 */
13 static int cmp_color_name(const void *a0, const void *b0)
14 {
15 const struct ul_color_name
16 *a = (const struct ul_color_name *) a0,
17 *b = (const struct ul_color_name *) b0;
18 return strcmp(a->name, b->name);
19 }
20
21 /*
22 * Maintains human readable color names
23 */
24 const char *color_sequence_from_colorname(const char *str)
25 {
26 static const struct ul_color_name basic_schemes[] = {
27 { "black", UL_COLOR_BLACK },
28 { "blink", UL_COLOR_BLINK },
29 { "blue", UL_COLOR_BLUE },
30 { "bold", UL_COLOR_BOLD },
31 { "brown", UL_COLOR_BROWN },
32 { "cyan", UL_COLOR_CYAN },
33 { "darkgray", UL_COLOR_DARK_GRAY },
34 { "gray", UL_COLOR_GRAY },
35 { "green", UL_COLOR_GREEN },
36 { "halfbright", UL_COLOR_HALFBRIGHT },
37 { "lightblue", UL_COLOR_BOLD_BLUE },
38 { "lightcyan", UL_COLOR_BOLD_CYAN },
39 { "lightgray,", UL_COLOR_GRAY },
40 { "lightgreen", UL_COLOR_BOLD_GREEN },
41 { "lightmagenta", UL_COLOR_BOLD_MAGENTA },
42 { "lightred", UL_COLOR_BOLD_RED },
43 { "magenta", UL_COLOR_MAGENTA },
44 { "red", UL_COLOR_RED },
45 { "reset", UL_COLOR_RESET, },
46 { "reverse", UL_COLOR_REVERSE },
47 { "yellow", UL_COLOR_BOLD_YELLOW },
48 };
49 struct ul_color_name key = { .name = str }, *res;
50
51 if (!str)
52 return NULL;
53
54 res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
55 sizeof(struct ul_color_name),
56 cmp_color_name);
57 return res ? res->seq : NULL;
58 }