From 49e98dedc9ebb87a1072e1166e01769e960af15f 2020-06-29 04:18:48
From: Branko Majic <branko@majic.rs>
Date: 2020-06-29 04:18:48
Subject: [PATCH] SCR-4: Added support to the Bash script template for coloured message output using utility functions.

---

diff --git a/templates/TEMPLATE.sh.tpl b/templates/TEMPLATE.sh.tpl
index aee7889f35a54459c3147027b98ff99b0174d0bd..cbd81235e9102173cc644ff2901e8b0da18538ff 100644
--- a/templates/TEMPLATE.sh.tpl
+++ b/templates/TEMPLATE.sh.tpl
@@ -63,6 +63,48 @@ $program, version $version
 EOF
 }
 
+# Set-up colours for message printing if we're not piping and terminal is
+# capable of outputting the colors.
+_color_terminal=$(tput colors 2>&1)
+if [[ -t 1 ]] && (( ${_color_terminal} > 0 )); then
+    _text_bold=$(tput bold)
+    _text_white=$(tput setaf 7)
+    _text_blue=$(tput setaf 6)
+    _text_green=$(tput setaf 2)
+    _text_yellow=$(tput setaf 3)
+    _text_red=$(tput setaf 1)
+    _text_reset=$(tput sgr0)
+else
+    _text_bold=""
+    _text_white=""
+    _text_blue=""
+    _text_green=""
+    _text_yellow=""
+    _text_red=""
+    _text_reset=""
+fi
+
+# Set-up functions for printing coloured messages.
+function debug() {
+    echo "${_text_bold}${_text_blue}[DEBUG]${_text_reset}" "$@"
+}
+
+function info() {
+    echo "${_text_bold}${_text_white}[INFO] ${_text_reset}" "$@"
+}
+
+function success() {
+    echo "${_text_bold}${_text_green}[OK]   ${_text_reset}" "$@"
+}
+
+function warning() {
+    echo "${_text_bold}${_text_yellow}[WARN] ${_text_reset}" "$@"
+}
+
+function error() {
+    echo "${_text_bold}${_text_red}[ERROR]${_text_reset}" "$@" >&2
+}
+
 # If no arguments were given, just show usage help.
 if [[ -z $1 ]]; then
     usage