1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-30 01:30:08 +00:00
ohmyzsh/plugins/wd/wd.sh

210 lines
4.3 KiB
Bash
Raw Normal View History

2013-11-16 01:15:17 +00:00
#!/bin/zsh
# WARP
# ====
# Jump to custom directories in terminal
# because `cd` takes too long...
#
# @github.com/mfaerevaag/wd
## variables
CONFIG=$HOME/.warprc
## colors
BLUE="\033[96m"
GREEN="\033[92m"
YELLOW="\033[93m"
RED="\033[91m"
NOC="\033[m"
2013-11-25 23:45:24 +00:00
# check if config file exists
if [[ ! -a $CONFIG ]]
then
# if not: create config file
touch $CONFIG
fi
2013-11-16 01:15:17 +00:00
## load warp points
typeset -A points
while read line
do
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
points[$key]=$val
done < $CONFIG
## functions
# prepended wd_ to not conflict with your environment (no sub shell)
wd_warp()
{
if [[ $1 =~ "^\.+$" ]]
then
if [[ $#1 < 2 ]]
then
wd_print_msg $YELLOW "Warping to current directory?"
else
(( n = $#1 - 1 ))
2014-03-04 19:25:54 +00:00
#wd_print_msg $BLUE "Warping..."
2013-11-16 01:15:17 +00:00
cd -$n > /dev/null
fi
elif [[ ${points[$1]} != "" ]]
then
2014-03-04 19:25:54 +00:00
#wd_print_msg $BLUE "Warping..."
2013-11-16 01:15:17 +00:00
cd ${points[$1]}
else
wd_print_msg $RED "Unkown warp point '$1'"
fi
}
wd_add()
{
2014-03-04 19:25:54 +00:00
if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
2013-11-16 01:15:17 +00:00
then
2014-03-04 19:25:54 +00:00
wd_print_msg $RED "Illegal warp point (see README)."
elif [[ ${points[$2]} == "" ]] || $1
2013-11-16 01:15:17 +00:00
then
2014-03-04 19:25:54 +00:00
wd_remove $2 > /dev/null
print "$2:$PWD" >> $CONFIG
2013-11-16 01:15:17 +00:00
wd_print_msg $GREEN "Warp point added"
else
2014-03-04 19:25:54 +00:00
wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
2013-11-16 01:15:17 +00:00
fi
}
wd_remove()
{
if [[ ${points[$1]} != "" ]]
then
if wd_tmp=`sed "/^$1:/d" $CONFIG`
then
2014-03-04 19:25:54 +00:00
# `>!` forces overwrite
# we need this if people use `setopt NO_CLOBBER`
echo $wd_tmp >! $CONFIG
2013-11-16 01:15:17 +00:00
wd_print_msg $GREEN "Warp point removed"
else
wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
fi
else
wd_print_msg $RED "Warp point was not found"
fi
}
wd_show()
{
wd_print_msg $BLUE "Warp points to current directory:"
wd_list_all | grep $PWD$
}
wd_list_all()
{
wd_print_msg $BLUE "All warp points:"
while read line
do
if [[ $line != "" ]]
then
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
print "\t" $key "\t -> \t" $val
fi
done < $CONFIG
}
wd_print_msg()
{
if [[ $1 == "" || $2 == "" ]]
then
print " $RED*$NOC Could not print message. Sorry!"
else
print " $1*$NOC $2"
fi
}
wd_print_usage()
{
2013-11-25 23:45:24 +00:00
print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
2013-11-16 01:15:17 +00:00
print "\nCommands:"
print "\t add \t Adds the current working directory to your warp points"
print "\t add! \t Overwrites existing warp point"
2014-03-04 19:25:54 +00:00
print "\t rm \t Removes the given warp point"
2013-11-25 23:45:24 +00:00
print "\t show \t Outputs warp points to current directory"
2014-03-04 19:25:54 +00:00
print "\t ls \t Outputs all stored warp points"
2013-11-16 01:15:17 +00:00
print "\t help \t Show this extremely helpful text"
}
## run
# get opts
2014-03-04 19:25:54 +00:00
args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*`
2013-11-16 01:15:17 +00:00
2013-11-25 23:45:24 +00:00
# check if no arguments were given
2013-11-16 01:15:17 +00:00
if [[ $? -ne 0 || $#* -eq 0 ]]
then
wd_print_usage
2013-11-25 23:45:24 +00:00
# check if config file is writeable
elif [[ ! -w $CONFIG ]]
then
wd_print_msg $RED "\'$CONFIG\' is not writeable."
# do nothing => exit
# can't run `exit`, as this would exit the executing shell
# i.e. your terminal
else
2013-11-16 01:15:17 +00:00
#set -- $args # WTF
for i
do
2014-03-04 19:25:54 +00:00
case "$i"
in
-a|--add|add)
wd_add false $2
2013-11-16 01:15:17 +00:00
break
;;
-a!|--add!|add!)
2014-03-04 19:25:54 +00:00
wd_add true $2
2013-11-16 01:15:17 +00:00
break
;;
2014-03-04 19:25:54 +00:00
-r|--remove|rm)
wd_remove $2
2013-11-16 01:15:17 +00:00
break
;;
2014-03-04 19:25:54 +00:00
-l|--list|ls)
wd_list_all
2013-11-16 01:15:17 +00:00
break
;;
2014-03-04 19:25:54 +00:00
-h|--help|help)
wd_print_usage
2013-11-16 01:15:17 +00:00
break
;;
2014-03-04 19:25:54 +00:00
-s|--show|show)
wd_show
2013-11-16 01:15:17 +00:00
break
;;
*)
wd_warp $i
break
;;
2014-03-04 19:25:54 +00:00
--)
break
;;
esac
2013-11-16 01:15:17 +00:00
done
fi
## garbage collection
# if not, next time warp will pick up variables from this run
# remember, there's no sub shell
2014-03-04 19:25:54 +00:00
unset points
unset args
unset val &> /dev/null # fixes issue #1