Add JQ_COLORS env var for color config (fix #1252)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nico Williams · committed by William Langford · parent 32d8f20 · Browse files · 6 files·+221 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+221 added−1 removed
- tests:+54 added−0 removed
- other:+167 added−1 removed
Provenance
- Commit
6d89e29- Tree
cc70c53- Author
Nico Williams <nico@cryptonector.com>- Committer
William Langford <wlangfor@gmail.com>- Parent
32d8f20
6 files changed+221 added−1 removed
modified:docs/content/3.manual/manual.yml+42 added
158 unmodified lines159 By default, jq outputs colored JSON if writing to a160 terminal. You can force it to produce color even if writing to161 a pipe or a file using `-C`, and disable color with `-M`.162 163+ Colors can be configured with the `JQ_COLORS` environment164+ variable (see below).165+166 * `--ascii-output` / `-a`:167 168 jq usually outputs non-ASCII Unicode codepoints as UTF-8, even169 if the input specified them as escape sequences (like3027 unmodified lines3197 Programs can use this to query a module's metadata, which they3198 could then use to, for example, search for, download, and3199 install missing dependencies.3200 3201+ - title: Colors3202+ body: |3203+3204+ To configure alternative colors just set the `JQ_COLORS`3205+ environment variable to colon-delimited list of partial terminal3206+ escape sequences like `"1;31"`, in this order:3207+3208+ - color for `null`3209+ - color for `false`3210+ - color for `true`3211+ - color for numbers3212+ - color for strings3213+ - color for arrays3214+ - color for objects3215+3216+ The default color scheme is the same as setting3217+ `"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39"`.3218+3219+ This is not a manual for VT100/ANSI escapes. However, each of3220+ these color specifications should consist of two numbers separated3221+ by a semi-colon, where the first number is one of these:3222+3223+ - 1 (bright)3224+ - 2 (dim)3225+ - 4 (underscore)3226+ - 5 (blink)3227+ - 7 (reverse)3228+ - 8 (hidden)3229+3230+ and the second is one of these:3231+3232+ - 30 (black)3233+ - 31 (red)3234+ - 32 (green)3235+ - 33 (yellow)3236+ - 34 (blue)3237+ - 35 (magenta)3238+ - 36 (cyan)3239+ - 37 (white)modified:src/jq.h+2 added
64 unmodified lines65 jv jq_util_input_get_position(jq_state*);66 jv jq_util_input_get_current_filename(jq_state*);67 jv jq_util_input_get_current_line(jq_state*);68 69+int jq_set_colors(const char *);70+71 #endif /* !JQ_H */modified:src/jv_print.c+36 added-1 removed
25 unmodified lines26 // for how to choose these.27 static const jv_kind color_kinds[] =28 {JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER,29 JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT};30+static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16];31+static const char *color_bufps[8];30-static const char* const colors[] =32+static const char* def_colors[] =33 {COL("1;30"), COL("0;39"), COL("0;39"), COL("0;39"),34 COL("0;32"), COL("1;39"), COL("1;39")};35 #define FIELD_COLOR COL("34;1")36 37+static const char **colors = def_colors;38+39+int40+jq_set_colors(const char *c)41+{42+ const char *e;43+ size_t i;44+45+ if (c == NULL)46+ return 1;47+ colors = def_colors;48+ memset(color_bufs, 0, sizeof(color_bufs));49+ for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]); i++)50+ color_bufps[i] = def_colors[i];51+ for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]) && *c != '\0'; i++, c = e) {52+ if ((e = strchr(c, ':')) == NULL)53+ e = c + strlen(c);54+ if ((size_t)(e - c) > sizeof(color_bufs[i]) - 4 /* ESC [ m NUL */)55+ return 0;56+ color_bufs[i][0] = ESC[0];57+ color_bufs[i][1] = '[';58+ (void) strncpy(&color_bufs[i][2], c, e - c);59+ if (strspn(&color_bufs[i][2], "0123456789;") < strlen(&color_bufs[i][2]))60+ return 0;61+ color_bufs[i][2 + (e - c)] = 'm';62+ color_bufps[i] = color_bufs[i];63+ if (e[0] == ':')64+ e++;65+ }66+ colors = color_bufps;67+ return 1;68+}69+70 static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) {71 if (strout) {72 *strout = jv_string_append_buf(*strout, s, len);73 } else {328 unmodified linesmodified:src/main.c+3 added
523 unmodified lines524 if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;525 if (options & COLOR_OUTPUT) dumpopts |= JV_PRINT_COLOR;526 if (options & NO_COLOR_OUTPUT) dumpopts &= ~JV_PRINT_COLOR;527 528+ if (getenv("JQ_COLORS") != NULL && !jq_set_colors(getenv("JQ_COLORS")))529+ fprintf(stderr, "Failed to set $JQ_COLORS\n");530+531 if (jv_get_kind(lib_search_paths) == JV_KIND_NULL) {532 // Default search path list533 lib_search_paths = JV_ARRAY(jv_string("~/.jq"),534 jv_string("$ORIGIN/../lib/jq"),125 unmodified linesmodified:tests/shtest+54 added
242 unmodified lines243 echo "jq halt_error(1) had unexpected output" 1>&2244 exit 1245 fi246 247+# Check $JQ_COLORS248+$JQ -Ccn . > $d/color249+printf '\033[1;30mnull\033[0m\n' > $d/expect250+cmp $d/color $d/expect251+JQ_COLORS='4;31' $JQ -Ccn . > $d/color252+printf '\033[4;31mnull\033[0m\n' > $d/expect253+cmp $d/color $d/expect254+JQ_COLORS='1;30:0;31:0;32:0;33:0;34:1;35:1;36' \255+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color256+(257+printf '\033[1;35m[\033[1;36m{'258+printf '\033[0m\033[34;1m"a"\033['259+printf '0m\033[1;36m:\033[0m\033['260+printf '0;32mtrue\033[0m\033[1'261+printf ';36m,\033[0m\033[34;1m'262+printf '"b"\033[0m\033[1;36m:\033'263+printf '[0m\033[0;31mfalse\033'264+printf '[0m\033[1;36m\033[1;36'265+printf 'm}\033[0m\033[1;35m,\033['266+printf '0;33m123\033[0m\033[1;'267+printf '35m,\033[1;30mnull\033'268+printf '[0m\033[1;35m\033[1;35'269+printf 'm]\033[0m\n'270+) > $d/expect271+cmp $d/color $d/expect272+273+# Check garbage in JQ_COLORS. We write each color sequence into a 16274+# char buffer that needs to hold ESC [ <color> m NUL, so each color275+# sequence can be no more than 12 chars (bytes). These emit a warning276+# on stderr.277+set -vx278+echo 'Failed to set $JQ_COLORS' > $d/expect_warning279+$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect280+JQ_COLORS='garbage;30:*;31:,;3^:0;$%:0;34:1;35:1;36' \281+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning282+cmp $d/color $d/expect283+cmp $d/warning $d/expect_warning284+JQ_COLORS='1234567890123456789;30:0;31:0;32:0;33:0;34:1;35:1;36' \285+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning286+cmp $d/color $d/expect287+cmp $d/warning $d/expect_warning288+JQ_COLORS='1;31234567890123456789:0;31:0;32:0;33:0;34:1;35:1;36' \289+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning290+cmp $d/color $d/expect291+cmp $d/warning $d/expect_warning292+JQ_COLORS='1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456:1234567890123456;1234567890123456' \293+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning294+cmp $d/color $d/expect295+cmp $d/warning $d/expect_warning296+JQ_COLORS="0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:" \297+ $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning298+cmp $d/color $d/expect299+cmp $d/warning $d/expect_warning300+301 exit 0modified:jq.1.prebuilt+84 added
103 unmodified lines104 .105 .IP106 By default, jq outputs colored JSON if writing to a terminal\. You can force it to produce color even if writing to a pipe or a file using \fB\-C\fR, and disable color with \fB\-M\fR\.107 .108+.IP109+Colors can be configured with the \fBJQ_COLORS\fR environment variable (see below)\.110+.111 .IP "\(bu" 4112 \fB\-\-ascii\-output\fR / \fB\-a\fR:113 .114 .IP3260 unmodified lines3375 .3376 .P3377 Programs can use this to query a module\'s metadata, which they could then use to, for example, search for, download, and install missing dependencies\.3378 .3379+.SH "COLORS"3380+To configure alternative colors just set the \fBJQ_COLORS\fR environment variable to colon\-delimited list of partial terminal escape sequences like \fB"1;31"\fR, in this order:3381+.3382+.IP "\(bu" 43383+color for \fBnull\fR3384+.3385+.IP "\(bu" 43386+color for \fBfalse\fR3387+.3388+.IP "\(bu" 43389+color for \fBtrue\fR3390+.3391+.IP "\(bu" 43392+color for numbers3393+.3394+.IP "\(bu" 43395+color for strings3396+.3397+.IP "\(bu" 43398+color for arrays3399+.3400+.IP "\(bu" 43401+color for objects3402+.3403+.IP "" 03404+.3405+.P3406+The default color scheme is the same as setting \fB"JQ_COLORS=1;30:0;39:0;39:0;39:0;32:1;39:1;39"\fR\.3407+.3408+.P3409+This is not a manual for VT100/ANSI escapes\. However, each of these color specifications should consist of two numbers separated by a semi\-colon, where the first number is one of these:3410+.3411+.IP "\(bu" 43412+1 (bright)3413+.3414+.IP "\(bu" 43415+2 (dim)3416+.3417+.IP "\(bu" 43418+4 (underscore)3419+.3420+.IP "\(bu" 43421+5 (blink)3422+.3423+.IP "\(bu" 43424+7 (reverse)3425+.3426+.IP "\(bu" 43427+8 (hidden)3428+.3429+.IP "" 03430+.3431+.P3432+and the second is one of these:3433+.3434+.IP "\(bu" 43435+30 (black)3436+.3437+.IP "\(bu" 43438+31 (red)3439+.3440+.IP "\(bu" 43441+32 (green)3442+.3443+.IP "\(bu" 43444+33 (yellow)3445+.3446+.IP "\(bu" 43447+34 (blue)3448+.3449+.IP "\(bu" 43450+35 (magenta)3451+.3452+.IP "\(bu" 43453+36 (cyan)3454+.3455+.IP "\(bu" 43456+37 (white)3457+.3458+.IP "" 03459+.3460 .SH "BUGS"3461 Presumably\. Report them or discuss them at:3462 .3463 .IP "" 411 unmodified lines