devtools/lib.sh: detect terminal colors better
Instead of guessing from the value of TERM
whether the terminal supports colors, actually
call tput.
Trust: Jonathan Amsterdam
Run-TryBot: Jonathan Amsterdam
TryBot-Result: kokoro
# Library of useful bash functions and variables.
-RED=; GREEN=; YELLOW=; BLUE=; BOLD=; RESET=;
-
-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=; GREEN=; YELLOW=; NORMAL=;
+MAXWIDTH=0
+
+if tput setaf 1 >& /dev/null; then
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
NORMAL=`tput sgr0`
-esac
+ 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 ))}..."
+ if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then
+ msg="${msg::$(( MAXWIDTH - 3 ))}..."
fi
- esac
info "\$ $msg"
$@ || err "command failed"
