diff options
Diffstat (limited to 'plugins/wd')
-rw-r--r-- | plugins/wd/LICENSE | 21 | ||||
-rw-r--r-- | plugins/wd/README.md | 26 | ||||
-rw-r--r-- | plugins/wd/_wd.sh | 103 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/wd/wd.plugin.zsh | 8 | ||||
-rwxr-xr-x | plugins/wd/wd.sh | 202 |
5 files changed, 209 insertions, 151 deletions
diff --git a/plugins/wd/LICENSE b/plugins/wd/LICENSE new file mode 100644 index 000000000..8caa6c6ce --- /dev/null +++ b/plugins/wd/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Markus Færevaag + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.
\ No newline at end of file diff --git a/plugins/wd/README.md b/plugins/wd/README.md index f9f4e7ac1..bc0ebe334 100644 --- a/plugins/wd/README.md +++ b/plugins/wd/README.md @@ -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 + $ wd rm foo - * List warp points to current directory (stored in `~/.warprc`): + * List all warp points (stored in `~/.warprc`): - wd show + $ wd ls - * List all warp points (stored in `~/.warprc`): + * List warp points to current directory - wd ls + $ wd show * Print usage with no opts or the `help` argument. diff --git a/plugins/wd/_wd.sh b/plugins/wd/_wd.sh index 950564435..0b03d8fff 100644 --- a/plugins/wd/_wd.sh +++ b/plugins/wd/_wd.sh @@ -1,48 +1,61 @@ -#compdef wd.sh - -zstyle ":completion:*:descriptions" format "%B%d%b" - -CONFIG=$HOME/.warprc - -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' -) - -local -a points -while read line -do - points+=$(awk "{ gsub(/\/Users\/$USER|\/home\/$USER/,\"~\"); print }" <<< $line) -done < $CONFIG - -_wd() -{ - # init variables - local curcontext="$curcontext" state line - typeset -A opt_args - - # init state - _arguments \ - '1: :->command' \ - '2: :->argument' - - 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 +#compdef wd + +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 + +zmodload zsh/mapfile + +function _wd() { + local CONFIG=$HOME/.warprc + local ret=1 + + local -a commands + local -a warp_points + + warp_points=( "${(f)mapfile[$CONFIG]//$HOME/~}" ) + + 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 diff --git a/plugins/wd/wd.plugin.zsh b/plugins/wd/wd.plugin.zsh index bbec4a715..c0559293d 100755..100644 --- a/plugins/wd/wd.plugin.zsh +++ b/plugins/wd/wd.plugin.zsh @@ -1,9 +1,11 @@ #!/bin/zsh -# WARP -# ==== +# WARP DIRECTORY +# ============== # oh-my-zsh plugin # # @github.com/mfaerevaag/wd -alias wd='. $ZSH/plugins/wd/wd.sh' +wd() { + . $ZSH/plugins/wd/wd.sh +} diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index 744f58bc2..dfb9ad89a 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -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 - touch $CONFIG + # 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,70 +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 "Unkown warp point '$1'" + wd_print_msg $RED "Unknown warp point '${point}'" fi } wd_add() { - if [[ $1 =~ "^\.+$" ]] + local force=$1 + local point=$2 + + if [[ $point =~ "^[\.]+$" ]] + then + wd_print_msg $RED "Warp point cannot be just dots" + elif [[ $point =~ "(\s|\ )+" ]] then - wd_print_msg $RED "Illeagal warp point (see README)." - elif [[ ${points[$1]} == "" ]] || $2 + wd_print_msg $RED "Warp point should not contain whitespace" + elif [[ $point == *:* ]] then - wd_remove $1 > /dev/null - print "$1:$PWD" >> $CONFIG + 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 '$1' alredy 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 - 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 @@ -110,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 remove 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 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:,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 ]] @@ -149,68 +173,64 @@ 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" - in - -a|--add|add) - wd_add $2 false - shift - shift + case "$o" + 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 + wd_warp $o break ;; - --) - shift; break;; - esac + --) + break + ;; + esac done 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 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 |