diff options
author | Nico Williams <nico@cryptonector.com> | 2017-04-29 13:00:52 -0500 |
---|---|---|
committer | William Langford <wlangfor@gmail.com> | 2017-04-29 14:00:52 -0400 |
commit | 6d89e297febdbcbad4ecf201e56fc8ec99f67137 (patch) | |
tree | cc70c53bf330c65c8cabe10152c89ce9805c5e12 /src/jv_print.c | |
parent | 32d8f2000a80c3702b04f3423180ebfeaa503eb7 (diff) |
Add JQ_COLORS env var for color config (fix #1252)
Diffstat (limited to 'src/jv_print.c')
-rw-r--r-- | src/jv_print.c | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/jv_print.c b/src/jv_print.c index 3fcf8378..5ebc01e6 100644 --- a/src/jv_print.c +++ b/src/jv_print.c @@ -27,11 +27,46 @@ static const jv_kind color_kinds[] = {JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER, JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT}; -static const char* const colors[] = +static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16]; +static const char *color_bufps[8]; +static const char* def_colors[] = {COL("1;30"), COL("0;39"), COL("0;39"), COL("0;39"), COL("0;32"), COL("1;39"), COL("1;39")}; #define FIELD_COLOR COL("34;1") +static const char **colors = def_colors; + +int +jq_set_colors(const char *c) +{ + const char *e; + size_t i; + + if (c == NULL) + return 1; + colors = def_colors; + memset(color_bufs, 0, sizeof(color_bufs)); + for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]); i++) + color_bufps[i] = def_colors[i]; + for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]) && *c != '\0'; i++, c = e) { + if ((e = strchr(c, ':')) == NULL) + e = c + strlen(c); + if ((size_t)(e - c) > sizeof(color_bufs[i]) - 4 /* ESC [ m NUL */) + return 0; + color_bufs[i][0] = ESC[0]; + color_bufs[i][1] = '['; + (void) strncpy(&color_bufs[i][2], c, e - c); + if (strspn(&color_bufs[i][2], "0123456789;") < strlen(&color_bufs[i][2])) + return 0; + color_bufs[i][2 + (e - c)] = 'm'; + color_bufps[i] = color_bufs[i]; + if (e[0] == ':') + e++; + } + colors = color_bufps; + return 1; +} + static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) { if (strout) { *strout = jv_string_append_buf(*strout, s, len); |