Skip to content

Commit 951e04e

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 951e04e

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
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-3
Original file line numberDiff line numberDiff line change
@@ -325,20 +325,46 @@ if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}
325325
fi
326326

327327
# Check $JQ_COLORS
328+
unset JQ_COLORS
329+
330+
## Default colors, null input
328331
$JQ -Ccn . > $d/color
329332
printf '\033[1;30mnull\033[0m\n' > $d/expect
330333
cmp $d/color $d/expect
334+
335+
## Set non-default color, null input
331336
JQ_COLORS='4;31' $JQ -Ccn . > $d/color
332337
printf '\033[4;31mnull\033[0m\n' > $d/expect
333338
cmp $d/color $d/expect
334-
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \
339+
340+
## Default colors, complex input
341+
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
342+
(
343+
printf '\033[1;37m[\033[1;37m{'
344+
printf '\033[0m\033[1;34m"a"\033['
345+
printf '0m\033[1;37m:\033[0m\033['
346+
printf '0;37mtrue\033[0m\033[1'
347+
printf ';37m,\033[0m\033[1;34m'
348+
printf '"b"\033[0m\033[1;37m:\033'
349+
printf '[0m\033[0;37mfalse\033'
350+
printf '[0m\033[1;37m\033[1;37'
351+
printf 'm}\033[0m\033[1;37m,\033['
352+
printf '0;37m123\033[0m\033[1;'
353+
printf '37m,\033[1;30mnull\033'
354+
printf '[0m\033[1;37m\033[1;37'
355+
printf 'm]\033[0m\n'
356+
) > $d/expect
357+
cmp -b $d/color $d/expect
358+
359+
## Set non-default colors, complex input
360+
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36:1;30' \
335361
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
336362
(
337363
printf '\033[1;35m[\033[1;36m{'
338-
printf '\033[0m\033[34;1m"a"\033['
364+
printf '\033[0m\033[1;30m"a"\033['
339365
printf '0m\033[1;36m:\033[0m\033['
340366
printf '0;32mtrue\033[0m\033[1'
341-
printf ';36m,\033[0m\033[34;1m'
367+
printf ';36m,\033[0m\033[1;30m'
342368
printf '"b"\033[0m\033[1;36m:\033'
343369
printf '[0m\033[0;31mfalse\033'
344370
printf '[0m\033[1;36m\033[1;36'

0 commit comments

Comments
 (0)