aboutsummaryrefslogtreecommitdiff
path: root/devtools
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-12-14 06:17:18 -0500
committerJonathan Amsterdam <jba@google.com>2021-12-14 15:18:54 +0000
commit29728f9d1dc17d2bc77e401b00bca0cb563117fc (patch)
tree21f4bfa960969538114da671312ea98a2594287a /devtools
parent503695b24cabdbe6677f6f973e081a9b44f3e5da (diff)
downloadgo-x-pkgsite-29728f9d1dc17d2bc77e401b00bca0cb563117fc.tar.xz
devtools/lib.sh: detect terminal colors better
Instead of guessing from the value of TERM whether the terminal supports colors, actually call tput. Change-Id: I9e7c3412d740fb139463682e49516d5199e9b4d8 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/371595 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org>
Diffstat (limited to 'devtools')
-rw-r--r--devtools/lib.sh32
1 files changed, 12 insertions, 20 deletions
diff --git a/devtools/lib.sh b/devtools/lib.sh
index 1ed82c91..ef56198e 100644
--- a/devtools/lib.sh
+++ b/devtools/lib.sh
@@ -4,19 +4,16 @@
# Library of useful bash functions and variables.
-RED=; GREEN=; YELLOW=; BLUE=; BOLD=; RESET=;
+RED=; GREEN=; YELLOW=; NORMAL=;
+MAXWIDTH=0
-case $TERM in
- '' | xterm) ;;
- # If xterm is not xterm-16color, xterm-88color, or xterm-256color, tput will
- # return the error:
- # tput: No value for $TERM and no -T specified
- *)
- RED=`tput setaf 1`
- GREEN=`tput setaf 2`
- YELLOW=`tput setaf 3`
- NORMAL=`tput sgr0`
-esac
+if tput setaf 1 >& /dev/null; then
+ RED=`tput setaf 1`
+ GREEN=`tput setaf 2`
+ YELLOW=`tput setaf 3`
+ NORMAL=`tput sgr0`
+ MAXWIDTH=$(( $(tput cols) - 2 ))
+fi
EXIT_CODE=0
@@ -36,14 +33,9 @@ runcmd() {
msg="$@"
# Truncate command logging for narrow terminals.
# Account for the 2 characters of '$ '.
- case ${TERM} in
- '' | xterm) ;;
- *)
- maxwidth=$(( $(tput cols) - 2 ))
- if [[ ${#msg} -gt $maxwidth ]]; then
- msg="${msg::$(( maxwidth - 3 ))}..."
- fi
- esac
+ if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then
+ msg="${msg::$(( MAXWIDTH - 3 ))}..."
+ fi
info "\$ $msg"
$@ || err "command failed"