helm/zsh/functions
2013-11-30 13:02:12 -05:00

101 lines
2.4 KiB
Bash

# Check to see if a command exists
function command_exists {
command -v "$1" &> /dev/null
}
# OS detection
function is_deb {
command_exists apt-get
}
function is_mac {
[[ $(uname) == 'Darwin' ]]
}
function is_linux {
[[ $(uname) == 'Linux' ]]
}
# Package management aliases
function pkg_install() {
if is_mac; then
brew install $1
elif is_deb; then
sudo apt-get -y install $1
fi
}
function pkg_remove() {
if is_mac; then
brew remove $1
elif is_deb; then
sudo apt-get -y remove $1
fi
}
function pkg_update() {
if is_mac; then
brew update && brew upgrade
elif is_deb; then
sudo apt-get update && sudo apt-get upgrade
fi
}
# Ask for admin password up front and keep sudo alive until the end of the script
function keep_alive {
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
}
# Extract a compressed file with the appropriate command
function unpack {
if [ -f "$1" ]; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
if is_mac; then
# Change working directory to the top-most Finder window location
function cdf() { # short for `cdfinder`
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"
}
# Open a file in Mac OS's quick-look
function quicklook {
(( $# > 0 )) && qlmanage -p $* &>/dev/null &
}
# Check to see if the (mac) app exists
function app_exists {
[ -d /Applications/"$1" ] || [ -e /Applications/"$1".app ] || [ -e ~/Applications/"$1".app ]
}
fi
# Output functions
function echo_g {
echo -e "$fg[green]$1$reset_color"
}
function echo_r {
echo -e "$fg[red]$1$reset_color"
}
# vim:set ft=zsh: