git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Add some fancy colors in the test library when terminal supports it.

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Oct 22, 2007, 08:53 UTC
Message-ID
<471C6510.8010300@viscovery.net>
In-Reply-To
<20071022081341.GC32763@artemis.corp>
Pierre Habouzit schrieb:
Show 35 quoted lines
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
> 
> Maybe this is just me, but I don't find the output of the test-suite
> easy to watch while scrolling. This puts some colors in proper places.
> 
>   * end-test summaries are in green or red depending on the sucess of
>     the tests.
>   * errors are in red.
>   * skipped tests and other things that tests `say` are in brown (now
>     you can _see_ that your testsuite skips some tests on purpose, I
>     only noticed recently that I missed part of the environment for
>     proper testing).
> 
> I'm not 100% sure the test to see if terminal supports color is correct, and
> people using emacs shell buffer or alike tools may have better ideas on how to
> make it.
> 
> and yes, I know that it "depends" upon tput, but if tput isn't available, the
>     [ "x$TERM" != "xdumb" ] && tput hpa 60 >/dev/null 2>&1 && tput setaf 1 >/dev/null 2>&1
> expression will fail, and color will be disabled.
> 
>  t/test-lib.sh |   32 ++++++++++++++++++++++----------
>  1 files changed, 22 insertions(+), 10 deletions(-)
> 
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index cc1253c..c6521c0 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -59,14 +59,24 @@ esac
>  # '
>  # . ./test-lib.sh
>  
> +[ "x$TERM" != "xdumb" ] && tput hpa 60 >/dev/null 2>&1 && tput setaf 1 >/dev/null 2>&1
> +nocolor=$?
test "x$TERM" != "xdumb" &&
	tput hpa 60 >/dev/null 2>&1 &&
	tput setaf 1 >/dev/null 2>&1 &&
	color=t
BTW, doesn't tput fail if stdout/stderr is not a terminal, like above?
Show 7 quoted lines
> +
> +say_color () {
> +	[ "$nocolor" = 0 ] &&  [ "$1" != '-1' ] && tput setaf "$1"
> +	shift
> +	echo "* $*"
> +	tput op
> +}

What if tput is not available, like on Windows? How about this (at the end of the file, so it can obey --no-color):

if test "$color"; then
	say_color () {
		test "$1" != '-1' && tput setaf "$1"
		shift
		echo "* $*"
		tput op
	}
else
	say_color() {
		shift
		echo "* $*"
	}
fi
> +	--no-color)
> +	    nocolor=1; shift ;;
	    color=; shift ;;
-- Hannes "We don't need no double negation"
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 2 of 10 in “Add some fancy colors in the test library when terminal supports it.”
  1. Add some fancy colors in the test library when terminal supports it.Pierre Habouzit, Oct 22, 2007
  2. Johannes SixtOct 22, 2007
  3. Pierre HabouzitOct 22, 2007
  4. Johannes SixtOct 22, 2007
  5. Pierre HabouzitOct 22, 2007
  6. Johannes SixtOct 22, 2007
  7. Pierre HabouzitOct 22, 2007
  8. Christian CouderOct 23, 2007
  9. Johannes SixtOct 23, 2007
  10. Pierre HabouzitOct 23, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.