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_debug=0
_sqr_dry=0
_sqr_logLastColor=
_sqr_verbose=0
# Colors
@@ -93,7 +92,6 @@ set -o pipefail
esac
done
_sqr_logLastColor="${log_color}"
[[ -z "${log_color}" ]] && col_end=""
# 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; }
# 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() {
if (( _sqr_debug )) ; then set +o xtrace; else true; fi
}
sqr::debugContinue() {
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
@@ -197,6 +245,16 @@ quiet() {
silent() {
[[ $LOG_LEVEL -eq 0 ]]
}
# dry-run
# Started with --dry-run
dry() {
(( _sqr_dry ))
}
# verbose
# Started with --verbose
verbose() {
(( _sqr_verbose ))
}
### interactive
# confirm [OPTIONS] [--] [QUESTION]
@@ -280,26 +338,58 @@ ask() {
answer="${2:-}"
fi
printf '%s\n' "${answer}"
# return if answer is empty
sqr::debugContinue
if (( ! empty )) ; then
[[ -n "${answer}" ]]
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
for arg in "$@" ; do
case "$1" in
--debug)
_sqr_debug="1"
shift ;;
--dry-run|-d)
_sqr_dry=1
shift ;;
--quiet|-q)
_sqr_interactive=0
shift ;;
-qq)
--silent|-qq)
_sqr_interactive=0
LOG_LEVEL=0
shift ;;
--debug)
_sqr_debug="1"
--verbose|-v)
_sqr_verbose=1
shift ;;
esac
done
@@ -313,7 +403,7 @@ sqr::main() {
fi
sqr::print 'Running...\n'
confirm -y -- 'Continue?'
confirm -y 'Continue?'
seq_config 2>/dev/null || true
step_1 "$@"
}