From 35958d0c61cff969506a4ab1a15e1e28c7a60f77 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 24 Mar 2014 13:40:05 +0100 Subject: [PATCH] libsmartcols: colors may now be specified as human-readable strings Signed-off-by: Ondrej Oprala --- libsmartcols/src/cell.c | 7 +++++++ libsmartcols/src/column.c | 7 +++++++ libsmartcols/src/line.c | 7 +++++++ libsmartcols/src/test.c | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index 5e3ea6081b..fb523f0137 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "smartcolsP.h" @@ -85,6 +86,12 @@ int scols_cell_set_color(struct libscols_cell *ce, const char *color) if (!ce) return -EINVAL; if (color) { + if (isalnum(*color)) { + color = colorscheme_from_string(color); + + if (!color) + return -EINVAL; + } p = strdup(color); if (!p) return -ENOMEM; diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index 5fad5319fc..29eb636664 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "smartcolsP.h" @@ -118,6 +119,12 @@ int scols_column_set_color(struct libscols_column *cl, const char *color) if (!cl) return -EINVAL; if (color) { + if (isalnum(*color)) { + color = colorscheme_from_string(color); + + if (!color) + return -EINVAL; + } p = strdup(color); if (!p) return -ENOMEM; diff --git a/libsmartcols/src/line.c b/libsmartcols/src/line.c index b982fba6d8..ec5add3fb9 100644 --- a/libsmartcols/src/line.c +++ b/libsmartcols/src/line.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "smartcolsP.h" @@ -196,6 +197,12 @@ int scols_line_set_color(struct libscols_line *ln, const char *color) if (!ln) return -EINVAL; if (color) { + if (isalnum(*color)) { + color = colorscheme_from_string(color); + + if (!color) + return -EINVAL; + } p = strdup(color); if (!p) return -ENOMEM; diff --git a/libsmartcols/src/test.c b/libsmartcols/src/test.c index 547a5fd003..0899cea311 100644 --- a/libsmartcols/src/test.c +++ b/libsmartcols/src/test.c @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) struct libscols_cell *h = scols_column_get_header(cl); scols_column_set_color(cl, UL_COLOR_BOLD_GREEN); - scols_cell_set_color(h, UL_COLOR_GREEN); + scols_cell_set_color(h, "green"); /* a human-readable string is also legal */ } scols_table_new_column(tb, "BAR", 0.3); scols_table_new_column(tb, "PATH", 0.3); -- 2.47.3