diff options
Diffstat (limited to 'plugins/wd')
-rw-r--r-- | plugins/wd/LICENSE | 21 | ||||
-rw-r--r-- | plugins/wd/README.md | 40 | ||||
-rw-r--r-- | plugins/wd/_wd.sh | 61 | ||||
-rw-r--r-- | plugins/wd/wd.plugin.zsh | 11 | ||||
-rwxr-xr-x | plugins/wd/wd.sh | 236 |
5 files changed, 369 insertions, 0 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 new file mode 100644 index 000000000..bc0ebe334 --- /dev/null +++ b/plugins/wd/README.md @@ -0,0 +1,40 @@ +## 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 foo + + If a warp point with the same name exists, use `add!` to overwrite it. + + 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. + + * From an other directory (not necessarily), warp to `foo` with: + + $ wd foo + + * 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 foo + + * List all warp points (stored in `~/.warprc`): + + $ wd ls + + * List warp points to current directory + + $ wd show + + * 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..0b03d8fff --- /dev/null +++ b/plugins/wd/_wd.sh @@ -0,0 +1,61 @@ +#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 new file mode 100644 index 000000000..c0559293d --- /dev/null +++ b/plugins/wd/wd.plugin.zsh @@ -0,0 +1,11 @@ +#!/bin/zsh + +# WARP DIRECTORY +# ============== +# 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..dfb9ad89a --- /dev/null +++ b/plugins/wd/wd.sh @@ -0,0 +1,236 @@ +#!/bin/zsh + +# WARP DIRECTORY +# ============== +# Jump to custom directories in terminal +# because `cd` takes too long... +# +# @github.com/mfaerevaag/wd + + +## variables +readonly CONFIG=$HOME/.warprc + +# 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 [ ! -e $CONFIG ] +then + # if not, create config file + touch $CONFIG +fi + +# load warp points +typeset -A points +while read -r line +do + arr=(${(s,:,)line}) + key=${arr[1]} + val=${arr[2]} + + points[$key]=$val +done < $CONFIG + + +## functions + +wd_warp() +{ + local point=$1 + + if [[ $point =~ "^\.+$" ]] + then + if [ $#1 < 2 ] + then + wd_print_msg $YELLOW "Warping to current directory?" + else + (( n = $#1 - 1 )) + cd -$n > /dev/null + fi + elif [[ ${points[$point]} != "" ]] + then + cd ${points[$point]} + else + wd_print_msg $RED "Unknown warp point '${point}'" + fi +} + +wd_add() +{ + 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 "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 '${point}' already exists. Use 'add!' to overwrite." + fi +} + +wd_remove() +{ + local point=$1 + + if [[ ${points[$point]} != "" ]] + then + if sed -i.bak "s,^${point}:.*$,,g" $CONFIG + then + wd_print_msg $GREEN "Warp point removed" + else + wd_print_msg $RED "Something bad happened! Sorry." + fi + else + wd_print_msg $RED "Warp point was not found" + fi +} + +wd_list_all() +{ + wd_print_msg $BLUE "All warp points:" + + while IFS= read -r line + do + if [[ $line != "" ]] + then + arr=(${(s,:,)line}) + key=${arr[1]} + val=${arr[2]} + + printf "%20s -> %s\n" $key $val + fi + 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() +{ + local color=$1 + local msg=$2 + + if [[ $color == "" || $msg == "" ]] + then + print " ${RED}*${NOC} Could not print message. Sorry!" + else + print " ${color}*${NOC} ${msg}" + fi +} + +wd_print_usage() +{ + 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 -- $*) + +# 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 + # do nothing + # can't run `exit`, as this would exit the executing shell + wd_print_msg $RED "\'$CONFIG\' is not writeable." + +else + for o + do + case "$o" + 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 $o + 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 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 |