Skip to content

Commit

Permalink
style(lib): color logs if T0_COLOR true & misc (#22)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tcodes0 authored Sep 11, 2024
1 parent 97f492e commit 56234e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: actions/[email protected]
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
Expand Down
49 changes: 42 additions & 7 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 56234e2

Please sign in to comment.