All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel Díaz" <daniel.diaz@linaro.org>
To: kvm@vger.kernel.org
Cc: "Daniel Díaz" <daniel.diaz@linaro.org>
Subject: [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal
Date: Wed, 10 Apr 2019 10:05:00 -0500	[thread overview]
Message-ID: <20190410150500.14056-1-daniel.diaz@linaro.org> (raw)

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


             reply	other threads:[~2019-04-10 15:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-10 15:05 Daniel Díaz [this message]
2019-04-10 17:48 ` [kvm-unit-tests PATCH v2] scripts: Colorize output only on terminal Andrew Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190410150500.14056-1-daniel.diaz@linaro.org \
    --to=daniel.diaz@linaro.org \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.