1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-26 15:50:09 +00:00
ohmyzsh/plugins/wd/wd.sh

237 lines
4.6 KiB
Bash
Raw Normal View History

2013-11-16 01:15:17 +00:00
#!/bin/zsh
# WARP DIRECTORY
# ==============
2013-11-16 01:15:17 +00:00
# Jump to custom directories in terminal
# because `cd` takes too long...
#
# @github.com/mfaerevaag/wd
## variables
readonly CONFIG=$HOME/.warprc
2013-11-16 01:15:17 +00:00
# colors
readonly BLUE="\033[96m"
readonly GREEN="\033[92m"
readonly YELLOW="\033[93m"
readonly RED="\033[91m"
readonly NOC="\033[m"
2013-11-16 01:15:17 +00:00
## init
2013-11-25 23:45:24 +00:00
# check if config file exists
if [ ! -e $CONFIG ]
2013-11-25 23:45:24 +00:00
then
# if not, create config file
touch $CONFIG
2013-11-25 23:45:24 +00:00
fi
# load warp points
2013-11-16 01:15:17 +00:00
typeset -A points
while read -r line
2013-11-16 01:15:17 +00:00
do
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
points[$key]=$val
done < $CONFIG
## functions
wd_warp()
{
local point=$1
if [[ $point =~ "^\.+$" ]]
2013-11-16 01:15:17 +00:00
then
if [ $#1 < 2 ]
2013-11-16 01:15:17 +00:00
then
wd_print_msg $YELLOW "Warping to current directory?"
else
(( n = $#1 - 1 ))
cd -$n > /dev/null
fi
elif [[ ${points[$point]} != "" ]]
2013-11-16 01:15:17 +00:00
then
cd ${points[$point]}
2013-11-16 01:15:17 +00:00
else
wd_print_msg $RED "Unknown warp point '${point}'"
2013-11-16 01:15:17 +00:00
fi
}
wd_add()
{
local force=$1
local point=$2
if [[ $point =~ "^[\.]+$" ]]
then
wd_print_msg $RED "Warp point cannot be just dots"
elif [[ $point =~ "(\s|\ )+" ]]
2013-11-16 01:15:17 +00:00
then
wd_print_msg $RED "Warp point should not contain whitespace"
elif [[ $point == *:* ]]
2013-11-16 01:15:17 +00:00
then
wd_print_msg $RED "Warp point cannot contain colons"
elif [[ $point == "" ]]
then
wd_print_msg $RED "Warp point cannot be empty"
elif [[ ${points[$2]} == "" ]] || $force
then
wd_remove $point > /dev/null
printf "%q:%q\n" "${point}" "${PWD}" >> $CONFIG
2013-11-16 01:15:17 +00:00
wd_print_msg $GREEN "Warp point added"
else
wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite."
2013-11-16 01:15:17 +00:00
fi
}
wd_remove()
{
local point=$1
if [[ ${points[$point]} != "" ]]
2013-11-16 01:15:17 +00:00
then
if sed -i.bak "s,^${point}:.*$,,g" $CONFIG
2013-11-16 01:15:17 +00:00
then
wd_print_msg $GREEN "Warp point removed"
else
wd_print_msg $RED "Something bad happened! Sorry."
2013-11-16 01:15:17 +00:00
fi
else
wd_print_msg $RED "Warp point was not found"
fi
}
wd_list_all()
{
wd_print_msg $BLUE "All warp points:"
while IFS= read -r line
2013-11-16 01:15:17 +00:00
do
if [[ $line != "" ]]
then
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
printf "%20s -> %s\n" $key $val
2013-11-16 01:15:17 +00:00
fi
done <<< $(sed "s:${HOME}:~:g" $CONFIG)
}
wd_show()
{
local cwd=$(print $PWD | sed "s:^${HOME}:~:")
wd_print_msg $BLUE "Warp points to current directory:"
wd_list_all | grep -e "${cwd}$"
2013-11-16 01:15:17 +00:00
}
wd_print_msg()
{
local color=$1
local msg=$2
if [[ $color == "" || $msg == "" ]]
2013-11-16 01:15:17 +00:00
then
print " ${RED}*${NOC} Could not print message. Sorry!"
2013-11-16 01:15:17 +00:00
else
print " ${color}*${NOC} ${msg}"
2013-11-16 01:15:17 +00:00
fi
}
wd_print_usage()
{
cat <<- EOF
Usage: wd [add|-a|--add] [rm|-r|--remove] <point>
Commands:
add Adds the current working directory to your warp points
add! Overwrites existing warp point
rm Removes the given warp point
show Outputs warp points to current directory
ls Outputs all stored warp points
help Show this extremely helpful text
EOF
2013-11-16 01:15:17 +00:00
}
## run
# get opts
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 ]
2013-11-25 23:45:24 +00:00
then
# do nothing
2013-11-25 23:45:24 +00:00
# can't run `exit`, as this would exit the executing shell
wd_print_msg $RED "\'$CONFIG\' is not writeable."
2013-11-25 23:45:24 +00:00
else
for o
2013-11-16 01:15:17 +00:00
do
case "$o"
2014-03-04 19:25:54 +00:00
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 $o
2013-11-16 01:15:17 +00:00
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
unset wd_warp
unset wd_add
unset wd_remove
unset wd_show
unset wd_list_all
unset wd_print_msg
unset wd_print_usage
2014-03-04 19:25:54 +00:00
unset args
unset points
2014-03-04 19:25:54 +00:00
unset val &> /dev/null # fixes issue #1