Disable colored output on a dumb terminal (#3552)
Enable colored output by default only when the TERM environment variable is set to a value other than dumb, as git and grep do, so that users no longer need to pass -M or set NO_COLOR on terminals that cannot display escape sequences. Colored output is now disabled not only for TERM=dumb but also when TERM is unset or empty. The -C option still enables colors in all these cases. On Windows, colored output is still decided by ANSICON and the console mode, and TERM is not checked.
This commit is contained in:
1 parent
6b53f80311
commit
633ae6058a
4 files changed
+44
-9
No files matched your search
@@ -169,8 +169,10 @@ sections:
|
||||
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 `-C`, and disable color with `-M`.
|
||||
When the `NO_COLOR` environment variable is not empty, jq disables
|
||||
colored output by default, but you can enable it by `-C`.
|
||||
When the `NO_COLOR` environment variable is not empty,
|
||||
or the `TERM` environment variable is unset, empty, or set to
|
||||
`dumb`, jq disables colored output by default, but you can
|
||||
enable it by `-C`.
|
||||
|
||||
Colors can be configured with the `JQ_COLORS` environment
|
||||
variable (see below).
|
||||
|
||||
Generated
+1
-1
@@ -115,7 +115,7 @@ Output the fields of each object with the keys in sorted order\.
|
||||
\fB\-\-color\-output\fR / \fB\-C\fR and \fB\-\-monochrome\-output\fR / \fB\-M\fR:
|
||||
.
|
||||
.IP
|
||||
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\. When the \fBNO_COLOR\fR environment variable is not empty, jq disables colored output by default, but you can enable it by \fB\-C\fR\.
|
||||
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\. When the \fBNO_COLOR\fR environment variable is not empty, or the \fBTERM\fR environment variable is unset, empty, or set to \fBdumb\fR, jq disables colored output by default, but you can enable it by \fB\-C\fR\.
|
||||
.
|
||||
.IP
|
||||
Colors can be configured with the \fBJQ_COLORS\fR environment variable (see below)\.
|
||||
|
||||
+4
-1
@@ -543,7 +543,10 @@ int main(int argc, char* argv[]) {
|
||||
#ifdef USE_ISATTY
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
#ifndef WIN32
|
||||
dumpopts |= JV_PRINT_ISATTY | JV_PRINT_COLOR;
|
||||
dumpopts |= JV_PRINT_ISATTY;
|
||||
char *term = getenv("TERM");
|
||||
if (term != NULL && term[0] != '\0' && strcmp(term, "dumb"))
|
||||
dumpopts |= JV_PRINT_COLOR;
|
||||
#else
|
||||
/* Verify we actually have the console, as the NUL device is also regarded as
|
||||
tty. Windows can handle color if ANSICON (or ConEmu) is installed, or
|
||||
|
||||
+35
-5
@@ -691,7 +691,7 @@ for colors in '/' '[30' '30m' '30:31m:32' '30:*:31' 'invalid'; do
|
||||
done
|
||||
unset JQ_COLORS
|
||||
|
||||
# Check $NO_COLOR
|
||||
# Check $NO_COLOR and $TERM
|
||||
test_no_color=true
|
||||
$msys && test_no_color=false
|
||||
$mingw && test_no_color=false
|
||||
@@ -708,22 +708,52 @@ if $test_no_color && command -v script >/dev/null 2>&1; then
|
||||
sed 's/^\x5E\x44\x08\x08//'; }
|
||||
fi
|
||||
|
||||
faketty $JQ_NO_B -n . > $d/color
|
||||
faketty env TERM=xterm-256color $JQ_NO_B -n . > $d/color
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env NO_COLOR= $JQ_NO_B -n . > $d/color
|
||||
faketty env TERM=xterm-256color NO_COLOR= $JQ_NO_B -n . > $d/color
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env NO_COLOR=1 $JQ_NO_B -n . > $d/color
|
||||
faketty env TERM=xterm-256color NO_COLOR=1 $JQ_NO_B -n . > $d/color
|
||||
printf 'null\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env NO_COLOR=1 $JQ_NO_B -Cn . > $d/color
|
||||
faketty env TERM=xterm-256color NO_COLOR=1 $JQ_NO_B -Cn . > $d/color
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env TERM=dumb $JQ_NO_B -n . > $d/color
|
||||
printf 'null\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env TERM=dumb $JQ_NO_B -Cn . > $d/color
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env TERM= $JQ_NO_B -n . > $d/color
|
||||
printf 'null\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
faketty env TERM= $JQ_NO_B -Cn . > $d/color
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
(unset TERM; faketty $JQ_NO_B -n . > $d/color)
|
||||
printf 'null\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
cmp $d/color $d/expect
|
||||
(unset TERM; faketty $JQ_NO_B -Cn . > $d/color)
|
||||
printf '\033[0;90mnull\033[0m\r\n' > $d/expect
|
||||
od -tc $d/expect
|
||||
od -tc $d/color
|
||||
|
||||
Reference in new issue
Block a user