blob: f872c776e17067620eab1732d68de9054aad006f (
plain) (
tree)
|
|
# output handling
coco() {
[[ -z "$1" ]] && echo -ne "\033[0m" || echo -ne "\033[${1}m"
}
# name all colors to get rid of hieroglyphics
color() {
case $1 in
black) coco '0;30' ;;
dgray) coco '1;30' ;;
red) coco '0;31' ;;
lred) coco '1;31' ;;
green) coco '0;32' ;;
lgreen) coco '1;32' ;;
brown) coco '0;33' ;;
yellow) coco '1;33' ;;
blue) coco '0;34' ;;
lblue) coco '1;34' ;;
purple) coco '0;35' ;;
lpurple) coco '1;35' ;;
cyan) coco '0;36' ;;
lcyan) coco '1;36' ;;
lgray) coco '0;37' ;;
white) coco '1;37' ;;
*) coco '0' ;;
esac
}
_einfon() {
echo -ne " ${GOOD}*${NORMAL} $*"
return 0
}
_einfo() {
echo -e " ${GOOD}*${NORMAL} $*"
return 0
}
_ewarn() {
echo -e " ${WARN}*${NORMAL} $*"
return 0
}
_eerror() {
echo -e " ${BAD}*${NORMAL} $*"
return 0
}
_ewrap() {
local cmd="$1"
shift
local c="${ENDCOL}"
local p="$* "
local max=$((COLS - 11))
# Only use what we have space for
local d="${p%%\\n*}"
if [[ ${d} == "${p}" ]] ; then
d="${p:0:${max}}"
else
d="${d} "
fi
# Grab the last whole word
[[ ${#d} == "${max}" ]] && d="${d% *}"
# Print that
_${cmd} "${d}"
# Recurse on the rest
local r="$*"
d="${d} "
r="${r:${#d}}"
[[ -n ${r} ]] && ${cmd} "${r}"
return 0
}
einfo() {
_ewrap einfo "$*"
}
ewarn() {
_ewrap ewarn "$*"
}
eerror() {
echo
_ewrap eerror "$*"
return 1
}
eheading() {
echo
echo -n "$(color green)>>> "
echo -n "$@"
echo " <<<$(color)"
}
# input handling
eask_num() {
local -i reply=
_einfon
read -p "Please enter ${1:-Number} or press CTRL-C to abort> " reply
REPLY=${reply}
}
eask_str() {
local reply=
_einfon
read -p "Please enter ${1:-String} or press CTRL-C to abort> " reply
REPLY=${reply}
}
eask_bool() {
local reply= default=${2} str="[y/n]"
if [[ ${default} == y || ${default} == Y || ${default} == true ]]
then
str="[Y/n]"
elif [[ ${default} == n || ${default} == N || ${default} == false ]]
then
str="[y/N]"
fi
while [[ "${reply}" != y && "${reply}" != n ]]; do
_einfon
read -p "${1} ${str} " reply
if [[ -z ${reply} ]]
then
reply=${default}
break
fi
done
REPLY=${reply}
}
echoose() {
einfo ${1:-"Choose something from the following list"}
einfo
shift
local -a items=
while true; do
items=()
for i in "$@"; do
items=( "${items[@]}" "${i}" )
einfo "${#items[@]}) ${i}"
done
eask_num
if [[ ${REPLY} -le 0 || ${REPLY} -gt ${#items[@]} ]]; then
eerror "\nInvalid selection. Try again.\n " || :
else
break
fi
done
REPLY=${items[$(( ${REPLY} - 1 ))]}
}
# make-like exec wrapper
eexec() {
einfo "$@"
"$@"
}
# alway use the right thing, even if EDITOR is not set
editor() {
if [[ -z "${EDITOR}" || ! -x "${EDITOR}" ]]; then
if [[ -x /usr/bin/vim ]]; then
EDITOR=/usr/bin/vim
elif [[ -x /usr/bin/nano ]]; then
EDITOR=/usr/bin/nano
fi
fi
[[ -x ${EDITOR} ]] && ${EDITOR} "$@"
}
# Setup a secure $PATH. Just add system defaults.
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
if [[ -z ${COLS} || -n ${COLUMNS} ]] ; then
COLS="${COLUMNS:-0}"
(( COLS == 0 )) && COLS=$(set -- `stty size 2>/dev/null` ; echo "$2")
(( COLS > 0 )) || (( COLS = 80 ))
fi
ENDCOL=$'\e[A\e['$(( COLS - 9 ))'C'
GOOD=$'\e[32;01m'
WARN=$'\e[33;01m'
BAD=$'\e[31;01m'
NORMAL=$'\e[0m'
# vim: set ts=4 :
|