Make object key color configurable (close #1739, #1791, #2638)

Co-authored-by: David Haguenauer <ml@kurokatta.org>
Co-authored-by: Eric Pruitt <eric.pruitt@gmail.com>
This commit is contained in:
authored and Nico Williams committed 2023-07-18 17:53:29 -05:00
1 parent 62d65a8a9c
commit 0b558f6ed4
4 files changed
+55 -32

No files matched your search

+2 -1
View File
@@ -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
Generated
+4 -1
View File
@@ -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:
+7 -14
View File
@@ -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<sizeof(color_kinds)/sizeof(color_kinds[0]); i++) {
if (jv_get_kind(x) == color_kinds[i]) {
color = colors[i];
if ((flags & JV_PRINT_COLOR) && jv_get_kind(x) != JV_KIND_INVALID) {
color = colors[(int)jv_get_kind(x) - 1];
put_str(color, F, S, flags & JV_PRINT_ISATTY);
break;
}
}
}
if (indent > MAX_PRINT_DEPTH) {
put_str("<skipped: too deep>", F, S, flags & JV_PRINT_ISATTY);
+42 -16
View File
@@ -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