Skip to content

Commit

Permalink
style(lib): color log, err and fatal outputs if T0_COLOR is true
Browse files Browse the repository at this point in the history
  • Loading branch information
tcodes0 committed Sep 11, 2024
1 parent c5a5629 commit d0520bd
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -94,15 +103,23 @@ 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
# 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
}

Expand Down

0 comments on commit d0520bd

Please sign in to comment.