Use tput if available

This commit is contained in:
Héctor Molinero Fernández 2021-03-02 21:07:28 +01:00
commit 1b51a78e53

44
hblock
View file

@ -267,26 +267,10 @@ exists() {
}
# Pretty print methods.
printInfo() {
if [ "${quiet-}" != 'true' ]; then
if [ "${color-}" != 'true' ]; then printf '[INFO] %s\n' "${@-}"
else printf '\033[0m[\033[1;32mINFO\033[0m] %s\n' "${@-}"; fi
fi
}
printWarn() {
if [ "${color-}" != 'true' ]; then printf '[WARN] %s\n' "${@-}" >&2
else printf '\033[0m[\033[1;33mWARN\033[0m] %s\n' "${@-}" >&2; fi
}
printError() {
if [ "${color-}" != 'true' ]; then printf '[ERROR] %s\n' "${@-}" >&2
else printf '\033[0m[\033[1;31mERROR\033[0m] %s\n' "${@-}" >&2; fi
}
printList() {
if [ "${quiet-}" != 'true' ]; then
if [ "${color-}" != 'true' ]; then printf ' * %s\n' "${@-}"
else printf '\033[0m \033[1;36m*\033[0m %s\n' "${@-}"; fi
fi
}
printInfo() { [ -n "${NO_STDOUT+x}" ] || printf "${COLOR_RESET-}[${COLOR_BGREEN-}INFO${COLOR_RESET-}] %s\n" "${@-}"; }
printWarn() { [ -n "${NO_STDERR+x}" ] || printf "${COLOR_RESET-}[${COLOR_BYELLOW-}WARN${COLOR_RESET-}] %s\n" "${@-}" >&2; }
printError() { [ -n "${NO_STDERR+x}" ] || printf "${COLOR_RESET-}[${COLOR_BRED-}ERROR${COLOR_RESET-}] %s\n" "${@-}" >&2; }
printList() { [ -n "${NO_STDOUT+x}" ] || printf "${COLOR_RESET-} ${COLOR_BCYAN-}*${COLOR_RESET-} %s\n" "${@-}"; }
# Print a pseudorandom string.
rand() {
@ -464,13 +448,19 @@ main() {
# shellcheck disable=SC2086
{ optParse "${@-}"; set -- ${posArgs-} >/dev/null; }
if [ "${color:?}" = 'auto' ]; then
# Check color support, but honor ${NO_COLOR} variable (https://no-color.org).
if [ -t 1 ] && [ -z "${NO_COLOR+x}" ]; then
color='true'
else
color='false'
fi
# Define terminal colors if the color option is enabled or in auto mode if STDOUT is attached to a TTY and the
# "NO_COLOR" variable is not set (https://no-color.org).
if [ "${color:?}" = 'true' ] || { [ "${color:?}" = 'auto' ] && [ -z "${NO_COLOR+x}" ] && [ -t 1 ]; }; then
COLOR_RESET="$({ exists tput && tput sgr0; } 2>/dev/null || printf '\033[0m')"
COLOR_BRED="$({ exists tput && tput bold && tput setaf 1; } 2>/dev/null || printf '\033[1;31m')"
COLOR_BGREEN="$({ exists tput && tput bold && tput setaf 2; } 2>/dev/null || printf '\033[1;32m')"
COLOR_BYELLOW="$({ exists tput && tput bold && tput setaf 3; } 2>/dev/null || printf '\033[1;33m')"
COLOR_BCYAN="$({ exists tput && tput bold && tput setaf 6; } 2>/dev/null || printf '\033[1;36m')"
fi
# Set "NO_STDOUT" variable if the quiet option is enabled (other methods will honor this variable).
if [ "${quiet:?}" = 'true' ]; then
NO_STDOUT='true'
fi
# Check the header file.