Skip to content

Commit

Permalink
Respect NO_COLOR environment variable to disable color output
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny authored and nicowilliams committed Jul 19, 2023
1 parent 87e3dfd commit 0615d49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ int main(int argc, char* argv[]) {
dumpopts |= JV_PRINT_COLOR;
}
#endif
if (dumpopts & JV_PRINT_COLOR) {
char *no_color = getenv("NO_COLOR");
if (no_color != NULL && no_color[0] != '\0')
dumpopts &= ~JV_PRINT_COLOR;
}
}
#endif
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
Expand Down
24 changes: 24 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,28 @@ JQ_COLORS="0123456789123:0123456789123:0123456789123:0123456789123:0123456789123
cmp $d/color $d/expect
cmp $d/warning $d/expect_warning

# Check $NO_COLOR
if command -v script >/dev/null 2>&1; then
unset NO_COLOR
if script -qc echo /dev/null >/dev/null 2>&1; then
faketty() { script -qec "$*" /dev/null; }
else # macOS
faketty() { script -q /dev/null "$@" /dev/null |
sed 's/^\x5E\x44\x08\x08//'; }
fi

faketty $JQ -n . > $d/color
printf '\033[1;30mnull\033[0m\r\n' > $d/expect
cmp $d/color $d/expect
NO_COLOR= faketty $JQ -n . > $d/color
printf '\033[1;30mnull\033[0m\r\n' > $d/expect
cmp $d/color $d/expect
NO_COLOR=1 faketty $JQ -n . > $d/color
printf 'null\r\n' > $d/expect
cmp $d/color $d/expect
NO_COLOR=1 faketty $JQ -Cn . > $d/color
printf '\033[1;30mnull\033[0m\r\n' > $d/expect
cmp $d/color $d/expect
fi

exit 0

0 comments on commit 0615d49

Please sign in to comment.