Skip to content

Commit

Permalink
Make object key color configurable (close #1739, #1791, #2638)
Browse files Browse the repository at this point in the history
Co-authored-by: David Haguenauer <[email protected]>
Co-authored-by: Eric Pruitt <[email protected]>
  • Loading branch information
3 people committed Jul 12, 2023
1 parent 14e5e63 commit a3e8a10
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
3 changes: 2 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3471,9 +3471,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
Expand Down
5 changes: 4 additions & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 8 additions & 15 deletions src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
put_str(color, F, S, flags & JV_PRINT_ISATTY);
break;
}
}
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);
}
if (indent > MAX_PRINT_DEPTH) {
put_str("<skipped: too deep>", F, S, flags & JV_PRINT_ISATTY);
Expand Down
34 changes: 29 additions & 5 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -325,20 +325,44 @@ if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}
fi

# Check $JQ_COLORS
$JQ -Ccn . > $d/color
## Default colors, null input
JQ_COLORS= $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_COLORS= $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 -b $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 '\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[34;1m'
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'
Expand All @@ -356,7 +380,7 @@ cmp $d/color $d/expect
# on stderr.
set -vx
echo 'Failed to set $JQ_COLORS' > $d/expect_warning
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect
JQ_COLORS= $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect
JQ_COLORS='garbage;30:*;31:,;3^:0;$%:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
cmp $d/color $d/expect
Expand Down

0 comments on commit a3e8a10

Please sign in to comment.