oh-my-zsh/plugins/wd/wd.sh

447 lines
9.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
2014-09-07 19:56:34 +00:00
# version
readonly WD_VERSION=0.4
2013-11-16 01:15:17 +00:00
# colors
2014-09-07 19:56:34 +00:00
readonly WD_BLUE="\033[96m"
readonly WD_GREEN="\033[92m"
readonly WD_YELLOW="\033[93m"
readonly WD_RED="\033[91m"
readonly WD_NOC="\033[m"
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
## functions
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
# helpers
wd_yesorno()
{
# variables
local question="${1}"
local prompt="${question} "
local yes_RETVAL="0"
local no_RETVAL="3"
local RETVAL=""
local answer=""
# read-eval loop
while true ; do
printf $prompt
read -r answer
case ${answer:=${default}} in
Y|y|YES|yes|Yes )
RETVAL=${yes_RETVAL} && \
break
;;
N|n|NO|no|No )
RETVAL=${no_RETVAL} && \
break
;;
* )
echo "Please provide a valid answer (y or n)"
;;
esac
done
2014-09-07 19:56:34 +00:00
return ${RETVAL}
}
2013-11-25 23:45:24 +00:00
2014-09-07 19:56:34 +00:00
wd_print_msg()
{
if [[ -z $wd_quiet_mode ]]
then
local color=$1
local msg=$2
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
if [[ $color == "" || $msg == "" ]]
then
print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!"
else
print " ${color}*${WD_NOC} ${msg}"
fi
fi
}
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
wd_print_usage()
{
cat <<- EOF
Usage: wd [command] <point>
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
Commands:
add <point> Adds the current working directory to your warp points
add! <point> Overwrites existing warp point
rm <point> Removes the given warp point
show Print warp points to current directory
show <point> Print path to given warp point
2015-01-25 23:13:39 +00:00
list Print all stored warp points
ls <point> Show files from given warp point
path <point> Show the path to given warp point
2014-09-07 19:56:34 +00:00
clean! Remove points warping to nonexistent directories
-v | --version Print version
-d | --debug Exit after execution with exit codes (for testing)
-c | --config Specify config file (default ~/.warprc)
-q | --quiet Suppress all output
help Show this extremely helpful text
EOF
}
wd_exit_fail()
{
local msg=$1
2015-01-25 23:13:39 +00:00
wd_print_msg $WD_RED $msg
2014-09-07 19:56:34 +00:00
WD_EXIT_CODE=1
}
wd_exit_warn()
{
local msg=$1
wd_print_msg $WD_YELLOW $msg
WD_EXIT_CODE=1
}
2015-01-25 23:13:39 +00:00
wd_getdir()
{
local name_arg=$1
point=$(wd_show $name_arg)
dir=${point:28+$#name_arg+7}
if [[ -z $name_arg ]]; then
wd_exit_fail "You must enter a warp point"
break
elif [[ -z $dir ]]; then
wd_exit_fail "Unknown warp point '${name_arg}'"
break
fi
}
2014-09-07 19:56:34 +00:00
# core
2013-11-16 01:15:17 +00:00
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
2014-09-07 19:56:34 +00:00
wd_exit_warn "Warping to current directory?"
2013-11-16 01:15:17 +00:00
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
2014-09-07 19:56:34 +00:00
wd_exit_fail "Unknown warp point '${point}'"
2013-11-16 01:15:17 +00:00
fi
}
wd_add()
{
local force=$1
local point=$2
if [[ $point =~ "^[\.]+$" ]]
then
2014-09-07 19:56:34 +00:00
wd_exit_fail "Warp point cannot be just dots"
2014-10-21 07:51:13 +00:00
elif [[ $point =~ "[[:space:]]+" ]]
2013-11-16 01:15:17 +00:00
then
2014-09-07 19:56:34 +00:00
wd_exit_fail "Warp point should not contain whitespace"
elif [[ $point == *:* ]]
2013-11-16 01:15:17 +00:00
then
2014-09-07 19:56:34 +00:00
wd_exit_fail "Warp point cannot contain colons"
elif [[ $point == "" ]]
then
2014-09-07 19:56:34 +00:00
wd_exit_fail "Warp point cannot be empty"
elif [[ ${points[$2]} == "" ]] || $force
then
wd_remove $point > /dev/null
2014-10-21 07:51:13 +00:00
printf "%q:%s\n" "${point}" "${PWD}" >> $WD_CONFIG
2014-09-07 19:56:34 +00:00
wd_print_msg $WD_GREEN "Warp point added"
2014-09-07 19:56:34 +00:00
# override exit code in case wd_remove did not remove any points
# TODO: we should handle this kind of logic better
WD_EXIT_CODE=0
2013-11-16 01:15:17 +00:00
else
2014-09-07 19:56:34 +00:00
wd_exit_warn "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
2014-09-07 19:56:34 +00:00
local config_tmp=$WD_CONFIG.tmp
if sed -n "/^${point}:.*$/!p" $WD_CONFIG > $config_tmp && mv $config_tmp $WD_CONFIG
2013-11-16 01:15:17 +00:00
then
2014-09-07 19:56:34 +00:00
wd_print_msg $WD_GREEN "Warp point removed"
2013-11-16 01:15:17 +00:00
else
2014-09-07 19:56:34 +00:00
wd_exit_fail "Something bad happened! Sorry."
2013-11-16 01:15:17 +00:00
fi
else
2014-09-07 19:56:34 +00:00
wd_exit_fail "Warp point was not found"
2013-11-16 01:15:17 +00:00
fi
}
wd_list_all()
{
2014-09-07 19:56:34 +00:00
wd_print_msg $WD_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]}
2014-09-07 19:56:34 +00:00
if [[ -z $wd_quiet_mode ]]
then
printf "%20s -> %s\n" $key $val
fi
2013-11-16 01:15:17 +00:00
fi
2014-09-07 19:56:34 +00:00
done <<< $(sed "s:${HOME}:~:g" $WD_CONFIG)
}
2015-01-25 23:13:39 +00:00
wd_ls()
{
wd_getdir $1
ls $dir
}
wd_path()
{
wd_getdir $1
echo $(echo $dir | sed "s:${HOME}:~:g")
}
wd_show()
{
2014-09-07 19:56:34 +00:00
local name_arg=$1
# if there's an argument we look up the value
if [[ ! -z $name_arg ]]
then
if [[ -z $points[$name_arg] ]]
then
wd_print_msg $WD_BLUE "No warp point named $name_arg"
else
wd_print_msg $WD_GREEN "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
fi
else
# hax to create a local empty array
local wd_matches
wd_matches=()
# do a reverse lookup to check whether PWD is in $points
if [[ ${points[(r)$PWD]} == $PWD ]]
then
for name in ${(k)points}
do
if [[ $points[$name] == $PWD ]]
then
wd_matches[$(($#wd_matches+1))]=$name
fi
done
wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
else
2014-09-08 07:09:44 +00:00
wd_print_msg $WD_YELLOW "No warp point to $(echo $PWD | sed "s:$HOME:~:")"
2014-09-07 19:56:34 +00:00
fi
fi
2013-11-16 01:15:17 +00:00
}
2014-09-07 19:56:34 +00:00
wd_clean() {
local force=$1
local count=0
local wd_tmp=""
while read line
do
if [[ $line != "" ]]
then
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
2014-09-07 19:56:34 +00:00
if [ -d "$val" ]
then
wd_tmp=$wd_tmp"\n"`echo $line`
else
wd_print_msg $WD_YELLOW "Nonexistent directory: ${key} -> ${val}"
count=$((count+1))
fi
fi
done < $WD_CONFIG
if [[ $count -eq 0 ]]
2013-11-16 01:15:17 +00:00
then
2014-09-07 19:56:34 +00:00
wd_print_msg $WD_BLUE "No warp points to clean, carry on!"
2013-11-16 01:15:17 +00:00
else
2014-09-07 19:56:34 +00:00
if $force || wd_yesorno "Removing ${count} warp points. Continue? (Y/n)"
then
echo $wd_tmp >! $WD_CONFIG
wd_print_msg $WD_GREEN "Cleanup complete. ${count} warp point(s) removed"
else
wd_print_msg $WD_BLUE "Cleanup aborted"
fi
2013-11-16 01:15:17 +00:00
fi
}
2014-09-07 19:56:34 +00:00
local WD_CONFIG=$HOME/.warprc
local WD_QUIET=0
local WD_EXIT_CODE=0
local WD_DEBUG=0
2014-09-07 19:56:34 +00:00
# Parse 'meta' options first to avoid the need to have them before
# other commands. The `-D` flag consumes recognized options so that
# the actual command parsing won't be affected.
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
zparseopts -D -E \
c:=wd_alt_config -config:=wd_alt_config \
q=wd_quiet_mode -quiet=wd_quiet_mode \
v=wd_print_version -version=wd_print_version \
d=wd_debug_mode -debug=wd_debug_mode
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
if [[ ! -z $wd_print_version ]]
then
echo "wd version $WD_VERSION"
fi
if [[ ! -z $wd_alt_config ]]
then
WD_CONFIG=$wd_alt_config[2]
fi
# check if config file exists
if [ ! -e $WD_CONFIG ]
then
# if not, create config file
touch $WD_CONFIG
fi
# load warp points
typeset -A points
while read -r line
do
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
points[$key]=$val
done < $WD_CONFIG
2013-11-16 01:15:17 +00:00
# get opts
2015-01-25 23:13:39 +00:00
args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,list,ls:,path:,help,show -- $*)
2013-11-16 01:15:17 +00:00
2014-09-07 19:56:34 +00:00
# check if no arguments were given, and that version is not set
if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
2013-11-16 01:15:17 +00:00
then
wd_print_usage
2014-09-07 19:56:34 +00:00
# check if config file is writeable
elif [ ! -w $WD_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
2014-09-07 19:56:34 +00:00
wd_exit_fail "\'$WD_CONFIG\' is not writeable."
2013-11-25 23:45:24 +00:00
else
2014-09-07 19:56:34 +00:00
# parse rest of options
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
;;
2015-01-25 23:13:39 +00:00
-l|list)
2014-03-04 19:25:54 +00:00
wd_list_all
2013-11-16 01:15:17 +00:00
break
;;
2015-01-25 23:13:39 +00:00
-ls|ls)
wd_ls $2
break
;;
-p|--path|path)
wd_path $2
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)
2014-09-07 19:56:34 +00:00
wd_show $2
break
;;
-c|--clean|clean)
wd_clean false
break
;;
-c!|--clean!|clean!)
wd_clean true
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
2014-09-07 19:56:34 +00:00
unset wd_yesorno
unset wd_print_usage
2014-09-07 19:56:34 +00:00
unset wd_alt_config
unset wd_quiet_mode
unset wd_print_version
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
2014-09-07 19:56:34 +00:00
if [[ ! -z $wd_debug_mode ]]
then
exit $WD_EXIT_CODE
else
unset wd_debug_mode
fi