# -*- sh-shell: bash -*-
# shellcheck shell=bash
if [ -z ${_LIBDEMO_INC+x} ]; then
_LIBDEMO_INC=true
prev=
use_colour() {
if [ "${LIBDEMO_USE_COLOUR:-}" = 1 ]; then
return 0
elif [ "${LIBDEMO_USE_COLOUR:-}" = 0 ]; then
return 1
elif [ "${CLICOLOR_FORCE:-}" = 1 ]; then
return 0
elif [ "${CLICOLOR_FORCE:-}" = 0 ]; then
return 1
elif [ "${TERM:-}" = dumb ]; then
return 1
elif [ -t "${1:-1}" ]; then
return 0
else
return 1
fi
}
use_unicode() {
if [ "${LIBDEMO_USE_UNICODE:-}" = 1 ]; then
return 0
elif [ "${LIBDEMO_USE_UNICODE:-}" = 0 ]; then
return 1
elif [ "${TERM:-}" = dumb ]; then
return 1
else
return 0
fi
}
demopause() {
if [ "${LIBDEMO_INTERACTIVE:-1}" = 1 ]; then
read -n 1 -s -r -p ""
fi
}
demoheader() {
local prefix
echo
if use_unicode; then
if use_colour; then
prefix="\e[1;34m[#]\e[0m"
else
prefix="[#]"
fi
else
if use_colour; then
prefix="\e[1;34m # \e[0m"
else
prefix=" # "
fi
fi
if use_colour; then
echo -e "${prefix}\e[1m" "$@" "\e[0m"
else
echo -e "${prefix}" "$@"
fi
prev=demoheader
}
h() {
demoheader "$@"
}
demoecho() {
if [ "${prev}" != demoecho ]; then
echo
fi
local prefix
prefix=" "
echo -e "${prefix}" "$@"
prev=demoecho
}
,() {
demoecho "$@"
}
democmd() {
echo
local prefix
if use_unicode; then
if use_colour; then
prefix="\e[1;35m[\xE2\x9F\xB6]\e[0m"
else
prefix="[\xE2\x9F\xB6]"
fi
else
if use_colour; then
prefix="\e[1;35m[>]\e[0m"
else
prefix="[>]"
fi
fi
if use_colour; then
echo -e "${prefix}\e[1m" "$@" "\e[0m"
else
echo -e "${prefix}" "$@"
fi
echo
demopause
if $@; then
if [ "${expect}" != 0 ]; then
return 1
fi
if use_unicode; then
if use_colour; then
prefix="\e[0;32m[\xE2\x9C\x93]\e[0m"
else
prefix="[\xE2\x9C\x93]"
fi
else
if use_colour; then
prefix="\e[0;32m[+]\e[0m"
else
prefix="[+]"
fi
fi
else
if [ "${expect}" = 0 ]; then
return 1
fi
if use_unicode; then
if use_colour; then
prefix="\e[0;31m[\xE2\x9C\x97]\e[0m"
else
prefix="[\xE2\x9C\x97]"
fi
else
if use_colour; then
prefix="\e[0;31m[ error ]\e[0m"
else
prefix="[ error ]"
fi
fi
fi
# echo -e "${prefix}"
demopause
prev=democmd
}
x() {
expect=0
democmd "$@"
}
f() {
expect=1
democmd "$@"
}
fi