5
5
6
6
# os::text::reset resets the terminal output to default if it is called in a TTY
7
7
function os::text::reset() {
8
- if [[ -t 1 ]] ; then
8
+ if os::text::internal::is_tty ; then
9
9
tput sgr0
10
10
fi
11
11
}
12
12
readonly -f os::text::reset
13
13
14
14
# os::text::bold sets the terminal output to bold text if it is called in a TTY
15
15
function os::text::bold() {
16
- if [[ -t 1 ]] ; then
16
+ if os::text::internal::is_tty ; then
17
17
tput bold
18
18
fi
19
19
}
20
20
readonly -f os::text::bold
21
21
22
22
# os::text::red sets the terminal output to red text if it is called in a TTY
23
23
function os::text::red() {
24
- if [[ -t 1 ]] ; then
24
+ if os::text::internal::is_tty ; then
25
25
tput setaf 1
26
26
fi
27
27
}
28
28
readonly -f os::text::red
29
29
30
30
# os::text::green sets the terminal output to green text if it is called in a TTY
31
31
function os::text::green() {
32
- if [[ -t 1 ]] ; then
32
+ if os::text::internal::is_tty ; then
33
33
tput setaf 2
34
34
fi
35
35
}
36
36
readonly -f os::text::green
37
37
38
38
# os::text::blue sets the terminal output to blue text if it is called in a TTY
39
39
function os::text::blue() {
40
- if [[ -t 1 ]] ; then
40
+ if os::text::internal::is_tty ; then
41
41
tput setaf 4
42
42
fi
43
43
}
44
44
readonly -f os::text::blue
45
45
46
46
# os::text::yellow sets the terminal output to yellow text if it is called in a TTY
47
47
function os::text::yellow() {
48
- if [[ -t 1 ]] ; then
48
+ if os::text::internal::is_tty ; then
49
49
tput setaf 11
50
50
fi
51
51
}
@@ -55,7 +55,7 @@ readonly -f os::text::yellow
55
55
# terminal and leaves the cursor on that line to allow for overwriting that text
56
56
# if it is called in a TTY
57
57
function os::text::clear_last_line() {
58
- if [[ -t 1 ]] ; then
58
+ if os::text::internal::is_tty ; then
59
59
tput cuu 1
60
60
tput el
61
61
fi
@@ -69,7 +69,7 @@ readonly -f os::text::clear_last_line
69
69
# No action is taken if this is called outside of a TTY
70
70
function os::text::clear_string() {
71
71
local -r string=" $1 "
72
- if [[ -t 1 ]] ; then
72
+ if os::text::internal::is_tty ; then
73
73
echo " ${string} " | while read line; do
74
74
# num_lines is the number of terminal lines this one line of output
75
75
# would have taken up with the current terminal width in columns
@@ -81,6 +81,16 @@ function os::text::clear_string() {
81
81
fi
82
82
}
83
83
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
+
84
94
# os::text::print_bold prints all input in bold text
85
95
function os::text::print_bold() {
86
96
os::text::bold
0 commit comments