Last active
April 6, 2022 02:50
-
-
Save 27Bslash6/ffa9cfb92c25ef27cad2900c74e2f6dc to your computer and use it in GitHub Desktop.
Small framework for formatting output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -ea | |
# ---------------------------------------------------------------------------- | |
# Pretty printing | |
TERM="${TERM:-'dumb'}" | |
if test -t 1 | |
then | |
# Check that it supports colours | |
ncolors=$(tput colors) | |
if test -n "$ncolors" && test $ncolors -ge 8 | |
then | |
bold="$(tput bold)" | |
# underline="$(tput smul)" | |
# standout="$(tput smso)" | |
normal="$(tput sgr0)" | |
# 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)" | |
fi | |
fi | |
function _verbose_debug() { | |
local mode=${1:-on} | |
if [ mode = "off" ] | |
then | |
unset PS4 | |
exit | |
fi | |
PS4=' [DEBUG] ${BASH_SOURCE##*/}:${LINENO}:' | |
set -x | |
} | |
function _fatal() { | |
_out "${bold:-}${red:-} [ERROR]${normal:-}" "$1" >&2 | |
exit 1 | |
} | |
function _out() { | |
local type | |
local text | |
type=$1 | |
shift | |
text=$* | |
printf "%s %s\n" "$type" "$text" | |
} | |
function _notice() { | |
_out "${white:-}[NOTICE]${white:-}" "$@" | |
} | |
function _skip() { | |
_out "${cyan:-} [SKIP]${normal:-}" "$@" | |
} | |
function _build() { | |
_out "${green:-} [BUILD]${normal:-}" "$@" | |
} | |
function _pull() { | |
_out "${green:-} [PULL]${normal:-}" "$@" | |
} | |
function _verbose() { | |
if [[ ${verbosity:-} != 'verbose' ]] | |
then | |
return | |
fi | |
_out "${green:-} [PULL]${normal:-}" "$@" | |
} | |
export -f _verbose_debug | |
export -f _fatal | |
export -f _notice | |
export -f _skip | |
export -f _build | |
export -f _pull | |
export -f _verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment