Last active
November 3, 2020 10:51
-
-
Save jeremypruitt/6b1bcd6bcfbff1daa75624d9d12ac6e5 to your computer and use it in GitHub Desktop.
CLI output helper lib
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
# Formatting | |
DEFAULT=`tput sgr0` | |
BRIGHT=`tput bold` | |
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` | |
log() { | |
if [ $# -lt 2 ]; then | |
local log_msg="$1" | |
else | |
local log_type="$1" | |
local log_msg="$2" | |
fi | |
case "$log_type" in | |
blank) echo -e " ${WHITE}${log_msg}${DEFAULT}" ;; | |
dblank) echo -e " ${WHITE}${log_msg}${DEFAULT}" ;; | |
error) echo -e "❌ ${BRIGHT}${RED}${ERROR}${log_msg}${DEFAULT}" ;; | |
indent) echo -e " ${BRIGHT}${GREEN}✔ ${DEFAULT}${log_msg}${DEFAULT}" ;; | |
dindent) echo -e " ${BRIGHT}${GREEN}✔ ${DEFAULT}${log_msg}${DEFAULT}" ;; | |
check) echo -e " ${BRIGHT}${GREEN}✔ ${DEFAULT}${log_msg}${DEFAULT}" ;; | |
info) echo -e "ℹ️ ${BRIGHT}${WHITE}${log_msg}${DEFAULT}" ;; | |
notice) echo -e "ℹ️ ${BRIGHT}${WHITE}${log_msg}${DEFAULT}" ;; | |
rocket) echo -e "🚀 ${BRIGHT}${WHITE}${log_msg}${DEFAULT}" ;; | |
clean) echo -e " 🧹 ${WHITE}${log_msg}${DEFAULT}" ;; | |
web) echo -e "🌎 ${WHITE}${log_msg} ${BRIGHT}${CYAN}${3}${DEFAULT}" ;; | |
dweb) echo -e " 🌎 ${WHITE}${log_msg} ${BRIGHT}${CYAN}${3}${DEFAULT}" ;; | |
shell) echo -e " 🖥️ ${WHITE}${log_msg}${DEFAULT}" ;; | |
*) echo -e "${log_msg}" ;; | |
esac | |
} | |
horizontal_rule() { | |
echo "${BRIGHT}${BLUE}――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――${DEFAULT}" | |
} | |
upper_rule() { | |
echo "${BRIGHT}${BLUE}▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔${DEFAULT}" | |
} | |
lower_rule() { | |
echo "${BRIGHT}${BLUE}▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁${DEFAULT}" | |
} | |
_() { eval "$@" 2>&1 | sed "s/^/ /" ; return "$PIPESTATUS" ;} | |
# Read a single char from /dev/tty, prompting with "$*" | |
function get_keypress { | |
local REPLY IFS= | |
>/dev/tty printf "❔ ${BRIGHT}${WHITE}%s${DEFAULT}" "$*" | |
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> '' | |
[[ $BASH_VERSION ]] && </dev/tty read -rn1 | |
printf '%s' "$REPLY" | |
} | |
# Get a y/n from the user, return yes=0, no=1 enter=$2. Prompt using $1. | |
# If set, return $2 on pressing enter, useful for cancel or defaulting. | |
function get_yes_keypress { | |
local prompt="${1:-Are you sure}" | |
local enter_return=$2 | |
local REPLY | |
while REPLY=$(get_keypress "$prompt"); do | |
[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses enter | |
case "$REPLY" in | |
Y|y) return 0;; | |
N|n) return 1;; | |
'') [[ $enter_return ]] && return "$enter_return" | |
esac | |
done | |
} | |
# Prompt to confirm, defaulting to NO on <enter> | |
# Usage: confirm "Dangerous. Are you sure?" && rm * | |
function confirm { | |
local prompt="${*:-Are you sure} [y/N]? " | |
get_yes_keypress "$prompt" 1 | |
} | |
# Prompt to confirm, defaulting to YES on <enter> | |
function confirm_yes { | |
local prompt="${*:-Are you sure} [Y/n]? " | |
get_yes_keypress "$prompt" 0 | |
} | |
function fail { | |
log error "$1" | |
exit "${2:-1}" ## Return a code specified by $2 or 1 by default. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment