test-lib: add support for colors without tput
For platforms that does not have tput we can still perform coloring by manually emitting the ANSI control codes. If tput is missing from $PATH, install a replacement function.
The exact strings has been dumped from a machine that has tput, by piping the output of tput through 'od -c -An'.
---
I got slightly annoyed that we didn't get colored output from the tests on Windows, so I decided to fix it.
Hopefully other platforms can benefit from this as well.
I'm not super happy with the condition to enable it. I considered an environment variable as well, but decided against it because "make -C t" from the root does not seem to pick up environment variables configured in the main Makefile.
Thoughts?
t/test-lib.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
t/test-lib.sh +14 -0
@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh + +if ! which tput > /dev/null ; then + tput () { + case "$1" in + bold) + echo -ne "\033[1m" ;; + setaf) + echo -ne "\033[0;3$2m" ;; + sgr0) + echo -ne "\033(\033[m" ;; + esac + } +fi + [ "x$ORIGINAL_TERM" != "xdumb" ] && ( TERM=$ORIGINAL_TERM && export TERM &&
Signature · 15 lines
1.7.11.msysgit.0.5.g0225efe.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
t/test-lib.sh export _x05 _x40 _z40 LF · line 133
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh +2
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh + +if ! which tput > /dev/null ; then
Testing the return value of "which" is not portable (I know, it's insane; SunOS is the common offender). Use "type" instead.
-Peff
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 15 lines · Jeff King
t/test-lib.sh export _x05 _x40 _z40 LF · line 133
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh +2
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh + +if ! which tput > /dev/null ; thenTesting the return value of "which" is not portable (I know, it's insane; SunOS is the common offender). Use "type" instead.
Junio already noticed it, and I suggested a fix that involved running it. However, I like your fix much better, thanks :)
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 8 lines · Erik Faye-Lund
t/test-lib.sh +0
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh
t/test-lib.sh export _x05 _x40 _z40 LF · line 141
Quoted 10 lines
t/test-lib.sh +10
-0+ +if ! which tput > /dev/null ; then + tput () { + case "$1" in + bold) + echo -ne "\033[1m" ;; + setaf) + echo -ne "\033[0;3$2m" ;; + sgr0) + echo -ne "\033(\033[m" ;;
I should of course have checked this earlier, but I find now that "echo -ne" isn't portable. So perhaps this on top?
t/test-lib.sh +3 -3
@@ -134,11 +134,11 @@ if ! which tput > /dev/null ; then tput () { case "$1" in bold) - echo -ne "\033[1m" ;; + printf "%b" "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "%b" "\033[0;3$2m" ;; sgr0) - echo -ne "\033(\033[m" ;; + printf "%b" "\033(\033[m" ;; esac } fi
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 14 lines · Erik Faye-Lund
Quoted 13 lines · Erik Faye-Lund
t/test-lib.sh +5
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh + +if ! which tput > /dev/null ; then + tput () { + case "$1" in + bold)
Quoted 8 lines
t/test-lib.sh export _x05 _x40 _z40 LF · line 141
Quoted 5 lines
t/test-lib.sh +5
-0+ echo -ne "\033[1m" ;; + setaf) + echo -ne "\033[0;3$2m" ;; + sgr0) + echo -ne "\033(\033[m" ;;I should of course have checked this earlier, but I find now that "echo -ne" isn't portable.
Neither is which, no?
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 25 lines · Junio C Hamano
Quoted 14 lines · Erik Faye-Lund
Quoted 13 lines · Erik Faye-Lund
t/test-lib.sh +5
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh + +if ! which tput > /dev/null ; then + tput () { + case "$1" in + bold)Quoted 8 lines
t/test-lib.sh export _x05 _x40 _z40 LF · line 141
Quoted 5 lines
t/test-lib.sh +5
-0+ echo -ne "\033[1m" ;; + setaf) + echo -ne "\033[0;3$2m" ;; + sgr0) + echo -ne "\033(\033[m" ;;I should of course have checked this earlier, but I find now that "echo -ne" isn't portable.
Quoted 1 line
Neither is which, no?
Oooh, right. Thanks for noticing. So I guess I should try to run it instead. From the POSIX spec, I can't find a way of running it that guarantees a return-code of 0 without clobbering the console somehow.
Perhaps the best thing is pass no operands, and check for $? == 127 instead?
Something like this?
t/test-lib.sh +2 -1
@@ -130,7 +130,8 @@ export _x05 _x40 _z40 LF # ' # . ./test-lib.sh -if ! which tput > /dev/null ; then +tput > /dev/null +if test $? -eq 127 ; then tput () { case "$1" in bold)
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 4 lines · Erik Faye-Lund
Quoted 1 line
Neither is which, no?
Oooh, right. Thanks for noticing. So I guess I should try to run it instead. From the POSIX spec,...
If you assume POSIX.1, there is "type".
$ type frotz ; echo $?
frotz is /usr/games/frotz
0
$ type frobnitz ; echo $?
bash: type: frobnitz: not found
1Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 32 lines · Erik Faye-Lund
Quoted 8 lines · Erik Faye-Lund
t/test-lib.sh +0
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sht/test-lib.sh export _x05 _x40 _z40 LF · line 141
Quoted 10 lines
t/test-lib.sh +10
-0+ +if ! which tput > /dev/null ; then + tput () { + case "$1" in + bold) + echo -ne "\033[1m" ;; + setaf) + echo -ne "\033[0;3$2m" ;; + sgr0) + echo -ne "\033(\033[m" ;;I should of course have checked this earlier, but I find now that "echo -ne" isn't portable. So perhaps this on top?
t/test-lib.sh +0
-1@@ -134,11 +134,11 @@ if ! which tput > /dev/null ; then tput () { case "$1" in bold) - echo -ne "\033[1m" ;;
t/test-lib.sh if ! which tput > /dev/null ; then · line 144
Quoted 10 lines
t/test-lib.sh +3
-2+ printf "%b" "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "%b" "\033[0;3$2m" ;; sgr0) - echo -ne "\033(\033[m" ;; + printf "%b" "\033(\033[m" ;; esac } fi
And again, I'm stupid for not reading documentation properly; octal escaped strings in the format string should work (and does on my systems), so this is sufficient:
t/test-lib.sh +3 -3
@@ -134,11 +134,11 @@ if ! which tput > /dev/null ; then tput () { case "$1" in bold) - echo -ne "\033[1m" ;; + printf "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "\033[0;3$2m" ;; sgr0) - echo -ne "\033(\033[m" ;; + printf "\033(\033[m" ;; esac } fi
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Am 14.09.2012 18:58, schrieb Erik Faye-Lund:
t/test-lib.sh if ! which tput > /dev/null ; then · line 139
Quoted 8 lines
t/test-lib.sh +2
-2tput () { case "$1" in bold) - echo -ne "\033[1m" ;; + printf "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "\033[0;3$2m" ;;
This should be
printf '\033[0;3%sm' "$2" ;;
t/test-lib.sh if ! which tput > /dev/null ; then · line 144
Quoted 6 lines
t/test-lib.sh +1
-1sgr0) - echo -ne "\033(\033[m" ;; + printf "\033(\033[m" ;; esac } fi
Did you test this only in rxvt or in CMD as well? (I hadn't time to test, yet, so I'm asking :-)
-- Hannes
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 10 lines · Johannes Sixt
Am 14.09.2012 18:58, schrieb Erik Faye-Lund:
t/test-lib.sh if ! which tput > /dev/null ; then · line 139
Quoted 8 lines
tput () { case "$1" in bold)t/test-lib.sh +2
-2- echo -ne "\033[1m" ;; + printf "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "\033[0;3$2m" ;;
Quoted 2 lines
This should be
printf '\033[0;3%sm' "$2" ;;
Is there a reason for %s rather than %d? It seem it only takes integers, and with %d at least we'd get an error message if someone started doing something else...
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Am 14.09.2012 20:11, schrieb Erik Faye-Lund:
Quoted 5 lines · Johannes Sixt
Quoted 1 line
printf '\033[0;3%sm' "$2" ;;Is there a reason for %s rather than %d? It seem it only takes integers,..
No reason. I just mechanically converted your original expression. But there is no reason for my conversion, either, if it can be more or less guaranteed that no arbitrary strings are passed in $2.
-- Hannes
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 10 lines · Johannes Sixt
Am 14.09.2012 18:58, schrieb Erik Faye-Lund:
t/test-lib.sh if ! which tput > /dev/null ; then · line 139
Quoted 8 lines
tput () { case "$1" in bold)t/test-lib.sh +2
-2- echo -ne "\033[1m" ;; + printf "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "\033[0;3$2m" ;;
Quoted 3 lines
This should be
printf '\033[0;3%sm' "$2" ;;
That's probably a good idea, yeah.
Quoted 9 lines
t/test-lib.sh if ! which tput > /dev/null ; then · line 144
Quoted 6 lines
sgr0)t/test-lib.sh +1
-1- echo -ne "\033(\033[m" ;; + printf "\033(\033[m" ;; esac } fiDid you test this only in rxvt or in CMD as well? (I hadn't time to test, yet, so I'm asking :-)
I don't have rxvt installed, but it works for me in CMD also.
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Am 14.09.2012 19:28, schrieb Johannes Sixt:
Quoted 13 lines
Am 14.09.2012 18:58, schrieb Erik Faye-Lund:
t/test-lib.sh if ! which tput > /dev/null ; then · line 139
Quoted 8 lines
t/test-lib.sh +2
-2tput () { case "$1" in bold) - echo -ne "\033[1m" ;; + printf "\033[1m" ;; setaf) - echo -ne "\033[0;3$2m" ;; + printf "\033[0;3$2m" ;;This should be
printf '\033[0;3%sm' "$2" ;;
Quoted 9 lines
t/test-lib.sh if ! which tput > /dev/null ; then · line 144
Quoted 6 lines
t/test-lib.sh +1
-1sgr0) - echo -ne "\033(\033[m" ;; + printf "\033(\033[m" ;; esac } fiDid you test this only in rxvt or in CMD as well? (I hadn't time to test, yet, so I'm asking :-)
I tested your patch with this fixup, and it works for me (in CMD).
-- Hannes
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
2012/9/14 Erik Faye-Lund <[email protected]>:
Quoted 11 lines · Erik Faye-Lund
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh export _x05 _x40 _z40 LF · line 132
Quoted 9 lines
t/test-lib.sh +1
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh +
Nice. But this setting should be check that we have a terminal first isn't ? Some test like this before
test "X$$TERM" != Xdumb \
&& test -t 1 2>/dev/null \
&& ....
or the inverse logic. This is what automake and popt autogen.sh does.
Best Regards
2012/9/14 Elia Pinto <[email protected]>:
Quoted 15 lines
2012/9/14 Erik Faye-Lund <[email protected]>:
Quoted 11 lines · Erik Faye-Lund
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh export _x05 _x40 _z40 LF · line 132
Quoted 9 lines
t/test-lib.sh +1
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh +Nice. But this setting should be check that we have a terminal first isn't ? Some test like this before
Quoted 3 lines
test "X$$TERM" != Xdumb \
&& test -t 1 2>/dev/null \
&& ....
and in reality this echo use is not portable.
http://ftp.gnu.org/old-gnu/Manuals/autoconf-2.53/html_node/Limitations-of-Builtins.html
In popt 1_17 autogen.sh does
red=; grn=; lgn=; blu=; std=;
test "X$$TERM" != Xdumb \
&& test -t 1 2>/dev/null \
&& { \
red='^[[0;31m'; \
grn='^[[0;32m'; \
lgn='^[[1;32m'; \
blu='^[[1;34m'; \
std='^[[m'; \
}
and
Die() {
color="$red"
echo "${color}${_PROGNAME}: Error: $@${std}" >&2
exit 1
}Die "message here"
Quoted 4 lines
or the inverse logic. This is what automake and popt
autogen.shdoes.Best Regards
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 16 lines · Elia Pinto
2012/9/14 Elia Pinto <[email protected]>:
Quoted 15 lines
2012/9/14 Erik Faye-Lund <[email protected]>:
Quoted 11 lines · Erik Faye-Lund
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh export _x05 _x40 _z40 LF · line 132
Quoted 9 lines
t/test-lib.sh +1
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh +Nice. But this setting should be check that we have a terminal first isn't ? Some test like this before
Quoted 4 lines
Quoted 3 lines
test "X$$TERM" != Xdumb \
&& test -t 1 2>/dev/null \
&& ....and in reality this echo use is not portable.
Yeah; I posted a couple of follow-up mails earlier where I had noticed it and changed to printf instead. It seems the testsuite is already using it, so it's probably portable.
Thanks a lot for the extra set of eyes :)
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
Quoted 15 lines · Elia Pinto
2012/9/14 Erik Faye-Lund <[email protected]>:
Quoted 11 lines · Erik Faye-Lund
Quoted 10 lines · Erik Faye-Lund
t/test-lib.sh export _x05 _x40 _z40 LF · line 132
Quoted 9 lines
t/test-lib.sh +1
-0@@ -129,6 +129,20 @@ export _x05 _x40 _z40 LF # This test checks if command xyzzy does the right thing... # ' # . ./test-lib.sh +Nice. But this setting should be check that we have a terminal first isn't ? Some test like this before
Quoted 5 lines
test "X$$TERM" != Xdumb \
&& test -t 1 2>/dev/null \
&& ....or the inverse logic. This is what automake and popt
autogen.shdoes.
There's already such a check a few lines further down, and tput isn't used in such cases.
Signature · 12 lines
*** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en