Skip to content

Instantly share code, notes, and snippets.

@Ruxton
Created October 3, 2011 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ruxton/1258583 to your computer and use it in GitHub Desktop.
Save Ruxton/1258583 to your computer and use it in GitHub Desktop.
#
#Usage
#
#$ __selector "Select a volume" "selected_volume" "" "`ls -la /Volumes/`"
#$ cd $selected_volume
#
# __selector: good for selecting stuff
function __selector() {
local selections selPrompt selCurrent selListCommand selSize choose
selPrompt=$1
selReturn=$2
selCurrent=$3
selList=$4
prompt=$selPrompt
let count=0
for sel in $selList; do
let count++
selections[$count-1]=$sel
done
if [[ $count > 0 ]]; then
choose=0
selSize=${#selections[@]}
while [ $choose -eq 0 ]; do
let count=0
for sel in $selList; do
let count++
echo "$count) $sel"
done
echo
read -ep "${prompt}, followed by [ENTER]:" choose
if [[ $choose != ${choose//[^0-9]/} ]] || [ ! $choose -le $selSize ]
then
echo
echo "Please choose one of the listed numbers."
echo
let choose=0
fi
done
let choose--
export ${selReturn}=${selections[$choose]}
else
return 0
fi
}
###
# Used for searching commmands stored in ~/.profile.d (all these commands), looks for comment above func/alias for help text
###
# als: locate aliases/functions stored in ~/.profile.d
function als() {
local func ali
func="^$1.*()*$"
ali="^alias $1.*=*$"
grep -hE -B 1 "$func|$ali" ~/.profile.d/*.bash | grep "^# $1.*:*$"
}
function _als_bash_completer() { local current list
local cmd="${1##*/}"
COMPREPLY=()
local current="${COMP_WORDS[COMP_CWORD]}"
local func="^$1.*()*$"
local ali="^alias $1.*=*$"
if [[ ( ${COMP_CWORD} -eq 1 ) && ${COMP_WORDS[0]} == "$cmd" ]]; then
list=$(grep -hE -B 1 "^.*()*$|^alias .*=*$" ~/.profile.d/*.bash | grep "^# " | awk '{out=substr($2,0,length($2)-1)} { print out }' )
COMPREPLY=( $(compgen -W "$list" -- "${current}") )
fi
}
complete -F _als_bash_completer als
###
# Generate rails migrations and open in editor
##
# rgm: Generate rails migrations
function rgm() {
local fileopener file
fileopener='mate'
file=`rails g migration $@ | grep "create" | awk '{print $3}'`
# open migration in netbeans
$fileopener $file
}
# __screend: screen daemonizing
__screend() {
local name="${1}"
local check=`screen -list|grep $name|awk '{print $1}'`
if [[ "$2" = "" ]]; then
if [[ "${check}" = "" ]]; then
screen -dmS $name $name > /dev/null
local runCheck=`screen -list|grep $name|awk '{print $1}'`
if [[ "${runCheck}" = "" ]]; then
echo "Unable to start ${name} in background"
else
echo "Started ${name} in background"
fi
else
echo "${1} is running in background..."
read -ep "re-attach? (y/n)" choice
if [[ $choice = [yY] ]]; then
echo "Attaching ${name} session..."
screen -r $check
else
echo "${name} currently running at ${check}"
fi
fi
else
if [[ $2 -eq "load" ]]; then
screen -r $name
fi
fi
}
###
## ThumbIt - Sending stuff to and from a portable volume in OSX
###
# thumbit: zip and copy recursively to usb key
function thumbit() {
echo
echo "ThumbIT Zip & Copy"
echo
echo "Usage: thumbit <target>"
echo " thumbit --list"
echo " thumbit --rm"
__volume_selector "selected_volume"
local thumbitdir="/Volumes/${selected_volume}/thumbit"
if [ $1 == "--list" ]
then
ls -l $thumbitdir/*.zip
return 1
fi
zipf $1 $1
if [ ! -d $thumbitdir ] && [ ! -f $thumbitdir ]
then
mkdir $thumbitdir
fi
echo "Copying ${1} to ${selected_volume}..."
if [ -d $thumbitdir ]
then
cp -f $1.zip /Volumes/$selected_volume/thumbit/
rm -f $1.zip
echo "Done"
fi
}
# unthumbit: copy from usb key and unzip
function unthumbit() {
echo
echo "UnThumbIT Copy & Unzip"
echo
__volume_selector "selected_volume"
local thumbitdir="/Volumes/${selected_volume}/thumbit"
echo "Copying ${1} from ${selected_volume}..."
if [ -d $thumbitdir ]
then
cp -f $thumbitdir/$1.zip ./
unzip $1.zip
rm -f $1.zip
echo "Done"
fi
}
# thumbitrm: remove stuff from thumbit folders
function thumbitrm() {
echo
echo "ThumbIT Cleanup"
echo
__volume_selector "selected_volume"
local thumbitdir="/Volumes/${selected_volume}/thumbit"
if [ -f $thumbitdir ]
then
echo "Found ${1} in ${thumbitdir}"
else
echo "Unable to find ${1} in ${thumbitdir}"
fi
}
function __volume_selector() {
local volumes
volumes=`ls /Volumes/`
__selector "Enter a volume to select" $1 "" $volumes
}
function __dir_selector() {
local directories
local current_dir=`PWD`
directories=`ls -C $1`
__selector "Enter a directory to select" $2 "" $directories
}
###
# Switching in and out of "workspace" directories (has completion)
##
renv_path="/Users/ruxton/AppsByGreg/ror/"
wrenv_path="/Users/ruxton/Work/ror/"
wienv_path="/Users/ruxton/Work/ios/"
wjenv_path="/Users/ruxton/Work/java/"
penv_path="/Users/ruxton/AppsByGreg/php/"
wpenv_path="/Users/ruxton/Work/php/"
aenv_path="/Users/ruxton/AppsByGreg/android/"
# wrenv: Change directory into work ruby environment
function wrenv() { cd $wrenv_path$@ ;}
# wienv: Change directory into work iOS environment
function wienv() { cd $wienv_path$@ ;}
# wpenv: Change directory into work PHP environment
function wpenv() { cd $wpenv_path$@ ;}
# wjenv: Change directory into work PHP environment
function wjenv() { cd $wjenv_path$@ ;}
# renv: Change directory to personal ruby environment
function renv() { cd $renv_path$@ ;}
# penv: Change directory to personal PHP environment
function penv() { cd $penv_path$@ ;}
# aenv: Change directory to Android environment
function aenv() { cd $aenv_path$@ ;}
function _bash_complete_dirlister() {
local current dir_list dir
local cmd="${1##*/}"
temp=${cmd}_path
dir=${!temp}
COMPREPLY=()
current="${COMP_WORDS[COMP_CWORD]}"
if [[ ( ${COMP_CWORD} -eq 1 ) && ${COMP_WORDS[0]} == "$cmd" ]]; then
dir_list=$(ls -l "$dir"|grep '^d'|awk '{ print $9 }')
COMPREPLY=( $(compgen -W "$dir_list" -- "${current}") )
fi
}
complete -F _bash_complete_dirlister wrenv renv aenv penv wpenv wienv wjenv
###
# Zip files or folders into a zip file
###
# zipf: to create a ZIP archive of a file or folder
function zipf () {
args=("$@")
argsize=$#
for(( i=1;i<argsize;i++)); do
restof="$restof ${args[${i}]}"
done
zip -r "$1".zip $restof ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment