dotfiles/shell-functions

73 lines
1.9 KiB
Bash

#!/bin/zsh
# vim: ft=zsh
# Helper functions for the init files
# Eryn Wells <eryn@erynwells.me>
# Print prettier, more uniform messages
function print_msg {
# Level 0: always show
local -i level=0 bold=0
local prefix message noeol
local foreground background
while getopts 'bl:f:k:np:' opt; do
case $opt in
b) bold=1;;
l) level="$OPTARG";;
f) foreground="$OPTARG";;
k) background="$OPTARG";;
n) noeol=1;;
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"
[[ ${NOISY:-0} -ge $level ]] && print -P${noeol:+n} "${message}"
}
function print_info { print_msg -p '%F{blue}==>%f' "$@" }
function print_error { print_msg -p '%F{red}==>%f' "$@" }
function print_heading { print_msg -b "$@" }
function print_info_heading { print_info -b "$@" }
function print_error_heading { print_error -b "$@" }
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' "$@" }
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
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
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
}