Skip to content
Myr

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

6 files+221 added−1 removed

modified:docs/content/3.manual/manual.yml+42 added
158 unmodified lines
159 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`.162163 Colors can be configured with the `JQ_COLORS` environment164 variable (see below).165166 * `--ascii-output` / `-a`:167168 jq usually outputs non-ASCII Unicode codepoints as UTF-8, even169 if the input specified them as escape sequences (like
3027 unmodified lines
3197 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.32003201 - title: Colors3202 body: |32033204 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:32073208 - color for `null`3209 - color for `false`3210 - color for `true`3211 - color for numbers3212 - color for strings3213 - color for arrays3214 - color for objects32153216 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"`.32183219 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:32223223 - 1 (bright)3224 - 2 (dim)3225 - 4 (underscore)3226 - 5 (blink)3227 - 7 (reverse)3228 - 8 (hidden)32293230 and the second is one of these:32313232 - 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 lines
65jv jq_util_input_get_position(jq_state*);66jv jq_util_input_get_current_filename(jq_state*);67jv jq_util_input_get_current_line(jq_state*);6869int jq_set_colors(const char *);7071#endif /* !JQ_H */
modified:src/jv_print.c+36 added-1 removed
25 unmodified lines
26// for how to choose these.27static 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};30static char color_bufs[sizeof(color_kinds)/sizeof(color_kinds[0])][16];31static const char *color_bufps[8];30static const char* const colors[] =32static 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")3637static const char **colors = def_colors;3839int40jq_set_colors(const char *c)41{42 const char *e;43 size_t i;4445 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}6970static 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 lines
modified:src/main.c+3 added
523 unmodified lines
524 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;527528 if (getenv("JQ_COLORS") != NULL && !jq_set_colors(getenv("JQ_COLORS")))529 fprintf(stderr, "Failed to set $JQ_COLORS\n");530531 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 lines
modified:tests/shtest+54 added
242 unmodified lines
243 echo "jq halt_error(1) had unexpected output" 1>&2244 exit 1245fi246247# Check $JQ_COLORS248$JQ -Ccn . > $d/color249printf '\033[1;30mnull\033[0m\n' > $d/expect250cmp $d/color $d/expect251JQ_COLORS='4;31' $JQ -Ccn . > $d/color252printf '\033[4;31mnull\033[0m\n' > $d/expect253cmp $d/color $d/expect254JQ_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(257printf '\033[1;35m[\033[1;36m{'258printf '\033[0m\033[34;1m"a"\033['259printf '0m\033[1;36m:\033[0m\033['260printf '0;32mtrue\033[0m\033[1'261printf ';36m,\033[0m\033[34;1m'262printf '"b"\033[0m\033[1;36m:\033'263printf '[0m\033[0;31mfalse\033'264printf '[0m\033[1;36m\033[1;36'265printf 'm}\033[0m\033[1;35m,\033['266printf '0;33m123\033[0m\033[1;'267printf '35m,\033[1;30mnull\033'268printf '[0m\033[1;35m\033[1;35'269printf 'm]\033[0m\n'270) > $d/expect271cmp $d/color $d/expect272273# 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.277set -vx278echo 'Failed to set $JQ_COLORS' > $d/expect_warning279$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/expect280JQ_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/warning282cmp $d/color $d/expect283cmp $d/warning $d/expect_warning284JQ_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/warning286cmp $d/color $d/expect287cmp $d/warning $d/expect_warning288JQ_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/warning290cmp $d/color $d/expect291cmp $d/warning $d/expect_warning292JQ_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/warning294cmp $d/color $d/expect295cmp $d/warning $d/expect_warning296JQ_COLORS="0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:0123456789123:" \297 $JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color 2>$d/warning298cmp $d/color $d/expect299cmp $d/warning $d/expect_warning300301exit 0
modified:jq.1.prebuilt+84 added
103 unmodified lines
104.105.IP106By 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.IP109Colors 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.IP
3260 unmodified lines
3375.3376.P3377Programs 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"3380To 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" 43383color for \fBnull\fR3384.3385.IP "\(bu" 43386color for \fBfalse\fR3387.3388.IP "\(bu" 43389color for \fBtrue\fR3390.3391.IP "\(bu" 43392color for numbers3393.3394.IP "\(bu" 43395color for strings3396.3397.IP "\(bu" 43398color for arrays3399.3400.IP "\(bu" 43401color for objects3402.3403.IP "" 03404.3405.P3406The 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.P3409This 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" 434121 (bright)3413.3414.IP "\(bu" 434152 (dim)3416.3417.IP "\(bu" 434184 (underscore)3419.3420.IP "\(bu" 434215 (blink)3422.3423.IP "\(bu" 434247 (reverse)3425.3426.IP "\(bu" 434278 (hidden)3428.3429.IP "" 03430.3431.P3432and the second is one of these:3433.3434.IP "\(bu" 4343530 (black)3436.3437.IP "\(bu" 4343831 (red)3439.3440.IP "\(bu" 4344132 (green)3442.3443.IP "\(bu" 4344433 (yellow)3445.3446.IP "\(bu" 4344734 (blue)3448.3449.IP "\(bu" 4345035 (magenta)3451.3452.IP "\(bu" 4345336 (cyan)3454.3455.IP "\(bu" 4345637 (white)3457.3458.IP "" 03459.3460.SH "BUGS"3461Presumably\. Report them or discuss them at:3462.3463.IP "" 4
11 unmodified lines