Skip to contentSkip to conversation

scripts: Colorize only when possible

PATCH In review Daniel Díaz 2 participants 3 messages Apr 9, 2019 – Apr 10, 2019 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: Colorize only when possible

Instead of colorizing the output of PASS/SKIP/FAIL all the time, do it only when the terminal supports colors and stdout is opened on a terminal.

---
 scripts/runtime.bash | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
scripts/runtime.bash +17 -3
index 200d5b6..0c38787 100644
@@ -2,9 +2,23 @@
 : ${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
+# Check available colors on the terminal, if any
+ncolors=`tput colors 2>/dev/null`
+color_reset=
+color_red=
+color_green=
+color_yellow=
+# If stdout exists and number of colors is eight or more, use them
+if [ -t 1 -a "$ncolors" -a "$ncolors" -ge 8 ]; 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()
 {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · Daniel Díaz

Instead of colorizing the output of PASS/SKIP/FAIL all the time, do it only when the terminal supports colors and stdout is opened on a terminal.

I'm guessing the motivation for this is because you're redirecting the output of run_tests.sh or the standalone scripts and getting something like

^[[32mPASS^[[0m selftest-vectors-kernel (2 tests)

in the output, which doesn't look nice nor lend itself well to parsing. Because otherwise you must be using a pretty old vt100 :) If I'm right about the motivation then we mostly care about the '[ -t 1]' part and we can drop the 'tput' stuff in order to avoid the ncurses dependency. Also that motivation should be added to the commit message.

Quoted 21 lines
---
 scripts/runtime.bash | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
scripts/runtime.bash +4 -3
index 200d5b6..0c38787 100644
@@ -2,9 +2,23 @@
 : ${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
+# Check available colors on the terminal, if any
+ncolors=`tput colors 2>/dev/null`
+color_reset=

scripts/runtime.bash line 18

Quoted 10 lines
scripts/runtime.bash +10 -0
+color_red=
+color_green=
+color_yellow=
+# If stdout exists and number of colors is eight or more, use them
+if [ -t 1 -a "$ncolors" -a "$ncolors" -ge 8 ]; then
+  color_reset="\e[0m"
+  color_red="\e[31m"
+  color_green="\e[32m"
+  color_yellow="\e[33m"
+fi

Some bash style nits:

- Please use $() instead of ``
- Please use if [ cond1 ] && [ cond2 ] && ... instead of '-a'
- This file uses four spaces for indentation.

Quoted 9 lines
scripts/runtime.bash +3 -0
+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()
 {
Signature · 1 line
2.17.1

Thanks,
drew

#2 lore raw

Hello!

Quoted 5 lines · Andrew Jones
Quoted 3 lines · Daniel Díaz

Instead of colorizing the output of PASS/SKIP/FAIL all the time, do it only when the terminal supports colors and stdout is opened on a terminal.

Quoted 7 lines

I'm guessing the motivation for this is because you're redirecting the output of run_tests.sh or the standalone scripts and getting something like

^[[32mPASS^[[0m selftest-vectors-kernel (2 tests)

in the output, which doesn't look nice nor lend itself well to parsing.

That's the case, yes [1]. We [2] come from the context of parsing the logs and extracting the test results, and, while doable [3], color sequences generally get in the way of easily parsing the output.

Quoted 4 lines

Because otherwise you must be using a pretty old vt100 :) If I'm right about the motivation then we mostly care about the '[ -t 1]' part and we can drop the 'tput' stuff in order to avoid the ncurses dependency. Also that motivation should be added to the commit message.

[...]

Quoted 5 lines

Some bash style nits:

- Please use $() instead of ``
- Please use if [ cond1 ] && [ cond2 ] && ... instead of '-a'
- This file uses four spaces for indentation.

Great! Thanks for the review. I'll send V2 briefly.

Greetings!

Daniel Díaz
[email protected]

[1] https://lkft.validation.linaro.org/scheduler/job/673625#L1462
[2] https://lkft.linaro.org/
[3] https://lkft.validation.linaro.org/results/673625/1_kvm-unit-tests