Skip to content

Commit

Permalink
feat(debug): Add color to PS4 prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilwashere committed Jun 27, 2019
1 parent 7e61bce commit 12803e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
17 changes: 11 additions & 6 deletions example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ __b3bp_err_report() {
### Command-line argument switches (like -d for debugmode, -h for showing helppage)
##############################################################################

# no color mode
if [[ "${arg_n:?}" = "1" ]]; then
NO_COLOR="true"
fi

# debug mode
if [[ "${arg_d:?}" = "1" ]]; then
set -o xtrace
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
LOG_LEVEL="7"
if [[ "${NO_COLOR:-}" = "true" ]]; then
# [!] Don't change to double quotes
export PS4='+\011 \t ${BASH_SOURCE}:${LINENO} \011 ${FUNCNAME[0]:+${FUNCNAME[0]}:\011 }'
else
export PS4='+\011 \e[1;30m\t \e[1;34m${BASH_SOURCE}\e[0m:\e[1;36m${LINENO}\e[0m \011 ${FUNCNAME[0]:+\e[0;35m${FUNCNAME[0]}\e[1;30m()\e[0m:\011 }'
fi
# Enable error backtracing
trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
fi
Expand All @@ -86,11 +96,6 @@ if [[ "${arg_v:?}" = "1" ]]; then
set -o verbose
fi

# no color mode
if [[ "${arg_n:?}" = "1" ]]; then
NO_COLOR="true"
fi

# help mode
if [[ "${arg_h:?}" = "1" ]]; then
# Help exists with code 1
Expand Down
38 changes: 30 additions & 8 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fi
__dir="$(cd "$(dirname "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")"
__base="$(basename "${__file}" .sh)"
__invocation="$(printf %q "${__file}")$((($#)) && printf ' %q' "$@" || true)"
# shellcheck disable=SC2034
__invocation="$( printf %q "${__file}")$( { (($#)) && printf ' %q' "$@"; } || true)"

# Define the environment variables (and their defaults) that this script depends on
LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
Expand Down Expand Up @@ -83,7 +84,7 @@ function __b3bp_log () {
local color="${!colorvar:-${color_error}}"
local color_reset="\\x1b[0m"

if [[ "${NO_COLOR:-}" = "true" ]] || ( [[ "${TERM:-}" != "xterm"* ]] && [[ "${TERM:-}" != "screen"* ]] ) || [[ ! -t 2 ]]; then
if [[ "${NO_COLOR:-}" = "true" ]] || { [[ "${TERM:-}" != "xterm"* ]] && [[ "${TERM:-}" != "screen"* ]]; } || [[ ! -t 2 ]]; then
if [[ "${NO_COLOR:-}" != "false" ]]; then
# Don't use colors on pipes or non-recognized terminals
color=""; color_reset=""
Expand Down Expand Up @@ -336,11 +337,37 @@ __b3bp_err_report() {
### Command-line argument switches (like -d for debugmode, -h for showing helppage)
##############################################################################

# no color mode
if [[ "${arg_n:?}" = "1" ]]; then
NO_COLOR="true"
fi

# debug mode
if [[ "${arg_d:?}" = "1" ]]; then
set -o xtrace
PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
LOG_LEVEL="7"

### PS4 - Debug prompt string (when using `set -o xtrace`)
##############################################################################

# When debugging a shell script via `set -x` or `set -o xtrace`
# this tricked-out prompt is used.
#
# The first character (+) is used and repeated for stack depth (subshells).
# Then insert a tab (\011), and log the current time (\t), (source)filename,
# line number and function name
# And finally, the actual line of code.

#+ 15:09:25 ./main.sh:299 main(): help 'Option -f (--file) requires an argument'
#+ 15:09:25 ./main.sh:116 help(): echo ''

if [[ "${NO_COLOR:-}" = "true" ]]; then
# [!] Don't change to double quotes
export PS4='+\011 \t ${BASH_SOURCE}:${LINENO} \011 ${FUNCNAME[0]:+${FUNCNAME[0]}:\011 }'
else
# the 'debug-so-fancy' coloured version. no double quotes here either
export PS4='+\011 \e[1;30m\t \e[1;34m${BASH_SOURCE}\e[0m:\e[1;36m${LINENO}\e[0m \011 ${FUNCNAME[0]:+\e[0;35m${FUNCNAME[0]}\e[1;30m()\e[0m:\011 }'
fi
# Enable error backtracing
trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
fi
Expand All @@ -350,11 +377,6 @@ if [[ "${arg_v:?}" = "1" ]]; then
set -o verbose
fi

# no color mode
if [[ "${arg_n:?}" = "1" ]]; then
NO_COLOR="true"
fi

# help mode
if [[ "${arg_h:?}" = "1" ]]; then
# Help exists with code 1
Expand Down

0 comments on commit 12803e5

Please sign in to comment.