Skip to content

Commit a3e8a10

Browse files
itchynyhaguenauericpruitt
committed
Make object key color configurable (close #1739, #1791, #2638)
Co-authored-by: David Haguenauer <[email protected]> Co-authored-by: Eric Pruitt <[email protected]>
1 parent 14e5e63 commit a3e8a10

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

docs/content/manual/manual.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3471,9 +3471,10 @@ sections:
34713471
- color for strings
34723472
- color for arrays
34733473
- color for objects
3474+
- color for object keys
34743475
34753476
The default color scheme is the same as setting
3476-
`"JQ_COLORS=1;30:0;37:0;37:0;37:0;32:1;37:1;37"`.
3477+
`JQ_COLORS="1;30:0;37:0;37:0;37:0;32:1;37:1;37:1;34"`.
34773478
34783479
This is not a manual for VT100/ANSI escapes. However, each of
34793480
these color specifications should consist of two numbers separated

jq.1.prebuilt

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jv_print.c

+8-15
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@
2525
#define COLRESET (ESC "[0m")
2626

2727
// Color table. See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
28-
// for how to choose these.
29-
static const jv_kind color_kinds[] =
30-
{JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER,
31-
JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT};
32-
static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16];
28+
// for how to choose these. The order is same as jv_kind definition, and
29+
// the last color is used for object keys.
30+
static char color_bufs[8][16];
3331
static const char *color_bufps[8];
3432
static const char* def_colors[] =
3533
{COL("1;30"), COL("0;37"), COL("0;37"), COL("0;37"),
36-
COL("0;32"), COL("1;37"), COL("1;37")};
37-
#define FIELD_COLOR COL("34;1")
34+
COL("0;32"), COL("1;37"), COL("1;37"), COL("1;34")};
35+
#define FIELD_COLOR (colors[7])
3836

3937
static const char **colors = def_colors;
4038

@@ -194,14 +192,9 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
194192
char buf[JVP_DTOA_FMT_MAX_LEN];
195193
const char* color = 0;
196194
double refcnt = (flags & JV_PRINT_REFCOUNT) ? jv_get_refcnt(x) - 1 : -1;
197-
if (flags & JV_PRINT_COLOR) {
198-
for (unsigned i=0; i<sizeof(color_kinds)/sizeof(color_kinds[0]); i++) {
199-
if (jv_get_kind(x) == color_kinds[i]) {
200-
color = colors[i];
201-
put_str(color, F, S, flags & JV_PRINT_ISATTY);
202-
break;
203-
}
204-
}
195+
if ((flags & JV_PRINT_COLOR) && jv_get_kind(x) != JV_KIND_INVALID) {
196+
color = colors[(int)jv_get_kind(x) - 1];
197+
put_str(color, F, S, flags & JV_PRINT_ISATTY);
205198
}
206199
if (indent > MAX_PRINT_DEPTH) {
207200
put_str("<skipped: too deep>", F, S, flags & JV_PRINT_ISATTY);

tests/shtest

+29-5
Original file line numberDiff line numberDiff line change
@@ -325,20 +325,44 @@ if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}
325325
fi
326326

327327
# Check $JQ_COLORS
328-
$JQ -Ccn . > $d/color
328+
## Default colors, null input
329+
JQ_COLORS= $JQ -Ccn . > $d/color
329330
printf '\033[1;30mnull\033[0m\n' > $d/expect
330331
cmp $d/color $d/expect
332+
333+
## Set non-default color, null input
331334
JQ_COLORS='4;31' $JQ -Ccn . > $d/color
332335
printf '\033[4;31mnull\033[0m\n' > $d/expect
333336
cmp $d/color $d/expect
334-
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \
337+
338+
## Default colors, complex input
339+
JQ_COLORS= $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
340+
(
341+
printf '\033[1;37m[\033[1;37m{'
342+
printf '\033[0m\033[1;34m"a"\033['
343+
printf '0m\033[1;37m:\033[0m\033['
344+
printf '0;37mtrue\033[0m\033[1'
345+
printf ';37m,\033[0m\033[1;34m'
346+
printf '"b"\033[0m\033[1;37m:\033'
347+
printf '[0m\033[0;37mfalse\033'
348+
printf '[0m\033[1;37m\033[1;37'
349+
printf 'm}\033[0m\033[1;37m,\033['
350+
printf '0;37m123\033[0m\033[1;'
351+
printf '37m,\033[1;30mnull\033'
352+
printf '[0m\033[1;37m\033[1;37'
353+
printf 'm]\033[0m\n'
354+
) > $d/expect
355+
cmp -b $d/color $d/expect
356+
357+
## Set non-default colors, complex input
358+
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36:1;30' \
335359
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
336360
(
337361
printf '\033[1;35m[\033[1;36m{'
338-
printf '\033[0m\033[34;1m"a"\033['
362+
printf '\033[0m\033[1;30m"a"\033['
339363
printf '0m\033[1;36m:\033[0m\033['
340364
printf '0;32mtrue\033[0m\033[1'
341-
printf ';36m,\033[0m\033[34;1m'
365+
printf ';36m,\033[0m\033[1;30m'
342366
printf '"b"\033[0m\033[1;36m:\033'
343367
printf '[0m\033[0;31mfalse\033'
344368
printf '[0m\033[1;36m\033[1;36'
@@ -356,7 +380,7 @@ cmp $d/color $d/expect
356380
# on stderr.
357381
set -vx
358382
echo 'Failed to set $JQ_COLORS' > $d/expect_warning
359-
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect
383+
JQ_COLORS= $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect
360384
JQ_COLORS='garbage;30:*;31:,;3^:0;$%:0;34:1;35:1;36' \
361385
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
362386
cmp $d/color $d/expect

0 commit comments

Comments
 (0)