Updated wd plugin to v0.3.0

This commit is contained in:
Markus Faerevaag 2014-03-04 20:25:54 +01:00
parent 6b3c953775
commit d41ac7fe3e
3 changed files with 95 additions and 80 deletions

View File

@ -1,48 +1,68 @@
#compdef wd.sh
#compdef wd
zstyle ":completion:*:descriptions" format "%B%d%b"
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion::complete:wd:*:commands' group-name commands
zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
zstyle ':completion::complete:wd::' list-grouped
CONFIG=$HOME/.warprc
# Call `_wd()` when when trying to complete the command `wd`
local -a main_commands
main_commands=(
add:'Adds the current working directory to your warp points'
#add'\!':'Overwrites existing warp point' # TODO: Fix
rm:'Removes the given warp point'
ls:'Outputs all stored warp points'
show:'Outputs warp points to current directory'
)
zmodload zsh/mapfile
function _wd() {
local ret=1
local CONFIG=$HOME/.warprc
local -a points
while read line
do
points+=$(awk "{ gsub(/\/Users\/$USER|\/home\/$USER/,\"~\"); print }" <<< $line)
done < $CONFIG
# Stolen from
# http://stackoverflow.com/questions/9000698/completion-when-program-has-sub-commands
_wd()
{
# init variables
local curcontext="$curcontext" state line
typeset -A opt_args
# local curcontext="$curcontext" state line
# typeset -A opt_args
# init state
_arguments \
'1: :->command' \
'2: :->argument'
local -a commands
local -a warp_points
warp_points=( "${(f)mapfile[$CONFIG]}" )
# LIST="${mapfile[$FNAME]}" # Not required unless stuff uses it
case $state in
command)
compadd "$@" add rm ls show
_describe -t warp-points 'Warp points:' points && ret=0
;;
argument)
case $words[2] in
rm|add!)
_describe -t warp-points 'warp points' points && ret=0
;;
*)
esac
esac
commands=(
'add:Adds the current working directory to your warp points'
'add!:Overwrites existing warp point'
'rm:Removes the given warp point'
'ls:Outputs all stored warp points'
'show:Outputs all warp points that point to the current directory'
'help:Show this extremely helpful text'
'..:Go back to last directory'
)
_arguments -C \
'1: :->first_arg' \
'2: :->second_arg' && ret=0
case $state in
first_arg)
_describe -t warp_points "Warp points" warp_points && ret=0
_describe -t commands "Commands" commands && ret=0
;;
second_arg)
case $words[2] in
add\!|rm)
_describe -t points "Warp points" warp_points && ret=0
;;
add)
_message 'Write the name of your warp point' && ret=0
;;
esac
;;
esac
return $ret
}
_wd "$@"
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

View File

@ -6,4 +6,6 @@
#
# @github.com/mfaerevaag/wd
alias wd='. $ZSH/plugins/wd/wd.sh'
wd() {
. $ZSH/plugins/wd/wd.sh
}

View File

@ -50,12 +50,12 @@ wd_warp()
wd_print_msg $YELLOW "Warping to current directory?"
else
(( n = $#1 - 1 ))
wd_print_msg $BLUE "Warping..."
#wd_print_msg $BLUE "Warping..."
cd -$n > /dev/null
fi
elif [[ ${points[$1]} != "" ]]
then
wd_print_msg $BLUE "Warping..."
#wd_print_msg $BLUE "Warping..."
cd ${points[$1]}
else
wd_print_msg $RED "Unkown warp point '$1'"
@ -64,16 +64,16 @@ wd_warp()
wd_add()
{
if [[ $1 =~ "^\.+$" ]]
if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
then
wd_print_msg $RED "Illeagal warp point (see README)."
elif [[ ${points[$1]} == "" ]] || $2
wd_print_msg $RED "Illegal warp point (see README)."
elif [[ ${points[$2]} == "" ]] || $1
then
wd_remove $1 > /dev/null
print "$1:$PWD" >> $CONFIG
wd_remove $2 > /dev/null
print "$2:$PWD" >> $CONFIG
wd_print_msg $GREEN "Warp point added"
else
wd_print_msg $YELLOW "Warp point '$1' alredy exists. Use 'add!' to overwrite."
wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
fi
}
@ -83,7 +83,9 @@ wd_remove()
then
if wd_tmp=`sed "/^$1:/d" $CONFIG`
then
echo $wd_tmp > $CONFIG
# `>!` forces overwrite
# we need this if people use `setopt NO_CLOBBER`
echo $wd_tmp >! $CONFIG
wd_print_msg $GREEN "Warp point removed"
else
wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
@ -131,9 +133,9 @@ wd_print_usage()
print "\nCommands:"
print "\t add \t Adds the current working directory to your warp points"
print "\t add! \t Overwrites existing warp point"
print "\t remove Removes the given warp point"
print "\t rm \t Removes the given warp point"
print "\t show \t Outputs warp points to current directory"
print "\t list \t Outputs all stored warp points"
print "\t ls \t Outputs all stored warp points"
print "\t help \t Show this extremely helpful text"
}
@ -141,7 +143,7 @@ wd_print_usage()
## run
# get opts
args=`getopt -o a:r:lhs -l add:,remove:,list,help,show -- $*`
args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*`
# check if no arguments were given
if [[ $? -ne 0 || $#* -eq 0 ]]
@ -161,49 +163,40 @@ else
for i
do
case "$i"
in
-a|--add|add)
wd_add $2 false
shift
shift
case "$i"
in
-a|--add|add)
wd_add false $2
break
;;
-a!|--add!|add!)
wd_add $2 true
shift
shift
wd_add true $2
break
;;
-r|--remove|rm)
wd_remove $2
shift
shift
-r|--remove|rm)
wd_remove $2
break
;;
-l|--list|ls)
wd_list_all
shift
-l|--list|ls)
wd_list_all
break
;;
-h|--help|help)
wd_print_usage
shift
-h|--help|help)
wd_print_usage
break
;;
-s|--show|show)
wd_show
shift
-s|--show|show)
wd_show
break
;;
*)
wd_warp $i
shift
break
;;
--)
shift; break;;
esac
--)
break
;;
esac
done
fi
@ -211,6 +204,6 @@ fi
## garbage collection
# if not, next time warp will pick up variables from this run
# remember, there's no sub shell
points=""
args=""
unhash -d val &> /dev/null # fixes issue #1
unset points
unset args
unset val &> /dev/null # fixes issue #1