From bab4ad2d85896e1552f9368584773a19fa223805 Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Tue, 17 Oct 2023 07:16:47 +0200 Subject: [PATCH] Auto-detect if terminal supports colors This enables build_oai to detect if the output terminal supports colors. If it does, it will use colors in its output. If it does not, it will just print the text, without color codes, improving legibility. --- cmake_targets/tools/build_helper | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cmake_targets/tools/build_helper b/cmake_targets/tools/build_helper index 2be24eef76..3028d9e56e 100755 --- a/cmake_targets/tools/build_helper +++ b/cmake_targets/tools/build_helper @@ -54,12 +54,17 @@ fi ############################### ## echo and family ############################### -red='\E[31m' -green='\E[32m' -yellow='\E[33m' -blue='\E[1;34m' -reset_color='\E[00m' -COLORIZE=1 +# only emit color if connected to a terminal, and if it supports colors +if [ -t 1 ]; then + num_colors=$(tput colors) + if [ -n "$num_colors" ] && [ "$num_colors" -ge 8 ]; then + red='\E[31m' + green='\E[32m' + yellow='\E[33m' + blue='\E[1;34m' + reset_color='\E[00m' + fi +fi cecho() { # Color-echo @@ -68,8 +73,7 @@ cecho() { local default_msg="No Message." message=${1:-$default_msg} color=${2:-$green} - [ "$COLORIZE" = "1" ] && message="$color$message$reset_color" - echo -e "$message" + echo -e "$color$message$reset_color" return }