By pre-setting terminal capabilities (colors, boldness) to shell
variables we avoid the overhead of running external commands (in
separate subshell) each time colored text is printed to screen.
---
 test/test-lib.sh | 84 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/test/test-lib.sh b/test/test-lib.sh
index ffab1bb..8be2065 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -38,9 +38,6 @@ done,*)
        ;;
 esac

-# Keep the original TERM for say_color and test_emacs
-ORIGINAL_TERM=$TERM
-
 # dtach(1) provides more capable terminal environment to anything
 # that requires more than dumb terminal...
 [ x"${TERM:-dumb}" = xdumb ] && DTACH_TERM=vt100 || DTACH_TERM=$TERM
@@ -50,8 +47,49 @@ LANG=C
 LC_ALL=C
 PAGER=cat
 TZ=UTC
+export LANG LC_ALL PAGER TZ
+
+if [ x"${TERM:-dumb}" != xdumb ]; then
+       [ -t 1 ] &&
+       # http://en.wikipedia.org/wiki/Tput
+       TC_BOLD=`tput bold 2>/dev/null` &&
+       TC_RED=`tput setaf 1 2>/dev/null` &&
+       TC_GREEN=`tput setaf 2 2>/dev/null` &&
+       TC_YELLOW=`tput setaf 3 2>/dev/null` &&
+       TC_RESET=`tput sgr0 2>/dev/null` ||
+       TC_BOLD=
+else
+       TC_BOLD=
+fi
+
+if test -n "$TC_BOLD"; then
+       say_color () {
+               case "$1" in
+                       error) printf '%s%s' "$TC_BOLD" "$TC_RED" ;;
+                       skip)  printf '%s%s' "$TC_BOLD" "$TC_GREEN" ;;
+                       pass)  printf '%s' "$TC_GREEN" ;;
+                       info)  printf '%s' "$TC_YELLOW" ;;
+                       *) test -n "$quiet" && return;;
+               esac
+               shift
+               printf " "
+               printf "$@"
+               printf '%s' "$TC_RESET"
+               print_subtest
+       }
+else
+       say_color() {
+               test -z "$1" && test -n "$quiet" && return
+               shift
+               printf " "
+               printf "$@"
+               print_subtest
+       }
+fi
+
 TERM=dumb
-export LANG LC_ALL PAGER TERM TZ
+export TERM
+
 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
 if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
       ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
@@ -82,15 +120,6 @@ _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
 # This test checks if command xyzzy does the right thing...
 # '
 # . ./test-lib.sh
-[ "x$ORIGINAL_TERM" != "xdumb" ] && (
-               TERM=$ORIGINAL_TERM &&
-               export TERM &&
-               [ -t 1 ] &&
-               tput bold >/dev/null 2>&1 &&
-               tput setaf 1 >/dev/null 2>&1 &&
-               tput sgr0 >/dev/null 2>&1
-       ) &&
-       color=t

 while test "$#" -ne 0
 do
@@ -136,35 +165,6 @@ else
     }
 fi

-if test -n "$color"; then
-       say_color () {
-               (
-               TERM=$ORIGINAL_TERM
-               export TERM
-               case "$1" in
-                       error) tput bold; tput setaf 1;; # bold red
-                       skip)  tput bold; tput setaf 2;; # bold green
-                       pass)  tput setaf 2;;            # green
-                       info)  tput setaf 3;;            # brown
-                       *) test -n "$quiet" && return;;
-               esac
-               shift
-               printf " "
-               printf "$@"
-               tput sgr0
-               print_subtest
-               )
-       }
-else
-       say_color() {
-               test -z "$1" && test -n "$quiet" && return
-               shift
-               printf " "
-               printf "$@"
-               print_subtest
-       }
-fi
-
 error () {
        say_color error "error: $*\n"
        GIT_EXIT_OK=t
-- 
1.8.0

Reply via email to