diff --git a/helpers/debug.sh b/helpers/debug.sh index 43e2101..c63c3c3 100644 --- a/helpers/debug.sh +++ b/helpers/debug.sh @@ -1,26 +1,3 @@ #!/usr/bin/env bash source $( dirname $(readlink -e -- "${BASH_SOURCE}"))/io.sh || exit 255 -######################################################## -# Functions to debug scripts -######################################################## - -var_dump() { - debug "$1 = ${!1}" -} - -print_stacktrace() { - STACK="" - local i - local stack_size=${#FUNCNAME[@]} - debug "Callstack:" - # for (( i=$stack_size-1; i>=1; i-- )); do - for (( i=1; i<$stack_size; i++ )); do - local func="${FUNCNAME[$i]}" - [ x$func = x ] && func=MAIN - local linen="${BASH_LINENO[$(( i - 1 ))]}" - local src="${BASH_SOURCE[$i]}" - [ x"$src" = x ] && src=non_file_source - debug " at $func $src:$linen" - done -} diff --git a/helpers/io.sh b/helpers/io.sh index 063b866..724dadb 100644 --- a/helpers/io.sh +++ b/helpers/io.sh @@ -7,93 +7,132 @@ which tput > /dev/null 2>&1 && [[ $(tput -T$TERM colors) -gt 8 ]] && CAN_USE_COLORS=1 || CAN_USE_COLORS=0 USE_COLORS=${USE_COLORS:-$CAN_USE_COLORS} -[ $USE_COLORS = 1 ] && IINFO="( i )" || IINFO='' -[ $USE_COLORS = 1 ] && INOTE="( * )" || INOTE='' -[ $USE_COLORS = 1 ] && IWARN="( # )" || IWARN='' -[ $USE_COLORS = 1 ] && IERROR="( ! )" || IERROR='' -[ $USE_COLORS = 1 ] && IFATAL="( @ )" || IFATAL='' -[ $USE_COLORS = 1 ] && ISUCCESS="( ! )" || ISUCCESS='' -[ $USE_COLORS = 1 ] && IASK="( ? )" || IASK='' -[ $USE_COLORS = 1 ] && IDEBUG="(DBG)" || IDEBUG='' -[ $USE_COLORS = 1 ] && IVRB="( + )" || IVRB='' +# Icons (message prefixes) +[[ "$USE_COLORS" == 1 ]] && IINFO="( i )" || IINFO='' +[[ "$USE_COLORS" == 1 ]] && INOTE="( * )" || INOTE='' +[[ "$USE_COLORS" == 1 ]] && IWARN="( # )" || IWARN='' +[[ "$USE_COLORS" == 1 ]] && IERROR="( ! )" || IERROR='' +[[ "$USE_COLORS" == 1 ]] && IFATAL="( @ )" || IFATAL='' +[[ "$USE_COLORS" == 1 ]] && ISUCCESS="( ! )" || ISUCCESS='' +[[ "$USE_COLORS" == 1 ]] && IASK="( ? )" || IASK='' +[[ "$USE_COLORS" == 1 ]] && IDEBUG="(DBG)" || IDEBUG='' +[[ "$USE_COLORS" == 1 ]] && IVRB="( + )" || IVRB='' -[ $USE_COLORS = 1 ] && BOLD="\e[1m" || BOLD='' -[ $USE_COLORS = 1 ] && DIM="\e[2m" || DIM='' -[ $USE_COLORS = 1 ] && NOTBOLD="\e[22m" || NOTBOLD='' # sometimes \e[21m -[ $USE_COLORS = 1 ] && NOTDIM="\e[22m" || NOTDIM='' -[ $USE_COLORS = 1 ] && NORMAL="\e[20m" || NORMAL='' -[ $USE_COLORS = 1 ] && RESET="\e[0m" || RESET='' +# Text attributes +[[ "$USE_COLORS" == 1 ]] && FRESET="$(tput sgr0)" || FRESET='' # Normal +[[ "$USE_COLORS" == 1 ]] && FBOLD="$(tput bold)" || FBOLD='' # Bold +[[ "$USE_COLORS" == 1 ]] && FDIM="$(tput dim)" || FDIM='' # Dimmed +[[ "$USE_COLORS" == 1 ]] && FLINE="$(tput smul)" || FLINE='' # Underlined +[[ "$USE_COLORS" == 1 ]] && FENDLINE="$(tput rmul)" || FENDLINE='' # End of underlined +[[ "$USE_COLORS" == 1 ]] && FBLINK="$(tput blink)" || FBLINK='' # Blink +[[ "$USE_COLORS" == 1 ]] && FREV="$(tput rev)" || FREV='' # Reversed -[ $USE_COLORS = 1 ] && FRESET="\e[39m" || FRESET='' -[ $USE_COLORS = 1 ] && FBLACK="\e[30m" || FBLACK='' -[ $USE_COLORS = 1 ] && FWHITE="\e[97m" || FWHITE='' -[ $USE_COLORS = 1 ] && FRED="\e[31m" || FRED='' -[ $USE_COLORS = 1 ] && FGREEN="\e[32m" || FGREEN='' -[ $USE_COLORS = 1 ] && FYELLOW="\e[33m" || FYELLOW='' -[ $USE_COLORS = 1 ] && FBLUE="\e[34m" || FBLUE='' -[ $USE_COLORS = 1 ] && FLRED="\e[91m" || FLRED='' -[ $USE_COLORS = 1 ] && FLGREEN="\e[92m" || FLGREEN='' -[ $USE_COLORS = 1 ] && FLYELLOW="\e[93m" || FLYELLOW='' -[ $USE_COLORS = 1 ] && FLBLUE="\e[94m" || FLBLUE='' +# Text colors - normal +[[ "$USE_COLORS" == 1 ]] && FBLACK="$(tput setaf 0)" || FBLACK='' # Black +[[ "$USE_COLORS" == 1 ]] && FRED="$(tput setaf 1)" || FRED='' # Red +[[ "$USE_COLORS" == 1 ]] && FGREEN="$(tput setaf 2)" || FGREEN='' # Green +[[ "$USE_COLORS" == 1 ]] && FYELLOW="$(tput setaf 3)" || FYELLOW='' # Yellow +[[ "$USE_COLORS" == 1 ]] && FBLUE="$(tput setaf 4)" || FBLUE='' # Blue +[[ "$USE_COLORS" == 1 ]] && FPURPLE="$(tput setaf 5)" || FPURPLE='' # Purple +[[ "$USE_COLORS" == 1 ]] && FCYAN="$(tput setaf 6)" || FCYAN='' # Cyan +[[ "$USE_COLORS" == 1 ]] && FWHITE="$(tput setaf 7)" || FWHITE='' # White -[ $USE_COLORS = 1 ] && BRESET="\e[49m" || BRESET='' -[ $USE_COLORS = 1 ] && BBLACK="\e[40m" || BBLACK='' -[ $USE_COLORS = 1 ] && BWHITE="\e[107m" || BWHITE='' -[ $USE_COLORS = 1 ] && BRED="\e[41m" || BRED='' -[ $USE_COLORS = 1 ] && BGREEN="\e[42m" || BGREEN='' -[ $USE_COLORS = 1 ] && BYELLOW="\e[43m" || BYELLOW='' -[ $USE_COLORS = 1 ] && BBLUE="\e[44m" || BBLUE='' -[ $USE_COLORS = 1 ] && BLRED="\e[101m" || BLRED='' -[ $USE_COLORS = 1 ] && BLGREEN="\e[102m" || BLGREEN='' -[ $USE_COLORS = 1 ] && BLYELLOW="\e[103m" || BLYELLOW='' -[ $USE_COLORS = 1 ] && BLBLUE="\e[104m" || BLBLUE='' +# Text colors - bright +[[ "$USE_COLORS" == 1 ]] && FLBLACK="$(tput setaf 8)" || FLBLACK='' # Black +[[ "$USE_COLORS" == 1 ]] && FLRED="$(tput setaf 9)" || FLRED='' # Red +[[ "$USE_COLORS" == 1 ]] && FLGREEN="$(tput setaf 10)" || FLGREEN='' # Green +[[ "$USE_COLORS" == 1 ]] && FLYELLOW="$(tput setaf 11)" || FLYELLOW='' # Yellow +[[ "$USE_COLORS" == 1 ]] && FLBLUE="$(tput setaf 12)" || FLBLUE='' # Blue +[[ "$USE_COLORS" == 1 ]] && FLPURPLE="$(tput setaf 13)" || FLPURPLE='' # Purple +[[ "$USE_COLORS" == 1 ]] && FLCYAN="$(tput setaf 14)" || FLCYAN='' # Cyan +[[ "$USE_COLORS" == 1 ]] && FLWHITE="$(tput setaf 15)" || FLWHITE='' # White -dt() { +# Background colors - normal +[[ "$USE_COLORS" == 1 ]] && FBBLACK="$(tput setab 0)" || FBBLACK='' # Black +[[ "$USE_COLORS" == 1 ]] && FBRED="$(tput setab 1)" || FBRED='' # Red +[[ "$USE_COLORS" == 1 ]] && FBGREEN="$(tput setab 2)" || FBGREEN='' # Green +[[ "$USE_COLORS" == 1 ]] && FBYELLOW="$(tput setab 3)" || FBYELLOW='' # Yellow +[[ "$USE_COLORS" == 1 ]] && FBBLUE="$(tput setab 4)" || FBBLUE='' # Blue +[[ "$USE_COLORS" == 1 ]] && FBPURPLE="$(tput setab 5)" || FBPURPLE='' # Purple +[[ "$USE_COLORS" == 1 ]] && FBCYAN="$(tput setab 6)" || FBCYAN='' # Cyan +[[ "$USE_COLORS" == 1 ]] && FBWHITE="$(tput setab 7)" || FBWHITE='' # White + +# Background colors - bright +[[ "$USE_COLORS" == 1 ]] && FBLBLACK="$(tput setab 8)" || FBLBLACK='' # Black +[[ "$USE_COLORS" == 1 ]] && FBLRED="$(tput setab 9)" || FBLRED='' # Red +[[ "$USE_COLORS" == 1 ]] && FBLGREEN="$(tput setab 10)" || FBLGREEN='' # Green +[[ "$USE_COLORS" == 1 ]] && FBLYELLOW="$(tput setab 11)" || FBLYELLOW='' # Yellow +[[ "$USE_COLORS" == 1 ]] && FBLBLUE="$(tput setab 12)" || FBLBLUE='' # Blue +[[ "$USE_COLORS" == 1 ]] && FBLPURPLE="$(tput setab 13)" || FBLPURPLE='' # Purple +[[ "$USE_COLORS" == 1 ]] && FBLCYAN="$(tput setab 14)" || FBLCYAN='' # Cyan +[[ "$USE_COLORS" == 1 ]] && FBLWHITE="$(tput setab 15)" || FBLWHITE='' # White + +now() { echo "[$(date +'%H:%M:%S')] " } ask() { - IFS= read -rp "$(print ${BOLD}${BBLUE}${FWHITE}${IASK}${BRESET}\ ${BOLD}$1 ): " $2 + IFS= read -rp "$(print ${FBOLD}${FBBLUE}${FWHITE}${IASK}${FRESET}\ ${FBOLD}$1 ): " $2 } print() { - echo -e "$*${RESET}" + echo -e "$*${FRESET}" } debug() { if [ "$2" ]; then - print "${DIM}${BOLD}${RESET}${DIM}$(dt)${IDEBUG} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " >&2 + print "${FDIM}${FBOLD}${FRESET}${FDIM}$(now)${IDEBUG} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " >&2 else - print "${DIM}${BOLD}${RESET}${DIM}$(dt)${IDEBUG} $1 " >&2 + print "${FDIM}${FBOLD}${FRESET}${FDIM}$(now)${IDEBUG} $1 " >&2 fi } +var_dump() { + debug "$1 = ${!1}" +} + +print_stacktrace() { + STACK="" + local i + local stack_size=${#FUNCNAME[@]} + debug "Callstack:" + # for (( i=$stack_size-1; i>=1; i-- )); do + for (( i=1; i<$stack_size; i++ )); do + local func="${FUNCNAME[$i]}" + [ x$func = x ] && func=MAIN + local linen="${BASH_LINENO[$(( i - 1 ))]}" + local src="${BASH_SOURCE[$i]}" + [ x"$src" = x ] && src=non_file_source + debug " at $func $src:$linen" + done +} + verbose() { - print "${BOLD}$(dt)${IVRB}${RESET}${FYELLOW} $1 " + print "${FBOLD}$(now)${IVRB}${FRESET}${FYELLOW} $1 " } info() { - print "${BOLD}$(dt)${FWHITE}${BLBLUE}${IINFO}${RESET}${FWHITE} $1 " + print "${FBOLD}$(now)${FWHITE}${FBLBLUE}${IINFO}${FRESET}${FWHITE} $1 " } note() { - print "${BOLD}$(dt)${DIM}${FWHITE}${INOTE}${RESET} $1 " + print "${FBOLD}$(now)${FDIM}${FWHITE}${INOTE}${FRESET} $1 " } success() { - print "${BOLD}$(dt)${BGREEN}${FWHITE}${ISUCCESS}${BRESET}$FGREEN $1 " + print "${FBOLD}$(now)${FBGREEN}${FWHITE}${ISUCCESS}${FRESET}$FGREEN $1 " } warn() { - print "${BOLD}$(dt)${BYELLOW}${FBLACK}${IWARN}${BRESET}${FYELLOW} Warning:${RESET} $1 " + print "${FBOLD}$(now)${FBYELLOW}${FBLACK}${IWARN}${FRESET}${FYELLOW} Warning:${FRESET} $1 " } error() { - print "${BOLD}$(dt)${BLRED}${FWHITE}${IERROR} Error: ${BRESET}${FLRED} $1 " >&2 + print "${FBOLD}$(now)${FBLRED}${FWHITE}${IERROR} Error: ${FRESET}${FLRED} $1 " >&2 } fatal() { - print "${BOLD}$(dt)${BRED}${FWHITE}${IFATAL} FATAL: $1 " >&2 + print "${FBOLD}$(now)${FBRED}${FWHITE}${IFATAL} FATAL: $1 " >&2 print_stacktrace } @@ -102,24 +141,24 @@ die() { exit ${2:-255} } -# var='test var_dump' -# var_dump var -# debug 'test debug' -# verbose 'test verbose' -# info 'test info' -# note 'test note' -# success 'test success' -# warn 'test warn' -# error 'test error' -# fatal 'test fatal' -# die 'test die' +var='test var_dump' +var_dump var +debug 'test debug' +verbose 'test verbose' +info 'test info' +note 'test note' +success 'test success' +warn 'test warn' +error 'test error' +fatal 'test fatal' +die 'test die' # experiments ============================================================================== # https://unix.stackexchange.com/a/269085 # https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit # https://linuxcommand.org/lc3_adv_tput.php -# https://gist.github.com/dtmilano/4055d6df5b6e4ea87c5a72dc2d604193 +# https://gist.github.com/nowmilano/4055d6df5b6e4ea87c5a72dc2d604193 # https://gist.github.com/nick3499/402a6d7dccd474f2bdb679f4311b1238 # https://gist.github.com/connorjan/2b02126868157c2b69f9aa0a052cdc86 @@ -143,53 +182,6 @@ die() { # =========== -# Text color - normal -# fgblack="$(tput setaf 0)" # Black -# fgred="$(tput setaf 1)" # Red -# fggreen="$(tput setaf 2)" # Green -# fgyellow="$(tput setaf 3)" # Yellow -# fgblue="$(tput setaf 4)" # Blue -# fgpurple="$(tput setaf 5)" # Purple -# fgcyan="$(tput setaf 6)" # Cyan -# fgwhite="$(tput setaf 7)" # White - -# # Text color - bright -# bfgblack="$(tput setaf 8)" # Black -# bfgred="$(tput setaf 9)" # Red -# bfggreen="$(tput setaf 10)" # Green -# bfgyellow="$(tput setaf 11)" # Yellow -# bfgblue="$(tput setaf 12)" # Blue -# bfgpurple="$(tput setaf 13)" # Purple -# bfgcyan="$(tput setaf 14)" # Cyan -# bfgwhite="$(tput setaf 15)" # White - -# # Background color - normal -# bgblack="$(tput setab 0)" # Black - Background -# bgred="$(tput setab 1)" # Red -# bggreen="$(tput setab 2)" # Green -# bgyellow="$(tput setab 3)" # Yellow -# bgblue="$(tput setab 4)" # Blue -# bgpurple="$(tput setab 5)" # Purple -# bgcyan="$(tput setab 6)" # Cyan -# bgwhite="$(tput setab 7)" # White - -# # Background color - bright -# bbgblack="$(tput setab 8)" # Black - Background - Bright -# bbgred="$(tput setab 9)" # Red -# bbggreen="$(tput setab 10)" # Green -# bbgyellow="$(tput setab 11)" # Yellow -# bbgblue="$(tput setab 12)" # Blue -# bbgpurple="$(tput setab 13)" # Purple -# bbgcyan="$(tput setab 14)" # Cyan -# bbgwhite="$(tput setab 15)" # White - -# # Other attributes -# normal="$(tput sgr0)" # text reset -# bold="$(tput bold)" # make bold -# underline="$(tput smul)" # underline -# nounderlin="$(tput rmul)" # remove underline -# mkblink="$(tput blink)" # make blink -# reverse="$(tput rev)" # reverse # ===========