Skip to content

Commit bf8a119

Browse files
committed
fix: Should print color properly works
- Properly uses private version of function - Uses `FORCE_COLOR` to override
1 parent e70960a commit bf8a119

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

pkg/src/public/bash-core.sh

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,9 @@ core.print_debug() {
394394
# use tput because simple environment variable checking heuristics suffice. Deprecated because this code
395395
# has been moved to bash-std
396396
core.should_output_color() {
397-
local fd="$1"
398-
399-
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
400-
return 1
401-
fi
402-
403-
if [ -t "$fd" ]; then
404-
return 0
397+
if core.private.should_print_color "$@"; then :; else
398+
return $?
405399
fi
406-
407-
return 1
408400
}
409401

410402
# @description (DEPRECATED) Gets information from a particular package. If the key does not exist, then the value

pkg/src/util/util.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@ core.private.util.err_print() {
7979
# @internal
8080
core.private.should_print_color() {
8181
local fd="$1"
82+
83+
if [ ${NO_COLOR+x} ]; then
84+
return 1
85+
fi
86+
87+
if [[ $FORCE_COLOR == @(1|2|3) ]]; then
88+
return 0
89+
elif [[ $FORCE_COLOR == '0' ]]; then
90+
return 1
91+
fi
8292

83-
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
93+
if [ "$TERM" = 'dumb' ]; then
8494
return 1
8595
fi
8696

tests/misc.bats

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@ load './util/init.sh'
1515
}
1616

1717
@test "core.should_output_color works" {
18-
unset NO_COLOR COLORTERM TERM
18+
unset -v NO_COLOR FORCE_COLOR TERM
1919

2020
NO_COLOR= run core.should_output_color
2121
assert_failure
2222

23+
FORCE_COLOR=0 run core.should_output_color
24+
assert_failure
25+
26+
FORCE_COLOR=1 run core.should_output_color
27+
assert_success
28+
29+
FORCE_COLOR=2 run core.should_output_color
30+
assert_success
31+
32+
FORCE_COLOR=3 run core.should_output_color
33+
assert_success
34+
35+
# NO_COLOR has precedent over FORCE_COLOR
36+
NO_COLOR= FORCE_COLOR=1 run core.should_output_color
37+
assert_failure
38+
2339
TERM='dumb' run core.should_output_color
2440
assert_failure
2541
}

0 commit comments

Comments
 (0)