From d0520bd38b737d1abb75a6454e980a2a86595613 Mon Sep 17 00:00:00 2001 From: "Raphael \"Thom\" Thomazella" Date: Wed, 11 Sep 2024 13:09:14 -0300 Subject: [PATCH] style(lib): color log, err and fatal outputs if T0_COLOR is true --- lib.sh | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lib.sh b/lib.sh index 9979e20..0e02f48 100644 --- a/lib.sh +++ b/lib.sh @@ -15,16 +15,20 @@ # used to check this file has been sourced export LIB_LOADED=true -# ANSI escape codes for specific colors -export LIB_COLOR_PASS="\e[7;38;05;242m PASS \e[0m" # deprecated in favor of LIB_GREEN_PASS -export LIB_COLOR_FAIL="\e[2;7;38;05;197;47m FAIL \e[0m" # deprecated in favor of LIB_RED_FAIL -export LIB_GREEN_PASS="\e[7;38;05;242m PASS \e[0m" -export LIB_RED_FAIL="\e[2;7;38;05;197;47m FAIL \e[0m" - # ANSI escape codes for visual formatting export LIB_VISUAL_END="\e[0m" export LIB_FORMAT_DIM="\e[2m" + +# ANSI escape codes for specific colors export LIB_COLOR_DARK_GRAY="\e[38;05;8m" +export LIB_COLOR_RED="\e[38;05;124m" +export LIB_COLOR_RED_BRIGHT="\e[38;05;197m" + +# ANSI escape codes for specific text +export LIB_COLOR_PASS="\e[7;38;05;242m PASS \e[0m" # deprecated in favor of LIB_TEXT_PASS_GREEN +export LIB_COLOR_FAIL="\e[2;7;38;05;197;47m FAIL \e[0m" # deprecated in favor of LIB_TEXT_FAIL_RED +export LIB_TEXT_PASS_GREEN="\e[7;38;05;242m PASS \e[0m" +export LIB_TEXT_FAIL_RED="\e[2;7;38;05;197;47m FAIL \e[0m" # on most systems, sed is GNU sed export SED="sed" @@ -69,11 +73,16 @@ msgln() { } # Description: Log a message with INFO level and line number +# Globals : T0_COLOR (env) colored output if "true" # Args : Any # STDERR : INFO (pizza.sh:34) message + \n # Example : log $LINENO pizza order received log() { - __log INFO "$@" + if [ "${T0_COLOR:-}" == "true" ]; then + __log "${LIB_COLOR_DARK_GRAY}INFO" "$@" "${LIB_VISUAL_END}" + else + __log INFO "$@" + fi } # Description: Log a message with DEBUG level and line number @@ -94,7 +103,11 @@ debug() { # STDERR : ERROR (pizza.sh:34) message + \n # Example : err $LINENO oven temperature too high err() { - __log ERROR "$@" + if [ "${T0_COLOR:-}" == "true" ]; then + __log "${LIB_COLOR_RED}ERROR" "$@" "${LIB_VISUAL_END}" + else + __log ERROR "$@" + fi } # Description: Calls err with args, then exits with status 1 @@ -102,7 +115,11 @@ err() { # STDERR : FATAL (pizza.sh:34) message + \n # Example : fatal $LINENO we've run out of cheese fatal() { - __log FATAL "$@" + if [ "${T0_COLOR:-}" == "true" ]; then + __log "${LIB_COLOR_RED_BRIGHT}FATAL" "$@" "${LIB_VISUAL_END}" + else + __log FATAL "$@" + fi exit 1 }