Skip to content

Commit 0b558f6

Browse files
itchynyhaguenauericpruitt
authored andcommitted
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 62d65a8 commit 0b558f6

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

docs/content/manual/manual.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3485,9 +3485,10 @@ sections:
34853485
- color for strings
34863486
- color for arrays
34873487
- color for objects
3488+
- color for object keys
34883489
34893490
The default color scheme is the same as setting
3490-
`"JQ_COLORS=1;30:0;37:0;37:0;37:0;32:1;37:1;37"`.
3491+
`JQ_COLORS="1;30:0;37:0;37:0;37:0;32:1;37:1;37:1;34"`.
34913492
34923493
This is not a manual for VT100/ANSI escapes. However, each of
34933494
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

+42-16
Original file line numberDiff line numberDiff line change
@@ -335,29 +335,55 @@ if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}
335335
fi
336336

337337
# Check $JQ_COLORS
338+
unset JQ_COLORS
339+
340+
## Default colors, null input
338341
$JQ -Ccn . > $d/color
339342
printf '\033[1;30mnull\033[0m\n' > $d/expect
340343
cmp $d/color $d/expect
344+
345+
## Set non-default color, null input
341346
JQ_COLORS='4;31' $JQ -Ccn . > $d/color
342347
printf '\033[4;31mnull\033[0m\n' > $d/expect
343348
cmp $d/color $d/expect
344-
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \
349+
350+
## Default colors, complex input
351+
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
352+
{
353+
printf '\033[1;37m[\033[1;37m{'
354+
printf '\033[0m\033[1;34m"a"\033['
355+
printf '0m\033[1;37m:\033[0m\033['
356+
printf '0;37mtrue\033[0m\033[1'
357+
printf ';37m,\033[0m\033[1;34m'
358+
printf '"b"\033[0m\033[1;37m:\033'
359+
printf '[0m\033[0;37mfalse\033'
360+
printf '[0m\033[1;37m\033[1;37'
361+
printf 'm}\033[0m\033[1;37m,\033['
362+
printf '0;37m123\033[0m\033[1;'
363+
printf '37m,\033[1;30mnull\033'
364+
printf '[0m\033[1;37m\033[1;37'
365+
printf 'm]\033[0m\n'
366+
} > $d/expect
367+
cmp $d/color $d/expect
368+
369+
## Set non-default colors, complex input
370+
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36:1;30' \
345371
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
346-
(
347-
printf '\033[1;35m[\033[1;36m{'
348-
printf '\033[0m\033[34;1m"a"\033['
349-
printf '0m\033[1;36m:\033[0m\033['
350-
printf '0;32mtrue\033[0m\033[1'
351-
printf ';36m,\033[0m\033[34;1m'
352-
printf '"b"\033[0m\033[1;36m:\033'
353-
printf '[0m\033[0;31mfalse\033'
354-
printf '[0m\033[1;36m\033[1;36'
355-
printf 'm}\033[0m\033[1;35m,\033['
356-
printf '0;33m123\033[0m\033[1;'
357-
printf '35m,\033[1;30mnull\033'
358-
printf '[0m\033[1;35m\033[1;35'
359-
printf 'm]\033[0m\n'
360-
) > $d/expect
372+
{
373+
printf '\033[1;35m[\033[1;36m{'
374+
printf '\033[0m\033[1;30m"a"\033['
375+
printf '0m\033[1;36m:\033[0m\033['
376+
printf '0;32mtrue\033[0m\033[1'
377+
printf ';36m,\033[0m\033[1;30m'
378+
printf '"b"\033[0m\033[1;36m:\033'
379+
printf '[0m\033[0;31mfalse\033'
380+
printf '[0m\033[1;36m\033[1;36'
381+
printf 'm}\033[0m\033[1;35m,\033['
382+
printf '0;33m123\033[0m\033[1;'
383+
printf '35m,\033[1;30mnull\033'
384+
printf '[0m\033[1;35m\033[1;35'
385+
printf 'm]\033[0m\n'
386+
} > $d/expect
361387
cmp $d/color $d/expect
362388

363389
# Check garbage in JQ_COLORS. We write each color sequence into a 16

0 commit comments

Comments
 (0)