From a0f596eaa29856209592bd823750d52faee9225c Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Wed, 4 May 2022 23:01:39 +0200 Subject: [PATCH] WIP - exe(p) and output color --- sequencer.sh | 112 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 101 insertions(+), 11 deletions(-) diff --git a/sequencer.sh b/sequencer.sh index 576a5bc..41a8799 100755 --- a/sequencer.sh +++ b/sequencer.sh @@ -37,7 +37,6 @@ set -o pipefail _sqr_interactive=1 _sqr_debug=0 _sqr_dry=0 - _sqr_logLastColor= _sqr_verbose=0 # Colors @@ -78,7 +77,7 @@ set -o pipefail local appendText= local arg= local col_end="${col_off}" - + local log_level="${1:-}" shift local log_color="${1:-}" @@ -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 [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] @@ -214,7 +272,7 @@ confirm() { local inputHelp='[y/N] ' # default no local noHelp=0 local force=0 - + for arg in "${@}" ; do case "${1:-}" in --) @@ -270,7 +328,7 @@ ask() { esac done local answer= - if [[ -n "${2:-}" ]]; then + if [[ -n "${2:-}" ]] ; then ! interactive && printf '%s\n' "${2}" && sqr::debugContinue && return 0 read ${hidden?} -r -p "${1:-"User input"} ($2) " answer else @@ -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 +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 "$@" }