mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-11-19 21:41:07 +01:00
[wd] v0.3.1: Improved completion and bug fixes
This commit is contained in:
parent
c925aabea9
commit
346f6b7d24
4 changed files with 114 additions and 92 deletions
|
@ -2,37 +2,39 @@
|
||||||
|
|
||||||
**Maintainer:** [mfaerevaag](https://github.com/mfaerevaag)
|
**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
|
### Usage
|
||||||
|
|
||||||
* Add warp point to current working directory:
|
* 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.
|
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 ..
|
* You can warp back to previous directory, and so on, with this dot syntax:
|
||||||
wd ...
|
|
||||||
|
$ wd ..
|
||||||
|
$ wd ...
|
||||||
|
|
||||||
This is a wrapper for the zsh `dirs` function.
|
This is a wrapper for the zsh `dirs` function.
|
||||||
|
|
||||||
* Remove warp point test point:
|
* Remove warp point test point:
|
||||||
|
|
||||||
wd rm test
|
$ wd rm foo
|
||||||
|
|
||||||
* List warp points to current directory (stored in `~/.warprc`):
|
|
||||||
|
|
||||||
wd show
|
|
||||||
|
|
||||||
* List all warp points (stored in `~/.warprc`):
|
* 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.
|
* Print usage with no opts or the `help` argument.
|
||||||
|
|
|
@ -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:*:warp_points' group-name warp_points
|
||||||
zstyle ':completion::complete:wd::' list-grouped
|
zstyle ':completion::complete:wd::' list-grouped
|
||||||
|
|
||||||
# Call `_wd()` when when trying to complete the command `wd`
|
|
||||||
|
|
||||||
zmodload zsh/mapfile
|
zmodload zsh/mapfile
|
||||||
|
|
||||||
function _wd() {
|
function _wd() {
|
||||||
local ret=1
|
|
||||||
local CONFIG=$HOME/.warprc
|
local CONFIG=$HOME/.warprc
|
||||||
|
local ret=1
|
||||||
# Stolen from
|
|
||||||
# http://stackoverflow.com/questions/9000698/completion-when-program-has-sub-commands
|
|
||||||
|
|
||||||
# local curcontext="$curcontext" state line
|
|
||||||
# typeset -A opt_args
|
|
||||||
|
|
||||||
local -a commands
|
local -a commands
|
||||||
local -a warp_points
|
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=(
|
commands=(
|
||||||
'add:Adds the current working directory to your warp points'
|
'add:Adds the current working directory to your warp points'
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/zsh
|
#!/bin/zsh
|
||||||
|
|
||||||
# WARP
|
# WARP DIRECTORY
|
||||||
# ====
|
# ==============
|
||||||
# oh-my-zsh plugin
|
# oh-my-zsh plugin
|
||||||
#
|
#
|
||||||
# @github.com/mfaerevaag/wd
|
# @github.com/mfaerevaag/wd
|
||||||
|
|
157
plugins/wd/wd.sh
157
plugins/wd/wd.sh
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/zsh
|
#!/bin/zsh
|
||||||
|
|
||||||
# WARP
|
# WARP DIRECTORY
|
||||||
# ====
|
# ==============
|
||||||
# Jump to custom directories in terminal
|
# Jump to custom directories in terminal
|
||||||
# because `cd` takes too long...
|
# because `cd` takes too long...
|
||||||
#
|
#
|
||||||
|
@ -9,26 +9,28 @@
|
||||||
|
|
||||||
|
|
||||||
## variables
|
## variables
|
||||||
CONFIG=$HOME/.warprc
|
readonly CONFIG=$HOME/.warprc
|
||||||
|
|
||||||
## colors
|
# colors
|
||||||
BLUE="\033[96m"
|
readonly BLUE="\033[96m"
|
||||||
GREEN="\033[92m"
|
readonly GREEN="\033[92m"
|
||||||
YELLOW="\033[93m"
|
readonly YELLOW="\033[93m"
|
||||||
RED="\033[91m"
|
readonly RED="\033[91m"
|
||||||
NOC="\033[m"
|
readonly NOC="\033[m"
|
||||||
|
|
||||||
|
|
||||||
|
## init
|
||||||
|
|
||||||
# check if config file exists
|
# check if config file exists
|
||||||
if [[ ! -a $CONFIG ]]
|
if [ ! -e $CONFIG ]
|
||||||
then
|
then
|
||||||
# if not: create config file
|
# if not, create config file
|
||||||
touch $CONFIG
|
touch $CONFIG
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## load warp points
|
# load warp points
|
||||||
typeset -A points
|
typeset -A points
|
||||||
while read line
|
while read -r line
|
||||||
do
|
do
|
||||||
arr=(${(s,:,)line})
|
arr=(${(s,:,)line})
|
||||||
key=${arr[1]}
|
key=${arr[1]}
|
||||||
|
@ -39,72 +41,78 @@ done < $CONFIG
|
||||||
|
|
||||||
|
|
||||||
## functions
|
## functions
|
||||||
# prepended wd_ to not conflict with your environment (no sub shell)
|
|
||||||
|
|
||||||
wd_warp()
|
wd_warp()
|
||||||
{
|
{
|
||||||
if [[ $1 =~ "^\.+$" ]]
|
local point=$1
|
||||||
|
|
||||||
|
if [[ $point =~ "^\.+$" ]]
|
||||||
then
|
then
|
||||||
if [[ $#1 < 2 ]]
|
if [ $#1 < 2 ]
|
||||||
then
|
then
|
||||||
wd_print_msg $YELLOW "Warping to current directory?"
|
wd_print_msg $YELLOW "Warping to current directory?"
|
||||||
else
|
else
|
||||||
(( n = $#1 - 1 ))
|
(( n = $#1 - 1 ))
|
||||||
#wd_print_msg $BLUE "Warping..."
|
|
||||||
cd -$n > /dev/null
|
cd -$n > /dev/null
|
||||||
fi
|
fi
|
||||||
elif [[ ${points[$1]} != "" ]]
|
elif [[ ${points[$point]} != "" ]]
|
||||||
then
|
then
|
||||||
#wd_print_msg $BLUE "Warping..."
|
cd ${points[$point]}
|
||||||
cd ${points[$1]}
|
|
||||||
else
|
else
|
||||||
wd_print_msg $RED "Unknown warp point '$1'"
|
wd_print_msg $RED "Unknown warp point '${point}'"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
wd_add()
|
wd_add()
|
||||||
{
|
{
|
||||||
if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
|
local force=$1
|
||||||
|
local point=$2
|
||||||
|
|
||||||
|
if [[ $point =~ "^[\.]+$" ]]
|
||||||
then
|
then
|
||||||
wd_print_msg $RED "Illegal warp point (see README)."
|
wd_print_msg $RED "Warp point cannot be just dots"
|
||||||
elif [[ ${points[$2]} == "" ]] || $1
|
elif [[ $point =~ "(\s|\ )+" ]]
|
||||||
then
|
then
|
||||||
wd_remove $2 > /dev/null
|
wd_print_msg $RED "Warp point should not contain whitespace"
|
||||||
print "$2:$PWD" >> $CONFIG
|
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"
|
wd_print_msg $GREEN "Warp point added"
|
||||||
else
|
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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
wd_remove()
|
wd_remove()
|
||||||
{
|
{
|
||||||
if [[ ${points[$1]} != "" ]]
|
local point=$1
|
||||||
|
|
||||||
|
if [[ ${points[$point]} != "" ]]
|
||||||
then
|
then
|
||||||
if wd_tmp=`sed "/^$1:/d" $CONFIG`
|
if sed -i.bak "s,^${point}:.*$,,g" $CONFIG
|
||||||
then
|
then
|
||||||
# `>!` forces overwrite
|
|
||||||
# we need this if people use `setopt NO_CLOBBER`
|
|
||||||
echo $wd_tmp >! $CONFIG
|
|
||||||
wd_print_msg $GREEN "Warp point removed"
|
wd_print_msg $GREEN "Warp point removed"
|
||||||
else
|
else
|
||||||
wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
|
wd_print_msg $RED "Something bad happened! Sorry."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
wd_print_msg $RED "Warp point was not found"
|
wd_print_msg $RED "Warp point was not found"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
wd_show()
|
|
||||||
{
|
|
||||||
wd_print_msg $BLUE "Warp points to current directory:"
|
|
||||||
wd_list_all | grep $PWD$
|
|
||||||
}
|
|
||||||
|
|
||||||
wd_list_all()
|
wd_list_all()
|
||||||
{
|
{
|
||||||
wd_print_msg $BLUE "All warp points:"
|
wd_print_msg $BLUE "All warp points:"
|
||||||
while read line
|
|
||||||
|
while IFS= read -r line
|
||||||
do
|
do
|
||||||
if [[ $line != "" ]]
|
if [[ $line != "" ]]
|
||||||
then
|
then
|
||||||
|
@ -112,38 +120,52 @@ wd_list_all()
|
||||||
key=${arr[1]}
|
key=${arr[1]}
|
||||||
val=${arr[2]}
|
val=${arr[2]}
|
||||||
|
|
||||||
print "\t" $key "\t -> \t" $val
|
printf "%20s -> %s\n" $key $val
|
||||||
fi
|
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()
|
wd_print_msg()
|
||||||
{
|
{
|
||||||
if [[ $1 == "" || $2 == "" ]]
|
local color=$1
|
||||||
|
local msg=$2
|
||||||
|
|
||||||
|
if [[ $color == "" || $msg == "" ]]
|
||||||
then
|
then
|
||||||
print " $RED*$NOC Could not print message. Sorry!"
|
print " ${RED}*${NOC} Could not print message. Sorry!"
|
||||||
else
|
else
|
||||||
print " $1*$NOC $2"
|
print " ${color}*${NOC} ${msg}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
wd_print_usage()
|
wd_print_usage()
|
||||||
{
|
{
|
||||||
print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
|
cat <<- EOF
|
||||||
print "\nCommands:"
|
Usage: wd [add|-a|--add] [rm|-r|--remove] <point>
|
||||||
print "\t add \t Adds the current working directory to your warp points"
|
|
||||||
print "\t add! \t Overwrites existing warp point"
|
Commands:
|
||||||
print "\t rm \t Removes the given warp point"
|
add Adds the current working directory to your warp points
|
||||||
print "\t show \t Outputs warp points to current directory"
|
add! Overwrites existing warp point
|
||||||
print "\t ls \t Outputs all stored warp points"
|
rm Removes the given warp point
|
||||||
print "\t help \t Show this extremely helpful text"
|
show Outputs warp points to current directory
|
||||||
|
ls Outputs all stored warp points
|
||||||
|
help Show this extremely helpful text
|
||||||
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## run
|
## run
|
||||||
|
|
||||||
# get opts
|
# 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
|
# check if no arguments were given
|
||||||
if [[ $? -ne 0 || $#* -eq 0 ]]
|
if [[ $? -ne 0 || $#* -eq 0 ]]
|
||||||
|
@ -151,19 +173,16 @@ then
|
||||||
wd_print_usage
|
wd_print_usage
|
||||||
|
|
||||||
# check if config file is writeable
|
# check if config file is writeable
|
||||||
elif [[ ! -w $CONFIG ]]
|
elif [ ! -w $CONFIG ]
|
||||||
then
|
then
|
||||||
wd_print_msg $RED "\'$CONFIG\' is not writeable."
|
# do nothing
|
||||||
# do nothing => exit
|
|
||||||
# can't run `exit`, as this would exit the executing shell
|
# can't run `exit`, as this would exit the executing shell
|
||||||
# i.e. your terminal
|
wd_print_msg $RED "\'$CONFIG\' is not writeable."
|
||||||
|
|
||||||
else
|
else
|
||||||
#set -- $args # WTF
|
for o
|
||||||
|
|
||||||
for i
|
|
||||||
do
|
do
|
||||||
case "$i"
|
case "$o"
|
||||||
in
|
in
|
||||||
-a|--add|add)
|
-a|--add|add)
|
||||||
wd_add false $2
|
wd_add false $2
|
||||||
|
@ -190,7 +209,7 @@ else
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
wd_warp $i
|
wd_warp $o
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
|
@ -200,10 +219,18 @@ else
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
## garbage collection
|
## garbage collection
|
||||||
# if not, next time warp will pick up variables from this run
|
# if not, next time warp will pick up variables from this run
|
||||||
# remember, there's no sub shell
|
# 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 args
|
||||||
|
unset points
|
||||||
unset val &> /dev/null # fixes issue #1
|
unset val &> /dev/null # fixes issue #1
|
||||||
|
|
Loading…
Reference in a new issue