[wd] v0.3.1: Improved completion and bug fixes

This commit is contained in:
Markus Faerevaag 2014-07-15 17:42:30 +02:00
parent c925aabea9
commit 346f6b7d24
4 changed files with 114 additions and 92 deletions

View file

@ -2,37 +2,39 @@
**Maintainer:** [mfaerevaag](https://github.com/mfaerevaag)
`wd` (warp directory) lets you jump to custom directories in zsh, without using cd. Why? Because cd seems ineffecient when the folder is frequently visited or has a long path. [Source](https://github.com/mfaerevaag/wd)
`wd` (*warp directory*) lets you jump to custom directories in zsh, without using `cd`. Why? Because `cd` seems ineffecient when the folder is frequently visited or has a long path. [Source](https://github.com/mfaerevaag/wd)
### Usage
* Add warp point to current working directory:
wd add test
$ wd add foo
If a warp point with the same name exists, use `add!` to overwrite it.
* From an other directory, warp to test with:
Note, a warp point cannot contain colons, or only consist of only spaces and dots. The first will conflict in how `wd` stores the warp points, and the second will conflict other features, as below.
wd test
* From an other directory (not necessarily), warp to `foo` with:
* You can warp back to previous directory, and so on, with the puncticulation syntax:
$ wd foo
wd ..
wd ...
* You can warp back to previous directory, and so on, with this dot syntax:
$ wd ..
$ wd ...
This is a wrapper for the zsh `dirs` function.
* Remove warp point test point:
wd rm test
* List warp points to current directory (stored in `~/.warprc`):
wd show
$ wd rm foo
* List all warp points (stored in `~/.warprc`):
wd ls
$ wd ls
* List warp points to current directory
$ wd show
* Print usage with no opts or the `help` argument.

View file

@ -5,23 +5,16 @@ zstyle ':completion::complete:wd:*:commands' group-name commands
zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
zstyle ':completion::complete:wd::' list-grouped
# Call `_wd()` when when trying to complete the command `wd`
zmodload zsh/mapfile
function _wd() {
local ret=1
local CONFIG=$HOME/.warprc
# Stolen from
# http://stackoverflow.com/questions/9000698/completion-when-program-has-sub-commands
# local curcontext="$curcontext" state line
# typeset -A opt_args
local ret=1
local -a commands
local -a warp_points
warp_points=( "${(f)mapfile[$CONFIG]}" )
# LIST="${mapfile[$FNAME]}" # Not required unless stuff uses it
warp_points=( "${(f)mapfile[$CONFIG]//$HOME/~}" )
commands=(
'add:Adds the current working directory to your warp points'

View file

@ -1,7 +1,7 @@
#!/bin/zsh
# WARP
# ====
# WARP DIRECTORY
# ==============
# oh-my-zsh plugin
#
# @github.com/mfaerevaag/wd

View file

@ -1,7 +1,7 @@
#!/bin/zsh
# WARP
# ====
# WARP DIRECTORY
# ==============
# Jump to custom directories in terminal
# because `cd` takes too long...
#
@ -9,26 +9,28 @@
## variables
CONFIG=$HOME/.warprc
readonly CONFIG=$HOME/.warprc
## colors
BLUE="\033[96m"
GREEN="\033[92m"
YELLOW="\033[93m"
RED="\033[91m"
NOC="\033[m"
# colors
readonly BLUE="\033[96m"
readonly GREEN="\033[92m"
readonly YELLOW="\033[93m"
readonly RED="\033[91m"
readonly NOC="\033[m"
## init
# check if config file exists
if [[ ! -a $CONFIG ]]
if [ ! -e $CONFIG ]
then
# if not: create config file
# if not, create config file
touch $CONFIG
fi
## load warp points
# load warp points
typeset -A points
while read line
while read -r line
do
arr=(${(s,:,)line})
key=${arr[1]}
@ -39,72 +41,78 @@ done < $CONFIG
## functions
# prepended wd_ to not conflict with your environment (no sub shell)
wd_warp()
{
if [[ $1 =~ "^\.+$" ]]
local point=$1
if [[ $point =~ "^\.+$" ]]
then
if [[ $#1 < 2 ]]
if [ $#1 < 2 ]
then
wd_print_msg $YELLOW "Warping to current directory?"
else
(( n = $#1 - 1 ))
#wd_print_msg $BLUE "Warping..."
cd -$n > /dev/null
fi
elif [[ ${points[$1]} != "" ]]
elif [[ ${points[$point]} != "" ]]
then
#wd_print_msg $BLUE "Warping..."
cd ${points[$1]}
cd ${points[$point]}
else
wd_print_msg $RED "Unknown warp point '$1'"
wd_print_msg $RED "Unknown warp point '${point}'"
fi
}
wd_add()
{
if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
local force=$1
local point=$2
if [[ $point =~ "^[\.]+$" ]]
then
wd_print_msg $RED "Illegal warp point (see README)."
elif [[ ${points[$2]} == "" ]] || $1
wd_print_msg $RED "Warp point cannot be just dots"
elif [[ $point =~ "(\s|\ )+" ]]
then
wd_remove $2 > /dev/null
print "$2:$PWD" >> $CONFIG
wd_print_msg $RED "Warp point should not contain whitespace"
elif [[ $point == *:* ]]
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
wd_print_msg $GREEN "Warp point added"
else
wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite."
fi
}
wd_remove()
{
if [[ ${points[$1]} != "" ]]
local point=$1
if [[ ${points[$point]} != "" ]]
then
if wd_tmp=`sed "/^$1:/d" $CONFIG`
if sed -i.bak "s,^${point}:.*$,,g" $CONFIG
then
# `>!` 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!"
wd_print_msg $RED "Something bad happened! 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
while IFS= read -r line
do
if [[ $line != "" ]]
then
@ -112,38 +120,52 @@ wd_list_all()
key=${arr[1]}
val=${arr[2]}
print "\t" $key "\t -> \t" $val
printf "%20s -> %s\n" $key $val
fi
done < $CONFIG
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}$"
}
wd_print_msg()
{
if [[ $1 == "" || $2 == "" ]]
local color=$1
local msg=$2
if [[ $color == "" || $msg == "" ]]
then
print " $RED*$NOC Could not print message. Sorry!"
print " ${RED}*${NOC} Could not print message. Sorry!"
else
print " $1*$NOC $2"
print " ${color}*${NOC} ${msg}"
fi
}
wd_print_usage()
{
print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
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 rm \t Removes the given warp point"
print "\t show \t Outputs warp points to current directory"
print "\t ls \t Outputs all stored warp points"
print "\t help \t Show this extremely helpful text"
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
}
## run
# get opts
args=`getopt -o a:r:lhs -l add:,rm:,ls,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 ]]
@ -151,19 +173,16 @@ then
wd_print_usage
# check if config file is writeable
elif [[ ! -w $CONFIG ]]
elif [ ! -w $CONFIG ]
then
wd_print_msg $RED "\'$CONFIG\' is not writeable."
# do nothing => exit
# do nothing
# can't run `exit`, as this would exit the executing shell
# i.e. your terminal
wd_print_msg $RED "\'$CONFIG\' is not writeable."
else
#set -- $args # WTF
for i
for o
do
case "$i"
case "$o"
in
-a|--add|add)
wd_add false $2
@ -190,7 +209,7 @@ else
break
;;
*)
wd_warp $i
wd_warp $o
break
;;
--)
@ -200,10 +219,18 @@ else
done
fi
## garbage collection
# if not, next time warp will pick up variables from this run
# remember, there's no sub shell
unset points
unset wd_warp
unset wd_add
unset wd_remove
unset wd_show
unset wd_list_all
unset wd_print_msg
unset wd_print_usage
unset args
unset points
unset val &> /dev/null # fixes issue #1