dotfiles/bash_functions
Dan Buch 1bce27ce8b Deferring rbenv init until we need it
since it appears to be the shell init hog.
2013-01-19 23:45:51 -05:00

171 lines
3.1 KiB
Bash

#!/bin/bash
function __available_projects(){
/bin/ls $HOME/src | tr " " "\n"
}
function __proj_activate_comp(){
cur=${COMP_WORDS[COMP_CWORD]}
if [ $COMP_CWORD -eq 1 ]
then
COMPREPLY=( $( compgen -W "$(__available_projects)" $cur ) )
fi
}
function __proj_deactivate_reactivate_comp(){
cur=${COMP_WORDS[COMP_CWORD]}
if [ -n "$VIRTUAL_ENV" ]
then
COMPREPLY=( $(basename "$VIRTUAL_ENV") )
return 0
elif [ -n "$PROJ" ]
then
COMPREPLY=( $(basename "$PROJ") )
return 0
else
COMPREPLY=( $cur )
fi
}
function a(){
source $HOME/.activate_proj "$@"
}
function d(){
source $HOME/.deactivate_proj "$@"
}
function r(){
d "$1"
a "$1"
}
complete -F __proj_activate_comp -o default a
complete -F __proj_deactivate_reactivate_comp -o default d r
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
command_not_found_handle(){
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
else
return 127
fi
}
fi
function add_ssh_keys()
{
if [[ "$1" == "all" ]]
then
for key in $__SSH_KEYS__
do
ssh-add $key
done
else
ssh-add
fi
}
function get_ssh_agent(){
ssh-add -l
test $? -eq 2 || return
agent_out="$HOME/.ssh/agent.out"
source $agent_out
ssh-add -l
test $? -eq 2 || return
ssh-agent > $agent_out
source $agent_out
ssh-add
}
function get_gpg_agent(){
readlink -f "$HOME/.gnupg" >/dev/null || return 0
agent_out="$HOME/.gnupg/agent.out"
tmp=$(mktemp /tmp/gnupgXXXX)
gpg-agent $* > $tmp && mv $tmp $agent_out
grep GPG_TTY $agent_out >/dev/null || \
cat >> $agent_out <<EOF
export GPG_TTY="$(tty)"
EOF
source $agent_out
}
function retag(){
langs="python"
if [ -n "$1" ] ; then
langs="$langs,$1"
fi
rm -f $PWD/tags
ctags --recurse=yes --languages=$langs $PWD
}
function top_commands(){
history | awk '{ print $2 }' | sort | uniq -c | sort -k1 -rn | head
}
function rss_pid_command(){
cmd="ps -o rss,pid,command U $USER"
if [ -n "$1" ] ; then
grep_filter="grep $1"
$cmd | $grep_filter | grep -v "$grep_filter"
else
$cmd
fi
}
function exit_status() {
if [[ $1 -eq 0 ]]; then
echo "\[\033[1;32m\]✓\[\033[00m\]"
else
echo "\[\033[1;31m\]✗\[\033[00m\]:\[\033[31m\]$1\[\033[00m\]"
fi
}
function precmd(){
last_status=$?
PS1="\[\033[1;32m\]\u@\h\[\033[00m\]:\[\033[1;32m\]"
PS1="$PS1$(truncpwd)\[\033[00m\] -$(print-color-loadavg)- "
PS1="$PS1$(__git_ps1 "(%s)")\n$(exit_status $last_status) "
export PS1
}
function unhyphenate() {
ruby -e "puts '$1'.gsub(/[\-_]/, ' ')"
}
function rbenv_init() {
if [ -n "$DEBUG" ] ; then
export RBENV_DEBUG=1
time eval "$(rbenv init -)"
unset RBENV_DEBUG
else
eval "$(rbenv init -)"
fi
}