scripts/lib/common.zsh

65 lines
1.1 KiB
Bash
Raw Normal View History

2020-04-02 08:38:36 +00:00
autoload -Uz colors && colors
local c_fail="${fg_bold[red]}$reset_color"
2020-04-02 08:43:50 +00:00
local c_warn="${fg_bold[yellow]}$reset_color"
2020-04-02 08:38:36 +00:00
local c_success="${fg_bold[green]}$reset_color"
check() {
echo -n " ? $*"
}
result() {
echo -n "\e[s"
echo -n "\e[1G $1"
echo -n "\e[u"
shift
echo " $*"
}
fail() { result $c_fail $@ }
2020-04-02 08:43:50 +00:00
warn() { result $c_warn $@ }
2020-04-02 08:38:36 +00:00
succeed() { result $c_success $@ }
error() { echo -n " "; fail $@ }
2020-04-02 08:43:50 +00:00
warning() { echo -n " "; warn $@ }
2020-04-02 08:38:36 +00:00
exists() {
type "$1" &>/dev/null
}
alternative() {
if ! exists $1; then eval "$1(){ $2 \$@}"; fi
}
alternative-noparam() {
if ! exists $1; then eval "$1(){ $2 }"; fi
}
2020-04-02 08:38:36 +00:00
depend() {
local missing
local i
for i in "$@"; do
exists "$i" || {
2020-04-02 08:38:36 +00:00
echo >&2 " $c_fail Missing dependency: $i. Please install it"
missing=1
}
done
[[ -n "$missing" ]] && exit 1
}
urlencode() {
setopt extendedglob
input=$(</dev/stdin)
# by jkramer, source: http://stackoverflow.com/a/187853/928769
echo "${${(j: :)input}//(#b)(?)/%$[[##16]##${match[1]}]}"
}
2020-07-20 14:33:04 +00:00
pip-venv-deps() {
if [[ -n $VIRTUAL_ENV ]]; then
pip install -qqq "$@"
else
echo "Not in a virtual env."
exit 1
fi
}