From 0b558f6ed498717546406b5367483b976578a9b2 Mon Sep 17 00:00:00 2001 From: itchyny Date: Wed, 12 Jul 2023 09:31:26 +0900 Subject: [PATCH] Make object key color configurable (close #1739, #1791, #2638) Co-authored-by: David Haguenauer Co-authored-by: Eric Pruitt --- docs/content/manual/manual.yml | 3 +- jq.1.prebuilt | 5 ++- src/jv_print.c | 23 +++++--------- tests/shtest | 58 ++++++++++++++++++++++++---------- 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/docs/content/manual/manual.yml b/docs/content/manual/manual.yml index a951b9b2c7..2af18064d5 100644 --- a/docs/content/manual/manual.yml +++ b/docs/content/manual/manual.yml @@ -3485,9 +3485,10 @@ sections: - color for strings - color for arrays - color for objects + - color for object keys The default color scheme is the same as setting - `"JQ_COLORS=1;30:0;37:0;37:0;37:0;32:1;37:1;37"`. + `JQ_COLORS="1;30:0;37:0;37:0;37:0;32:1;37:1;37: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/jq.1.prebuilt b/jq.1.prebuilt index 3b09933236..3c63997d2e 100644 --- a/jq.1.prebuilt +++ b/jq.1.prebuilt @@ -3837,10 +3837,13 @@ color for arrays .IP "\(bu" 4 color for objects . +.IP "\(bu" 4 +color for object keys +. .IP "" 0 . .P -The default color scheme is the same as setting \fB"JQ_COLORS=1;30:0;37:0;37:0;37:0;32:1;37:1;37"\fR\. +The default color scheme is the same as setting \fBJQ_COLORS="1;30:0;37:0;37:0;37:0;32:1;37:1;37:1;34"\fR\. . .P This is not a manual for VT100/ANSI escapes\. However, each of these color specifications should consist of two numbers separated by a semi\-colon, where the first number is one of these: diff --git a/src/jv_print.c b/src/jv_print.c index 2e781bb8b4..d1db88aa89 100644 --- a/src/jv_print.c +++ b/src/jv_print.c @@ -25,16 +25,14 @@ #define COLRESET (ESC "[0m") // Color table. See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors -// 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}; -static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16]; +// for how to choose these. The order is same as jv_kind definition, and +// the last color is used for object keys. +static char color_bufs[8][16]; static const char *color_bufps[8]; static const char* def_colors[] = {COL("1;30"), COL("0;37"), COL("0;37"), COL("0;37"), - COL("0;32"), COL("1;37"), COL("1;37")}; -#define FIELD_COLOR COL("34;1") + COL("0;32"), COL("1;37"), COL("1;37"), COL("1;34")}; +#define FIELD_COLOR (colors[7]) static const char **colors = def_colors; @@ -194,14 +192,9 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI char buf[JVP_DTOA_FMT_MAX_LEN]; const char* color = 0; double refcnt = (flags & JV_PRINT_REFCOUNT) ? jv_get_refcnt(x) - 1 : -1; - if (flags & JV_PRINT_COLOR) { - for (unsigned i=0; i MAX_PRINT_DEPTH) { put_str("", F, S, flags & JV_PRINT_ISATTY); diff --git a/tests/shtest b/tests/shtest index a7fba4451d..0a0ddcf7cc 100755 --- a/tests/shtest +++ b/tests/shtest @@ -335,29 +335,55 @@ if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"} fi # Check $JQ_COLORS +unset 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;37m[\033[1;37m{' + printf '\033[0m\033[1;34m"a"\033[' + printf '0m\033[1;37m:\033[0m\033[' + printf '0;37mtrue\033[0m\033[1' + printf ';37m,\033[0m\033[1;34m' + printf '"b"\033[0m\033[1;37m:\033' + printf '[0m\033[0;37mfalse\033' + printf '[0m\033[1;37m\033[1;37' + printf 'm}\033[0m\033[1;37m,\033[' + printf '0;37m123\033[0m\033[1;' + printf '37m,\033[1;30mnull\033' + printf '[0m\033[1;37m\033[1;37' + printf 'm]\033[0m\n' +} > $d/expect +cmp $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 '0m\033[1;36m:\033[0m\033[' -printf '0;32mtrue\033[0m\033[1' -printf ';36m,\033[0m\033[34;1m' -printf '"b"\033[0m\033[1;36m:\033' -printf '[0m\033[0;31mfalse\033' -printf '[0m\033[1;36m\033[1;36' -printf 'm}\033[0m\033[1;35m,\033[' -printf '0;33m123\033[0m\033[1;' -printf '35m,\033[1;30mnull\033' -printf '[0m\033[1;35m\033[1;35' -printf 'm]\033[0m\n' -) > $d/expect +{ + printf '\033[1;35m[\033[1;36m{' + 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[1;30m' + printf '"b"\033[0m\033[1;36m:\033' + printf '[0m\033[0;31mfalse\033' + printf '[0m\033[1;36m\033[1;36' + printf 'm}\033[0m\033[1;35m,\033[' + printf '0;33m123\033[0m\033[1;' + printf '35m,\033[1;30mnull\033' + printf '[0m\033[1;35m\033[1;35' + printf 'm]\033[0m\n' +} > $d/expect cmp $d/color $d/expect # Check garbage in JQ_COLORS. We write each color sequence into a 16