diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index e6d4e57a1f..70d380a349 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -3264,7 +3264,7 @@ sections: body: | To configure alternative colors just set the `JQ_COLORS` - environment variable to colon-delimited list of partial terminal + environment variable to colon-separated list of partial terminal escape sequences like `"1;31"`, in this order: - color for `null` @@ -3274,9 +3274,10 @@ sections: - color for strings - color for arrays - color for objects + - color for fields (e.g., object keys) The default color scheme is the same as setting - `"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39"`. + `"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39:1;34"`. This is not a manual for VT100/ANSI escapes. However, each of these color specifications should consist of two numbers separated diff --git a/src/jv.h b/src/jv.h index d111c80b29..8994e4e688 100644 --- a/src/jv.h +++ b/src/jv.h @@ -13,7 +13,8 @@ typedef enum { JV_KIND_NUMBER, JV_KIND_STRING, JV_KIND_ARRAY, - JV_KIND_OBJECT + JV_KIND_OBJECT, + JV_KIND_FIELD, } jv_kind; struct jv_refcnt; diff --git a/src/jv_print.c b/src/jv_print.c index 5ebc01e628..19a784d66d 100644 --- a/src/jv_print.c +++ b/src/jv_print.c @@ -26,13 +26,13 @@ // for how to choose these. 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}; + JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT, JV_KIND_FIELD}; 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") + COL("0;32"), COL("1;39"), COL("1;39"), COL("1;34")}; +#define FIELD_COLOR (colors[JV_KIND_FIELD - 1]) static const char **colors = def_colors; diff --git a/tests/shtest b/tests/shtest index fc2ef15230..fdcd1abc3d 100755 --- a/tests/shtest +++ b/tests/shtest @@ -274,20 +274,44 @@ if [ "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1)" != xyz ]; then fi # Check $JQ_COLORS +## Default colors, null input $JQ -Ccn . > $d/color printf '\033[1;30mnull\033[0m\n' > $d/expect cmp $d/color $d/expect + +## Set non-default color, null input JQ_COLORS='4;31' $JQ -Ccn . > $d/color printf '\033[4;31mnull\033[0m\n' > $d/expect cmp $d/color $d/expect -JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \ + +## Default colors, complex input +$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color +( +printf '\033[1;39m[\033[1;39m{' +printf '\033[0m\033[1;34m"a"\033[' +printf '0m\033[1;39m:\033[0m\033[' +printf '0;39mtrue\033[0m\033[1' +printf ';39m,\033[0m\033[1;34m' +printf '"b"\033[0m\033[1;39m:\033' +printf '[0m\033[0;39mfalse\033' +printf '[0m\033[1;39m\033[1;39' +printf 'm}\033[0m\033[1;39m,\033[' +printf '0;39m123\033[0m\033[1;' +printf '39m,\033[1;30mnull\033' +printf '[0m\033[1;39m\033[1;39' +printf 'm]\033[0m\n' +) > $d/expect +cmp -b $d/color $d/expect + +## Set non-default colors, complex input +JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36:1;30' \ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color ( printf '\033[1;35m[\033[1;36m{' -printf '\033[0m\033[34;1m"a"\033[' +printf '\033[0m\033[1;30m"a"\033[' printf '0m\033[1;36m:\033[0m\033[' printf '0;32mtrue\033[0m\033[1' -printf ';36m,\033[0m\033[34;1m' +printf ';36m,\033[0m\033[1;30m' printf '"b"\033[0m\033[1;36m:\033' printf '[0m\033[0;31mfalse\033' printf '[0m\033[1;36m\033[1;36'