dotfiles/shell-functions

74 lines
1.9 KiB
Text
Raw Normal View History

2012-07-20 09:35:41 -07:00
#!/bin/zsh
# vim: ft=zsh
# Helper functions for the init files
# Eryn Wells <eryn@erynwells.me>
# Print prettier, more uniform messages
2012-11-29 15:18:57 -08:00
function print_msg {
# Level 0: always show
local -i level=0 bold=0
2013-08-29 14:47:57 -07:00
local prefix message noeol
2012-11-29 15:18:57 -08:00
local foreground background
2012-07-20 09:35:41 -07:00
2013-08-29 14:47:57 -07:00
while getopts 'bl:f:k:np:' opt; do
2012-11-29 15:18:57 -08:00
case $opt in
b) bold=1;;
l) level="$OPTARG";;
f) foreground="$OPTARG";;
k) background="$OPTARG";;
2013-08-29 14:47:57 -07:00
n) noeol=1;;
2012-11-29 15:18:57 -08:00
p) prefix="$OPTARG";;
esac
done
message=$@[$OPTIND,${#@}]
(( bold )) && message="%B${message}%b"
[[ -n "$foreground" ]] && message="%F{$foreground}$message%f"
[[ -n "$background" ]] && message="%K{$background}$message%k"
[[ -n "$prefix" ]] && message="$prefix $message"
2013-08-29 14:47:57 -07:00
[[ ${NOISY:-0} -ge $level ]] && print -P${noeol:+n} "${message}"
2012-11-29 15:18:57 -08:00
}
function print_info { print_msg -p '%F{blue}==>%f' "$@" }
function print_error { print_msg -p '%F{red}==>%f' "$@" }
2012-11-29 15:18:57 -08:00
function print_heading { print_msg -b "$@" }
function print_info_heading { print_info -b "$@" }
function print_error_heading { print_error -b "$@" }
2012-11-29 15:18:57 -08:00
function print_msg_sub { print_msg -p " *" "$@" }
function print_info_sub { print_msg_sub -p ' %F{blue}*%f' "$@" }
function print_error_sub { print_msg_sub -p ' %F{error}*%f' "$@" }
2012-11-27 16:24:08 -08:00
function binary_exists { return $(hash $1 1>/dev/null 2>&1) }
function binary_not_exists { binary_exists $1; return $(( ! $? )) }
function load_module {
local mod=$1
local modpath
for p in $fpath; do
2013-03-27 11:55:43 -07:00
modpath=$p/$mod
[[ -d $modpath ]] && break
modpath=''
done
if [[ -z "$modpath" ]]; then
print_error -l 1 "Couldn't find path to module: $mod"
return 1
fi
2013-03-27 11:55:43 -07:00
fpath+=($modpath)
print_info -l 1 "Loading module: $mod"
for func in `ls $modpath`; do
print_info_sub -l 2 "Loading function: $func"
autoload $func
done
return 0
}