WIP - exe(p) and output color

This commit is contained in:
2022-05-04 23:01:39 +02:00
parent 522294208e
commit a0f596eaa2

View File

@@ -37,7 +37,6 @@ set -o pipefail
_sqr_interactive=1 _sqr_interactive=1
_sqr_debug=0 _sqr_debug=0
_sqr_dry=0 _sqr_dry=0
_sqr_logLastColor=
_sqr_verbose=0 _sqr_verbose=0
# Colors # Colors
@@ -93,7 +92,6 @@ set -o pipefail
esac esac
done done
_sqr_logLastColor="${log_color}"
[[ -z "${log_color}" ]] && col_end="" [[ -z "${log_color}" ]] && col_end=""
# all remaining arguments are to be printed # all remaining arguments are to be printed
@@ -129,13 +127,63 @@ set -o pipefail
debug () { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && sqr::log "dbug" "${col_lightpurple}" "${@}"; true; } debug () { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && sqr::log "dbug" "${col_lightpurple}" "${@}"; true; }
# internal printf same loglevel as info # internal printf same loglevel as info
sqr::print () { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && printf "$@"; true; } # shellcheck disable=SC2059 # don't use variables in format
sqr::print () { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && printf "$@"; true; }
sqr::debugPause() { sqr::debugPause() {
if (( _sqr_debug )) ; then set +o xtrace; else true; fi if (( _sqr_debug )) ; then set +o xtrace; else true; fi
} }
sqr::debugContinue() { sqr::debugContinue() {
if (( _sqr_debug )) ; then set -o xtrace; else true; fi if (( _sqr_debug )) ; then set -o xtrace; else true; fi
} }
# color <FOREGROUND COLOR> [BACKGROUND COLOR]
color() {
[ ! -t 1 ] && return 0
[ -z "${1:-}" ] && tput sgr0 && return 0
case "${1:-}" in
black)
tput setaf 0 ;;
red)
tput setaf 1 ;;
green)
tput setaf 2 ;;
yellow)
tput setaf 3 ;;
blue)
tput setaf 4 ;;
magenta)
tput setaf 5 ;;
cyan)
tput setaf 6 ;;
white)
tput setaf 7 ;;
none)
tput sgr0
return 0 ;;
*)
tput setaf ${1:-} ;;
esac
case "${2:-}" in
black)
tput setab 0 ;;
red)
tput setab 1 ;;
green)
tput setab 2 ;;
yellow)
tput setab 3 ;;
blue)
tput setab 4 ;;
magenta)
tput setab 5 ;;
cyan)
tput setab 6 ;;
white)
tput setab 7 ;;
esac
}
} }
# Traps # Traps
@@ -197,6 +245,16 @@ quiet() {
silent() { silent() {
[[ $LOG_LEVEL -eq 0 ]] [[ $LOG_LEVEL -eq 0 ]]
} }
# dry-run
# Started with --dry-run
dry() {
(( _sqr_dry ))
}
# verbose
# Started with --verbose
verbose() {
(( _sqr_verbose ))
}
### interactive ### interactive
# confirm [OPTIONS] [--] [QUESTION] # confirm [OPTIONS] [--] [QUESTION]
@@ -280,26 +338,58 @@ ask() {
answer="${2:-}" answer="${2:-}"
fi fi
printf '%s\n' "${answer}" printf '%s\n' "${answer}"
# return if answer is empty
sqr::debugContinue sqr::debugContinue
if (( ! empty )) ; then if (( ! empty )) ; then
[[ -n "${answer}" ]] [[ -n "${answer}" ]]
fi fi
} }
sqr::main() { # Handle dry run and verbose output for commands without pipe and/or redirects
exe() {
dry && printf -- '--'
if dry || verbose ; then
(set -x; : "$@")
fi
if ! dry ; then
"$@"
fi
}
# Handle dry run and verbose output for commands containing pipe and/or redirects
# exep <COMMAND AS STRING(S)>
exep() {
if dry ; then
printf -- '--++ : %s\n' "$*"
elif verbose ; then
printf '++ : %s\n' "$*"
fi
if ! dry ; then
bash -c "$*"
fi
}
sqr::main() {
# options check # options check
for arg in "$@" ; do for arg in "$@" ; do
case "$1" in case "$1" in
--debug)
_sqr_debug="1"
shift ;;
--dry-run|-d)
_sqr_dry=1
shift ;;
--quiet|-q) --quiet|-q)
_sqr_interactive=0 _sqr_interactive=0
shift ;; shift ;;
-qq) --silent|-qq)
_sqr_interactive=0 _sqr_interactive=0
LOG_LEVEL=0 LOG_LEVEL=0
shift ;; shift ;;
--debug) --verbose|-v)
_sqr_debug="1" _sqr_verbose=1
shift ;; shift ;;
esac esac
done done
@@ -313,7 +403,7 @@ sqr::main() {
fi fi
sqr::print 'Running...\n' sqr::print 'Running...\n'
confirm -y -- 'Continue?' confirm -y 'Continue?'
seq_config 2>/dev/null || true seq_config 2>/dev/null || true
step_1 "$@" step_1 "$@"
} }