Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override field color with JQ_COLORS #1977

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3284,9 +3284,10 @@ sections:
- color for strings
- color for arrays
- color for objects
- color for field names

The default color scheme is the same as setting
`"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39"`.
`"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39: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
8 changes: 4 additions & 4 deletions src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ static const jv_kind color_kinds[] =
static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16];
static const char *color_bufps[8];
static const char* def_colors[] =
{COL("1;30"), COL("0;39"), COL("0;39"), COL("0;39"),
COL("0;32"), COL("1;39"), COL("1;39")};
#define FIELD_COLOR COL("34;1")
{COL("1;30"), COL("0;39"), COL("0;39"), COL("0;39"),
COL("0;32"), COL("1;39"), COL("1;39"), COL("1;34")};
#define FIELD_COLOR_INDEX 7

static const char **colors = def_colors;

Expand Down Expand Up @@ -328,7 +328,7 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);

first = 0;
if (color) put_str(FIELD_COLOR, F, S, flags & JV_PRINT_ISATTY);
if (color) put_str(colors[FIELD_COLOR_INDEX], F, S, flags & JV_PRINT_ISATTY);
jvp_dump_string(key, flags & JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
jv_free(key);
if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
Expand Down
47 changes: 35 additions & 12 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,45 @@ cmp $d/color $d/expect
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' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color
JQ_COLORS= \
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color
(
printf '\033[1;39m[\033[1;39m{'
printf '\033[0m\033[1;34m"a"\033['
printf '0m\033[1;39m:\033[0m\033['
printf '0;39mtrue\033[0m\033[1'
printf ';39m,\033[0m\033[1;34m'
printf '"b"\033[0m\033[1;39m:\033'
printf '[0m\033[0;39mfalse\033'
printf '[0m\033[1;39m\033[1;39'
printf 'm}\033[0m\033[1;39m,\033['
printf '0;39m123\033[0m\033[1;'
printf '39m,\033[1;30mnull\033'
printf '[0m\033[1;39m,\033[0;3'
printf '2m"string"\033[0m\033['
printf '1;39m\033[1;39m]\033[0'
printf 'm\n'
) > $d/expect

cmp $d/color $d/expect
JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36:1;37' \
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color
(
printf '\033[1;35m[\033[1;36m{'
printf '\033[0m\033[34;1m"a"\033['
printf '\033[0m\033[1;37m"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;37m'
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'
printf '[0m\033[1;35m,\033[0;3'
printf '4m"string"\033[0m\033['
printf '1;35m\033[1;35m]\033[0'
printf 'm\n'
) > $d/expect
cmp $d/color $d/expect

Expand All @@ -310,25 +333,25 @@ 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 -Ccn '[{"a":true,"b":false},123,null,"string"]' > $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
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color 2>$d/warning
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning
JQ_COLORS='1234567890123456789;30:0;31:0;32:0;33:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color 2>$d/warning
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning
JQ_COLORS='1;31234567890123456789:0;31:0;32:0;33:0;34:1;35:1;36' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color 2>$d/warning
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning
JQ_COLORS='1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456' \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color 2>$d/warning
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning
JQ_COLORS="0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:" \
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning
$JQ -Ccn '[{"a":true,"b":false},123,null,"string"]' > $d/color 2>$d/warning
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning

Expand Down