kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal
@ 2019-04-10 15:05 Daniel Díaz
  2019-04-10 17:48 ` Andrew Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Díaz @ 2019-04-10 15:05 UTC (permalink / raw)
  To: kvm; +Cc: Daniel Díaz

Instead of colorizing the output of PASS/SKIP/FAIL all the
time, do it only when stdout is plugged to a terminal, which
is generally a good indication that colors will be helpful.

When stdout is instead redirected, say, to a file, it's not
easy to parse the results of the tests. When analyzing and
extracting information out of the logs, it would be better to
strip the color information and just keep the test results.

Signed-off-by: Daniel Díaz <daniel.diaz@linaro.org>
---
 scripts/runtime.bash | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/scripts/runtime.bash b/scripts/runtime.bash
index 200d5b6..d4de6b1 100644
--- a/scripts/runtime.bash
+++ b/scripts/runtime.bash
@@ -2,9 +2,21 @@
 : ${MAX_SMP:=$(getconf _NPROCESSORS_CONF)}
 : ${TIMEOUT:=90s}
 
-PASS() { echo -ne "\e[32mPASS\e[0m"; }
-SKIP() { echo -ne "\e[33mSKIP\e[0m"; }
-FAIL() { echo -ne "\e[31mFAIL\e[0m"; }
+# Define text colors
+color_reset=
+color_red=
+color_green=
+color_yellow=
+# If stdout is plugged to terminal, use colors
+if [ -t 1 ]; then
+    color_reset="\e[0m"
+    color_red="\e[31m"
+    color_green="\e[32m"
+    color_yellow="\e[33m"
+fi
+PASS() { echo -ne "${color_green}PASS${color_reset}"; }
+SKIP() { echo -ne "${color_yellow}SKIP${color_reset}"; }
+FAIL() { echo -ne "${color_red}FAIL${color_reset}"; }
 
 extract_summary()
 {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal
  2019-04-10 15:05 [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal Daniel Díaz
@ 2019-04-10 17:48 ` Andrew Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Jones @ 2019-04-10 17:48 UTC (permalink / raw)
  To: Daniel Díaz; +Cc: kvm

On Wed, Apr 10, 2019 at 10:05:00AM -0500, Daniel Díaz wrote:
> Instead of colorizing the output of PASS/SKIP/FAIL all the
> time, do it only when stdout is plugged to a terminal, which
> is generally a good indication that colors will be helpful.
> 
> When stdout is instead redirected, say, to a file, it's not
> easy to parse the results of the tests. When analyzing and
> extracting information out of the logs, it would be better to
> strip the color information and just keep the test results.
> 
> Signed-off-by: Daniel Díaz <daniel.diaz@linaro.org>
> ---
>  scripts/runtime.bash | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/scripts/runtime.bash b/scripts/runtime.bash
> index 200d5b6..d4de6b1 100644
> --- a/scripts/runtime.bash
> +++ b/scripts/runtime.bash
> @@ -2,9 +2,21 @@
>  : ${MAX_SMP:=$(getconf _NPROCESSORS_CONF)}
>  : ${TIMEOUT:=90s}
>  
> -PASS() { echo -ne "\e[32mPASS\e[0m"; }
> -SKIP() { echo -ne "\e[33mSKIP\e[0m"; }
> -FAIL() { echo -ne "\e[31mFAIL\e[0m"; }
> +# Define text colors
> +color_reset=
> +color_red=
> +color_green=
> +color_yellow=
> +# If stdout is plugged to terminal, use colors
> +if [ -t 1 ]; then
> +    color_reset="\e[0m"
> +    color_red="\e[31m"
> +    color_green="\e[32m"
> +    color_yellow="\e[33m"
> +fi
> +PASS() { echo -ne "${color_green}PASS${color_reset}"; }
> +SKIP() { echo -ne "${color_yellow}SKIP${color_reset}"; }
> +FAIL() { echo -ne "${color_red}FAIL${color_reset}"; }
>  
>  extract_summary()
>  {
> -- 
> 2.17.1
>

Reviewed-by: Andrew Jones <drjones@redhat.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-10 17:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-10 15:05 [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal Daniel Díaz
2019-04-10 17:48 ` Andrew Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).