diff options
Diffstat (limited to 'plugins/wd')
-rw-r--r-- | plugins/wd/README.md | 38 | ||||
-rw-r--r-- | plugins/wd/_wd.sh | 68 | ||||
-rwxr-xr-x | plugins/wd/wd.plugin.zsh | 11 | ||||
-rwxr-xr-x | plugins/wd/wd.sh | 209 |
4 files changed, 326 insertions, 0 deletions
diff --git a/plugins/wd/README.md b/plugins/wd/README.md new file mode 100644 index 000000000..f9f4e7ac1 --- /dev/null +++ b/plugins/wd/README.md @@ -0,0 +1,38 @@ +## wd + +**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) + +### Usage + + * Add warp point to current working directory: + + wd add test + + If a warp point with the same name exists, use `add!` to overwrite it. + + * From an other directory, warp to test with: + + wd test + + * You can warp back to previous directory, and so on, with the puncticulation 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 + + * List all warp points (stored in `~/.warprc`): + + wd ls + + * Print usage with no opts or the `help` argument. diff --git a/plugins/wd/_wd.sh b/plugins/wd/_wd.sh new file mode 100644 index 000000000..29df63520 --- /dev/null +++ b/plugins/wd/_wd.sh @@ -0,0 +1,68 @@ +#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 + +# 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 -a commands + local -a warp_points + warp_points=( "${(f)mapfile[$CONFIG]}" ) + # LIST="${mapfile[$FNAME]}" # Not required unless stuff uses it + + 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 new file mode 100755 index 000000000..9800335fc --- /dev/null +++ b/plugins/wd/wd.plugin.zsh @@ -0,0 +1,11 @@ +#!/bin/zsh + +# WARP +# ==== +# oh-my-zsh plugin +# +# @github.com/mfaerevaag/wd + +wd() { + . $ZSH/plugins/wd/wd.sh +} diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh new file mode 100755 index 000000000..96a3426fc --- /dev/null +++ b/plugins/wd/wd.sh @@ -0,0 +1,209 @@ +#!/bin/zsh + +# WARP +# ==== +# Jump to custom directories in terminal +# because `cd` takes too long... +# +# @github.com/mfaerevaag/wd + + +## variables +CONFIG=$HOME/.warprc + +## colors +BLUE="\033[96m" +GREEN="\033[92m" +YELLOW="\033[93m" +RED="\033[91m" +NOC="\033[m" + + +# check if config file exists +if [[ ! -a $CONFIG ]] +then + # if not: create config file + touch $CONFIG +fi + +## load warp points +typeset -A points +while read line +do + arr=(${(s,:,)line}) + key=${arr[1]} + val=${arr[2]} + + points[$key]=$val +done < $CONFIG + + +## functions +# prepended wd_ to not conflict with your environment (no sub shell) + +wd_warp() +{ + if [[ $1 =~ "^\.+$" ]] + then + 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]} != "" ]] + then + #wd_print_msg $BLUE "Warping..." + cd ${points[$1]} + else + wd_print_msg $RED "Unkown warp point '$1'" + fi +} + +wd_add() +{ + if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]] + then + wd_print_msg $RED "Illegal warp point (see README)." + elif [[ ${points[$2]} == "" ]] || $1 + then + wd_remove $2 > /dev/null + print "$2:$PWD" >> $CONFIG + wd_print_msg $GREEN "Warp point added" + else + wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite." + fi +} + +wd_remove() +{ + if [[ ${points[$1]} != "" ]] + then + if wd_tmp=`sed "/^$1:/d" $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!" + 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 + do + if [[ $line != "" ]] + then + arr=(${(s,:,)line}) + key=${arr[1]} + val=${arr[2]} + + print "\t" $key "\t -> \t" $val + fi + done < $CONFIG +} + +wd_print_msg() +{ + if [[ $1 == "" || $2 == "" ]] + then + print " $RED*$NOC Could not print message. Sorry!" + else + print " $1*$NOC $2" + 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" +} + + +## run + +# get opts +args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*` + +# check if no arguments were given +if [[ $? -ne 0 || $#* -eq 0 ]] +then + wd_print_usage + +# check if config file is writeable +elif [[ ! -w $CONFIG ]] +then + wd_print_msg $RED "\'$CONFIG\' is not writeable." + # do nothing => exit + # can't run `exit`, as this would exit the executing shell + # i.e. your terminal + +else + #set -- $args # WTF + + for i + do + case "$i" + in + -a|--add|add) + wd_add false $2 + break + ;; + -a!|--add!|add!) + wd_add true $2 + break + ;; + -r|--remove|rm) + wd_remove $2 + break + ;; + -l|--list|ls) + wd_list_all + break + ;; + -h|--help|help) + wd_print_usage + break + ;; + -s|--show|show) + wd_show + break + ;; + *) + wd_warp $i + break + ;; + --) + break + ;; + esac + 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 args +unset val &> /dev/null # fixes issue #1 |