scwrypts/zsh/lib/utils/io.zsh

171 lines
4.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#####################################################################
### basic colorized print messages ##################################
#####################################################################
source "${0:a:h}/io.print.zsh"
[ ! $ERRORS ] && ERRORS=0
ERROR() { # command encountered an error
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="ERROR ✖" COLOR=$__RED PRINT "$@"
((ERRORS+=1))
return $ERRORS
}
SUCCESS() { # command completed successfully
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="SUCCESS ✔" COLOR=$__GREEN PRINT "$@"
}
REMINDER() { # include sysadmin reminder or other important notice to users
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="REMINDER " COLOR=$__BRIGHT_MAGENTA PRINT "$@"
}
STATUS() { # general status updates (prefer this to generic 'echo')
[[ $SCWRYPTS_LOG_LEVEL -ge 2 ]] \
&& PREFIX="STATUS " COLOR=$__BLUE PRINT "$@"
}
WARNING() { # warning-level messages; not errors
[[ $SCWRYPTS_LOG_LEVEL -ge 3 ]] \
&& PREFIX="WARNING " COLOR=$__YELLOW PRINT "$@"
}
DEBUG() { # helpful during development or (sparingly) to help others' development
[[ $SCWRYPTS_LOG_LEVEL -gt 4 ]] \
&& PREFIX="DEBUG " COLOR=$__WHITE PRINT "$@"
}
PROMPT() { # you probably want to use yN or INPUT from below
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="PROMPT " COLOR=$__CYAN PRINT "$@" \
&& PREFIX="USER ⌨" COLOR=$__BRIGHT_CYAN PRINT '' --no-line-end \
;
}
FAIL() { SCWRYPTS_LOG_LEVEL=1 ERROR "${@:2}"; exit $1; }
ABORT() { FAIL 69 'user abort'; }
#####################################################################
### check for reported errors and format USAGE contents #############
#####################################################################
CHECK_ERRORS() {
local FAIL_OUT=true
local DISPLAY_USAGE=true
[ ! $ERRORS ] && ERRORS=0
while [[ $# -gt 0 ]]
do
case $1 in
--fail ) FAIL_OUT=true ;;
--no-fail ) FAIL_OUT=false ;;
--usage ) DISPLAY_USAGE=true ;;
--no-usage ) DISPLAY_USAGE=false ;;
esac
shift 1
done
[[ $ERRORS -eq 0 ]] && return 0
[[ $DISPLAY_USAGE =~ true ]] && USAGE
[[ $FAIL_OUT =~ true ]] && exit $ERRORS || return $ERRORS
}
source "${0:a:h}/io.usage.zsh"
#####################################################################
### facilitate user prompt and input ################################
#####################################################################
Yn() { # ask a yes-or-no question (default yes)
PROMPT "$@ [Yn]"
[ $CI ] && { echo y >&2; return 0; }
[ $__SCWRYPTS_YES ] && [[ $__SCWRYPTS_YES -eq 1 ]] && { echo y; return 0; }
local Yn; READ -k Yn; echo >&2
[[ $Yn =~ [nN] ]] && return 1 || return 0
}
yN() { # ask a yes-or-no question (default no)
PROMPT "$@ [yN]"
[ $CI ] && { echo y >&2; return 0; }
[ $__SCWRYPTS_YES ] && [[ $__SCWRYPTS_YES -eq 1 ]] && { echo y; return 0; }
local yN; READ -k yN; echo >&2
[[ $yN =~ [yY] ]] && return 0 || return 1
}
INPUT() { # read a single line of user input
PROMPT "${@:2}"
READ $1
local VALUE=$(eval echo '$'$1)
[ $VALUE ]
}
source "${0:a:h}/io.fzf.zsh" # allow user to select from a list of inputs
EDIT() { # edit a file in user's preferred editor
[ $CI ] && {
INFO 'currently in CI, skipping EDIT'
return 0
}
STATUS "opening '$1' for editing"
$EDITOR $@ </dev/tty >/dev/tty
SUCCESS "finished editing '$1'!"
}
#####################################################################
### basic commands with tricky states or default requirements #######
#####################################################################
LESS() { less -R $@ </dev/tty >/dev/tty; }
YQ() {
yq --version | grep -q mikefarah || {
yq $@
return $?
}
yq eval '... comments=""' | yq $@
}
#####################################################################
### other i/o utilities #############################################
#####################################################################
CAPTURE() {
[ ! $USAGE ] && USAGE="
usage: stdout-varname stderr-varname [...cmd and args...]
captures stdout and stderr on separate variables for a command
"
{
IFS=$'\n' read -r -d '' $2;
IFS=$'\n' read -r -d '' $1;
} < <((printf '\0%s\0' "$(${@:3})" 1>&2) 2>&1)
}
GETSUDO() {
echo "\\033[1;36mPROMPT  : checking sudo password...\\033[0m" >&2
sudo echo hi >/dev/null 2>&1 </dev/tty \
&& SUCCESS '...authenticated!' \
|| { ERROR 'failed :c'; return 1; }
}
READ() {
[ $CI ] && {
INFO 'currently in CI, skipping READ'
return 0
}
read $@ </dev/tty
}