Skip to content

Commit 8455ef8

Browse files
committedJun 25, 2022
refactor: Mode duplication to shared function
·
v0.4.0v0.2.0
1 parent 0d829ed commit 8455ef8

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed
 

‎pkg/src/public/bash-std.sh‎

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,17 @@ std.find_parent_dir() {
1313
std.util.find_parent -d "$1"
1414
}
1515

16-
# @description Determine if color should be printed. Note that this doesn't
17-
# use tput because simple environment variable checking heuristics suffice
18-
std.should_output_color() {
19-
# https://no-color.org
20-
if [ ${NO_COLOR+x} ]; then
21-
return 1
22-
fi
23-
24-
# FIXME
25-
# # 0 => 2 colors
26-
# # 1 => 16 colors
27-
# # 2 => 256 colors
28-
# # 3 => 16,777,216 colors
29-
# if [[ -v FORCE_COLOR ]]; then
30-
# return 0
31-
# fi
32-
33-
if [ "$COLORTERM" = "truecolor" ] || [ "$COLORTERM" = "24bit" ]; then
34-
return 0
35-
fi
3616

37-
if [ "$TERM" = 'dumb' ]; then
38-
return 1
39-
fi
40-
41-
if [ -t 0 ]; then
42-
return 0
43-
fi
17+
# @description Determine if color should be printed to standard output
18+
# @noargs
19+
std.should_print_color_stdout() {
20+
std.private.should_print_color 1
21+
}
4422

45-
return 1
23+
# @description Determine if color should be printed to standard error
24+
# @noargs
25+
std.should_print_color_stderr() {
26+
std.private.should_print_color 2
4627
}
4728

4829
# @description Gets information from a particular package. If the key does not exist, then the value

‎pkg/src/util/util.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ std.find_parent() {
1919
); then :; else
2020
panic 'Failed to cd'
2121
fi
22+
}
23+
24+
std.private.should_print_color() {
25+
local fd="$1"
26+
27+
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
28+
return 1
29+
fi
30+
31+
if [ -t "$fd" ]; then
32+
return 0
33+
fi
34+
35+
return 1
2236
}

0 commit comments

Comments
 (0)
Please sign in to comment.