From 56234e23319a71d96e5a105b98c81cd7af1316b0 Mon Sep 17 00:00:00 2001 From: "Raphael \"Thom\" Thomazella" Date: Wed, 11 Sep 2024 16:17:34 +0000 Subject: [PATCH] style(lib): color logs if T0_COLOR true & misc (#22) * feat(lib): add debug log func with color option via env T0_COLOR * misc(lib): introduce LIB_GREEN_PASS and LIB_RED_FAIL * misc: rename cmds to t0 prefix * misc: lint fix * style(lib): color log, err and fatal outputs if T0_COLOR is true * docs(lib): document global vars --- .github/workflows/release_pr.yml | 2 +- lib.sh | 49 +++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/.github/workflows/release_pr.yml b/.github/workflows/release_pr.yml index ea9ded3..10304fb 100644 --- a/.github/workflows/release_pr.yml +++ b/.github/workflows/release_pr.yml @@ -36,7 +36,7 @@ jobs: uses: actions/setup-go@v5.0.2 with: go-version: 1.23 - - run: go install github.com/tcodes0/go/cmd/changelog@latest + - run: go install github.com/tcodes0/go/cmd/t0changelog@latest - name: Update changelog shell: bash diff --git a/lib.sh b/lib.sh index 20a3b4e..746487b 100644 --- a/lib.sh +++ b/lib.sh @@ -15,14 +15,21 @@ # 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" -export LIB_COLOR_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" @@ -66,27 +73,55 @@ 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 +# Globals : T0_COLOR (env) colored output if "true" +# Args : Any +# STDERR : INFO (pizza.sh:34) message + \n +# Example : log $LINENO pizza order received +debug() { + if [ "${T0_COLOR:-}" == "true" ]; then + __log "${LIB_COLOR_DARK_GRAY}DEBUG" "$@" "${LIB_VISUAL_END}" + else + __log DEBUG "$@" + fi } # Description: Log a message with ERROR level and line number +# Globals : T0_COLOR (env) colored output if "true" # Args : Any # 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 +# Globals : T0_COLOR (env) colored output if "true" # Args : Any # 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 }