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

Set field color using JQ_COLORS #1791

Closed
wants to merge 6 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
5 changes: 3 additions & 2 deletions docs/content/3.manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3264,7 +3264,7 @@ sections:
body: |

To configure alternative colors just set the `JQ_COLORS`
environment variable to colon-delimited list of partial terminal
environment variable to colon-separated list of partial terminal
escape sequences like `"1;31"`, in this order:

- color for `null`
Expand All @@ -3274,9 +3274,10 @@ sections:
- color for strings
- color for arrays
- color for objects
- color for fields (e.g., object keys)

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
3 changes: 2 additions & 1 deletion src/jv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ typedef enum {
JV_KIND_NUMBER,
JV_KIND_STRING,
JV_KIND_ARRAY,
JV_KIND_OBJECT
JV_KIND_OBJECT,
JV_KIND_FIELD,
} jv_kind;

struct jv_refcnt;
Expand Down
6 changes: 3 additions & 3 deletions src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
// 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};
JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT, JV_KIND_FIELD};
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("0;32"), COL("1;39"), COL("1;39"), COL("1;34")};
#define FIELD_COLOR (colors[JV_KIND_FIELD - 1])

static const char **colors = def_colors;

Expand Down
30 changes: 27 additions & 3 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,44 @@ if [ "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1)" != xyz ]; then
fi

# Check $JQ_COLORS
## Default colors, null input
$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 -Ccn '[{"a":true,"b":false},123,null]' > $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[1;39'
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 Down