Add JQ_COLORS env var for color config (fix #1252)
This commit is contained in:
1 parent
32d8f2000a
commit
6d89e297fe
6 files changed
+221
-1
No files matched your search
@@ -66,4 +66,6 @@ jv jq_util_input_get_position(jq_state*);
|
||||
jv jq_util_input_get_current_filename(jq_state*);
|
||||
jv jq_util_input_get_current_line(jq_state*);
|
||||
|
||||
int jq_set_colors(const char *);
|
||||
|
||||
#endif /* !JQ_H */
|
||||
+36
-1
@@ -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);
|
||||
|
||||
@@ -525,6 +525,9 @@ int main(int argc, char* argv[]) {
|
||||
if (options & COLOR_OUTPUT) dumpopts |= JV_PRINT_COLOR;
|
||||
if (options & NO_COLOR_OUTPUT) dumpopts &= ~JV_PRINT_COLOR;
|
||||
|
||||
if (getenv("JQ_COLORS") != NULL && !jq_set_colors(getenv("JQ_COLORS")))
|
||||
fprintf(stderr, "Failed to set $JQ_COLORS\n");
|
||||
|
||||
if (jv_get_kind(lib_search_paths) == JV_KIND_NULL) {
|
||||
// Default search path list
|
||||
lib_search_paths = JV_ARRAY(jv_string("~/.jq"),
|
||||
|
||||
Reference in new issue
Block a user