Skip to content

Commit 9c4f9ee

Browse files
author
OpenShift Bot
authored
Merge pull request #11877 from stevekuznetsov/skuznets/tty-test
Merged by openshift-bot
2 parents 8b037a2 + cbda43c commit 9c4f9ee

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

hack/lib/util/text.sh

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@
55

66
# os::text::reset resets the terminal output to default if it is called in a TTY
77
function os::text::reset() {
8-
if [[ -t 1 ]]; then
8+
if os::text::internal::is_tty; then
99
tput sgr0
1010
fi
1111
}
1212
readonly -f os::text::reset
1313

1414
# os::text::bold sets the terminal output to bold text if it is called in a TTY
1515
function os::text::bold() {
16-
if [[ -t 1 ]]; then
16+
if os::text::internal::is_tty; then
1717
tput bold
1818
fi
1919
}
2020
readonly -f os::text::bold
2121

2222
# os::text::red sets the terminal output to red text if it is called in a TTY
2323
function os::text::red() {
24-
if [[ -t 1 ]]; then
24+
if os::text::internal::is_tty; then
2525
tput setaf 1
2626
fi
2727
}
2828
readonly -f os::text::red
2929

3030
# os::text::green sets the terminal output to green text if it is called in a TTY
3131
function os::text::green() {
32-
if [[ -t 1 ]]; then
32+
if os::text::internal::is_tty; then
3333
tput setaf 2
3434
fi
3535
}
3636
readonly -f os::text::green
3737

3838
# os::text::blue sets the terminal output to blue text if it is called in a TTY
3939
function os::text::blue() {
40-
if [[ -t 1 ]]; then
40+
if os::text::internal::is_tty; then
4141
tput setaf 4
4242
fi
4343
}
4444
readonly -f os::text::blue
4545

4646
# os::text::yellow sets the terminal output to yellow text if it is called in a TTY
4747
function os::text::yellow() {
48-
if [[ -t 1 ]]; then
48+
if os::text::internal::is_tty; then
4949
tput setaf 11
5050
fi
5151
}
@@ -55,7 +55,7 @@ readonly -f os::text::yellow
5555
# terminal and leaves the cursor on that line to allow for overwriting that text
5656
# if it is called in a TTY
5757
function os::text::clear_last_line() {
58-
if [[ -t 1 ]]; then
58+
if os::text::internal::is_tty; then
5959
tput cuu 1
6060
tput el
6161
fi
@@ -69,7 +69,7 @@ readonly -f os::text::clear_last_line
6969
# No action is taken if this is called outside of a TTY
7070
function os::text::clear_string() {
7171
local -r string="$1"
72-
if [[ -t 1 ]]; then
72+
if os::text::internal::is_tty; then
7373
echo "${string}" | while read line; do
7474
# num_lines is the number of terminal lines this one line of output
7575
# would have taken up with the current terminal width in columns
@@ -81,6 +81,16 @@ function os::text::clear_string() {
8181
fi
8282
}
8383

84+
# If $TERM is set but not exported, we will not be able to call
85+
# tput. Therefore, we export whatever is present in $TERM.
86+
export TERM
87+
88+
# os::text::internal::is_tty determines if we are outputting to a TTY
89+
function os::text::internal::is_tty() {
90+
[[ -t 1 && -n "${TERM:-}" ]]
91+
}
92+
readonly -f os::text::internal::is_tty
93+
8494
# os::text::print_bold prints all input in bold text
8595
function os::text::print_bold() {
8696
os::text::bold

0 commit comments

Comments
 (0)