summaryrefslogtreecommitdiffstats
path: root/lib/color-names.c
diff options
context:
space:
mode:
authorKarel Zak2015-07-24 12:57:46 +0200
committerKarel Zak2015-07-24 12:57:46 +0200
commit0bef6f759beb87d743c6dfb2a95d3e0b7075a462 (patch)
treee373aca99dedd1a648cbb08238085f69efde308f /lib/color-names.c
parenttests: add udevadm settle (diff)
downloadkernel-qcow2-util-linux-0bef6f759beb87d743c6dfb2a95d3e0b7075a462.tar.gz
kernel-qcow2-util-linux-0bef6f759beb87d743c6dfb2a95d3e0b7075a462.tar.xz
kernel-qcow2-util-linux-0bef6f759beb87d743c6dfb2a95d3e0b7075a462.zip
libsmartcols: don't link with tinfo
Let's move color names to sequence translation to separate file to make it usable without all the stuff in lib/colors.c. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/color-names.c')
-rw-r--r--lib/color-names.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/color-names.c b/lib/color-names.c
new file mode 100644
index 000000000..923b2f039
--- /dev/null
+++ b/lib/color-names.c
@@ -0,0 +1,52 @@
+
+#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)
+{
+ struct ul_color_name *a = (struct ul_color_name *) a0,
+ *b = (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 },
+ { "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 ul_color_name key = { .name = (char *) 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;
+}