diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 13:14:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 13:14:44 +0000 |
commit | 30ff6afe596eddafacf22b1a5b2d1a3d6254ea15 (patch) | |
tree | 9b788335f92174baf7ee18f03ca8330b8c19ce2b /lib/color-names.c | |
parent | Initial commit. (diff) | |
download | util-linux-upstream.tar.xz util-linux-upstream.zip |
Adding upstream version 2.36.1.upstream/2.36.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/color-names.c')
-rw-r--r-- | lib/color-names.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/color-names.c b/lib/color-names.c new file mode 100644 index 0000000..9b1505e --- /dev/null +++ b/lib/color-names.c @@ -0,0 +1,64 @@ +/* + * No copyright is claimed. This code is in the public domain; do with + * it what you wish. + * + * Written by Karel Zak <kzak@redhat.com> + */ +#include "c.h" +#include "color-names.h" + +struct ul_color_name { + const char *name; + const char *seq; +}; + +/* + * qsort/bsearch buddy + */ +static int cmp_color_name(const void *a0, const void *b0) +{ + const struct ul_color_name + *a = (const struct ul_color_name *) a0, + *b = (const struct ul_color_name *) b0; + return strcmp(a->name, b->name); +} + +/* + * Maintains human readable color names + */ +const char *color_sequence_from_colorname(const char *str) +{ + static const struct ul_color_name basic_schemes[] = { + { "black", UL_COLOR_BLACK }, + { "blink", UL_COLOR_BLINK }, + { "blue", UL_COLOR_BLUE }, + { "bold", UL_COLOR_BOLD }, + { "brown", UL_COLOR_BROWN }, + { "cyan", UL_COLOR_CYAN }, + { "darkgray", UL_COLOR_DARK_GRAY }, + { "gray", UL_COLOR_GRAY }, + { "green", UL_COLOR_GREEN }, + { "halfbright", UL_COLOR_HALFBRIGHT }, + { "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 }, + { "reset", UL_COLOR_RESET, }, + { "reverse", UL_COLOR_REVERSE }, + { "yellow", UL_COLOR_BOLD_YELLOW }, + { "white", UL_COLOR_WHITE } + }; + struct ul_color_name key = { .name = str }, *res; + + if (!str) + return NULL; + + res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes), + sizeof(struct ul_color_name), + cmp_color_name); + return res ? res->seq : NULL; +} |