From 76e23e62c659fd61cf43b5e2cde350fd1d434d43 Mon Sep 17 00:00:00 2001 From: ncanceill Date: Thu, 29 May 2014 10:58:51 +0200 Subject: Fix permissions on wd plugin continuating #1923 --- plugins/wd/wd.plugin.zsh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 plugins/wd/wd.plugin.zsh (limited to 'plugins/wd') diff --git a/plugins/wd/wd.plugin.zsh b/plugins/wd/wd.plugin.zsh old mode 100755 new mode 100644 -- cgit v1.2.3-70-g09d2 From 6adc89131c1cf9fe79c3eaa3df1cd65a292bfc4d Mon Sep 17 00:00:00 2001 From: Simon Frost Date: Tue, 15 Jul 2014 14:20:39 +0100 Subject: Fix typo in warp directory plugin --- plugins/wd/wd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/wd') diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index 96a3426fc..9ebad6808 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -58,7 +58,7 @@ wd_warp() #wd_print_msg $BLUE "Warping..." cd ${points[$1]} else - wd_print_msg $RED "Unkown warp point '$1'" + wd_print_msg $RED "Unknown warp point '$1'" fi } -- cgit v1.2.3-70-g09d2 From 346f6b7d24d6021efa8c5167088f98c10f00ad3f Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Tue, 15 Jul 2014 17:42:30 +0200 Subject: [wd] v0.3.1: Improved completion and bug fixes --- plugins/wd/README.md | 26 ++++---- plugins/wd/_wd.sh | 15 ++--- plugins/wd/wd.plugin.zsh | 4 +- plugins/wd/wd.sh | 159 +++++++++++++++++++++++++++-------------------- 4 files changed, 113 insertions(+), 91 deletions(-) (limited to 'plugins/wd') 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 29df63520..0b03d8fff 100644 --- a/plugins/wd/_wd.sh +++ b/plugins/wd/_wd.sh @@ -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' diff --git a/plugins/wd/wd.plugin.zsh b/plugins/wd/wd.plugin.zsh index 9800335fc..c0559293d 100644 --- a/plugins/wd/wd.plugin.zsh +++ b/plugins/wd/wd.plugin.zsh @@ -1,7 +1,7 @@ #!/bin/zsh -# WARP -# ==== +# WARP DIRECTORY +# ============== # oh-my-zsh plugin # # @github.com/mfaerevaag/wd diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index 9ebad6808..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,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 "Warp point cannot be just dots" + elif [[ $point =~ "(\s|\ )+" ]] then - wd_print_msg $RED "Illegal warp point (see README)." - elif [[ ${points[$2]} == "" ]] || $1 + wd_print_msg $RED "Warp point should not contain whitespace" + elif [[ $point == *:* ]] then - wd_remove $2 > /dev/null - print "$2:$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 '$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] " - 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] + +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 -- cgit v1.2.3-70-g09d2 From a6b655fae9640873c1629abe7ba25f4823f4b714 Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Fri, 30 May 2014 10:21:16 +0200 Subject: [wd] v0.2.2: Added MIT-License --- plugins/wd/LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 plugins/wd/LICENSE (limited to 'plugins/wd') 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 -- cgit v1.2.3-70-g09d2 From 6855dd4727a395bfe8bd471c939819d907cb6c27 Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Sun, 7 Sep 2014 21:56:34 +0200 Subject: [wd] New minor version (v0.4) --- plugins/wd/README.md | 110 +++++++++++++++++- plugins/wd/_wd.sh | 6 +- plugins/wd/wd.sh | 318 +++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 355 insertions(+), 79 deletions(-) (limited to 'plugins/wd') diff --git a/plugins/wd/README.md b/plugins/wd/README.md index bc0ebe334..bcfeab684 100644 --- a/plugins/wd/README.md +++ b/plugins/wd/README.md @@ -1,8 +1,60 @@ -## wd +wd +== + +[![Build Status](https://travis-ci.org/mfaerevaag/wd.png?branch=master)](https://travis-ci.org/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. + + +### Setup + +### oh-my-zsh + +`wd` comes bundles with [oh-my-zshell](https://github.com/robbyrussell/oh-my-zsh)! + +Just add the plugin in your `~/.zshrc` file: + + plugins=(... wd) + + +#### Automatic + +Run either in terminal: + + * `curl -L https://github.com/mfaerevaag/wd/raw/master/install.sh | sh` + + * `wget --no-check-certificate https://github.com/mfaerevaag/wd/raw/master/install.sh -O - | sh` + + +#### Manual + + * Clone this repo to your liking + + * Add `wd` function to `.zshrc` (or `.profile` etc.): + + wd() { + . ~/paht/to/wd/wd.sh + } + + * Install manpage. From `wd`'s base directory (requires root permissions): + + # cp wd.1 /usr/share/man/man1/wd.1 + # chmod 644 /usr/share/man/man1/wd.1 + + Note, when pulling and updating `wd`, you'll need to do this again in case of changes to the manpage. + + +#### Completion + +If you're NOT using [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) and you want to utelize the zsh-completion feature, you will also need to add the path to your `wd` installation (`~/bin/wd` if you used the automatic installer) to your `fpath`. E.g. in your `~/.zshrc`: + + fpath=(~/path/to/wd $fpath) + +Also, you may have to force a rebuild of `zcompdump` by running: + + $ rm -f ~/.zcompdump; compinit -**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 @@ -24,6 +76,7 @@ $ wd ... This is a wrapper for the zsh `dirs` function. + (You might need `setopt AUTO_PUSHD` in your `.zshrc` if you hare not using [oh-my-zshell](https://github.com/robbyrussell/oh-my-zsh)). * Remove warp point test point: @@ -33,8 +86,55 @@ $ wd ls - * List warp points to current directory + * List warp points to current directory, or optionally, path to given warp point: $ wd show - * Print usage with no opts or the `help` argument. + * Remove warp points to non-existent directories. + + $ wd clean + + Use `clean!` to not be prompted with confirmation (force). + + * Print usage with no opts or the `help` argument: + + $ wd help + + * Print the running version of `wd`: + + $ wd --version + + * Specifically set the config file (default `~/.warprc`), which is useful when testing: + + $ wd --config ./file + + * Force `exit` with return code after running. This is not default, as it will *exit your terminal*, though required when testing/debugging. + + $ wd --debug + + * Silence all output: + + $ wd --quiet + + +### Testing + +`wd` comes with a small test suite, run with [shunit2](https://code.google.com/p/shunit2/). This can be used to confirm that things are working as it should on your setup, or to demonstrate an issue. + +To run, simply `cd` into the `test` directory and run the `tests.sh`. + + $ ./tests.sh + + +### License + +The project is licensed under the [MIT-license](https://github.com/mfaerevaag/wd/blob/master/LICENSE). + + +### Finally + +If you have issues, feedback or improvements, don't hesitate to report it or submit a pull-request. In the case of an issue, we would much appreciate if you would include a failing test in `test/tests.sh`. Explanation on how to run the tests, read the section "Testing" in this README. + +Credit to [altschuler](https://github.com/altschuler) for awesome idea. + +Hope you enjoy! diff --git a/plugins/wd/_wd.sh b/plugins/wd/_wd.sh index 0b03d8fff..2bb58dd31 100644 --- a/plugins/wd/_wd.sh +++ b/plugins/wd/_wd.sh @@ -21,7 +21,8 @@ function _wd() { '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' + 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point' + 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point' 'help:Show this extremely helpful text' '..:Go back to last directory' ) @@ -43,6 +44,9 @@ function _wd() { add) _message 'Write the name of your warp point' && ret=0 ;; + show) + _describe -t points "Warp points" warp_points && ret=0 + ;; esac ;; esac diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index dfb9ad89a..4e45b48e1 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -7,40 +7,108 @@ # # @github.com/mfaerevaag/wd - -## variables -readonly CONFIG=$HOME/.warprc +# version +readonly WD_VERSION=0.4 # colors -readonly BLUE="\033[96m" -readonly GREEN="\033[92m" -readonly YELLOW="\033[93m" -readonly RED="\033[91m" -readonly NOC="\033[m" +readonly WD_BLUE="\033[96m" +readonly WD_GREEN="\033[92m" +readonly WD_YELLOW="\033[93m" +readonly WD_RED="\033[91m" +readonly WD_NOC="\033[m" +## functions -## init +# helpers +wd_yesorno() +{ + # variables + local question="${1}" + local prompt="${question} " + local yes_RETVAL="0" + local no_RETVAL="3" + local RETVAL="" + local answer="" + + # read-eval loop + while true ; do + printf $prompt + read -r answer + + case ${answer:=${default}} in + Y|y|YES|yes|Yes ) + RETVAL=${yes_RETVAL} && \ + break + ;; + N|n|NO|no|No ) + RETVAL=${no_RETVAL} && \ + break + ;; + * ) + echo "Please provide a valid answer (y or n)" + ;; + esac + done -# check if config file exists -if [ ! -e $CONFIG ] -then - # if not, create config file - touch $CONFIG -fi + return ${RETVAL} +} -# load warp points -typeset -A points -while read -r line -do - arr=(${(s,:,)line}) - key=${arr[1]} - val=${arr[2]} +wd_print_msg() +{ + if [[ -z $wd_quiet_mode ]] + then + local color=$1 + local msg=$2 - points[$key]=$val -done < $CONFIG + if [[ $color == "" || $msg == "" ]] + then + print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!" + else + print " ${color}*${WD_NOC} ${msg}" + fi + fi +} +wd_print_usage() +{ + cat <<- EOF +Usage: wd [command] -## functions +Commands: + add Adds the current working directory to your warp points + add! Overwrites existing warp point + rm Removes the given warp point + show Print warp points to current directory + show Print path to given warp point + ls Print all stored warp points + clean! Remove points warping to nonexistent directories + + -v | --version Print version + -d | --debug Exit after execution with exit codes (for testing) + -c | --config Specify config file (default ~/.warprc) + -q | --quiet Suppress all output + + help Show this extremely helpful text +EOF +} + +wd_exit_fail() +{ + local msg=$1 + + wd_print_msg $WD_RED $1 + WD_EXIT_CODE=1 +} + +wd_exit_warn() +{ + local msg=$1 + + wd_print_msg $WD_YELLOW $msg + WD_EXIT_CODE=1 +} + +# core wd_warp() { @@ -50,7 +118,7 @@ wd_warp() then if [ $#1 < 2 ] then - wd_print_msg $YELLOW "Warping to current directory?" + wd_exit_warn "Warping to current directory?" else (( n = $#1 - 1 )) cd -$n > /dev/null @@ -59,7 +127,7 @@ wd_warp() then cd ${points[$point]} else - wd_print_msg $RED "Unknown warp point '${point}'" + wd_exit_fail "Unknown warp point '${point}'" fi } @@ -70,24 +138,28 @@ wd_add() if [[ $point =~ "^[\.]+$" ]] then - wd_print_msg $RED "Warp point cannot be just dots" + wd_exit_fail "Warp point cannot be just dots" elif [[ $point =~ "(\s|\ )+" ]] then - wd_print_msg $RED "Warp point should not contain whitespace" + wd_exit_fail "Warp point should not contain whitespace" elif [[ $point == *:* ]] then - wd_print_msg $RED "Warp point cannot contain colons" + wd_exit_fail "Warp point cannot contain colons" elif [[ $point == "" ]] then - wd_print_msg $RED "Warp point cannot be empty" + wd_exit_fail "Warp point cannot be empty" elif [[ ${points[$2]} == "" ]] || $force then wd_remove $point > /dev/null - printf "%q:%q\n" "${point}" "${PWD}" >> $CONFIG + printf "%q:%q\n" "${point}" "${PWD}" >> $WD_CONFIG + + wd_print_msg $WD_GREEN "Warp point added" - wd_print_msg $GREEN "Warp point added" + # override exit code in case wd_remove did not remove any points + # TODO: we should handle this kind of logic better + WD_EXIT_CODE=0 else - wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite." + wd_exit_warn "Warp point '${point}' already exists. Use 'add!' to overwrite." fi } @@ -97,20 +169,21 @@ wd_remove() if [[ ${points[$point]} != "" ]] then - if sed -i.bak "s,^${point}:.*$,,g" $CONFIG + local config_tmp=$WD_CONFIG.tmp + if sed -n "/^${point}:.*$/!p" $WD_CONFIG > $config_tmp && mv $config_tmp $WD_CONFIG then - wd_print_msg $GREEN "Warp point removed" + wd_print_msg $WD_GREEN "Warp point removed" else - wd_print_msg $RED "Something bad happened! Sorry." + wd_exit_fail "Something bad happened! Sorry." fi else - wd_print_msg $RED "Warp point was not found" + wd_exit_fail "Warp point was not found" fi } wd_list_all() { - wd_print_msg $BLUE "All warp points:" + wd_print_msg $WD_BLUE "All warp points:" while IFS= read -r line do @@ -120,66 +193,146 @@ wd_list_all() key=${arr[1]} val=${arr[2]} - printf "%20s -> %s\n" $key $val + if [[ -z $wd_quiet_mode ]] + then + printf "%20s -> %s\n" $key $val + fi fi - done <<< $(sed "s:${HOME}:~:g" $CONFIG) + done <<< $(sed "s:${HOME}:~:g" $WD_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}$" + local name_arg=$1 + # if there's an argument we look up the value + if [[ ! -z $name_arg ]] + then + if [[ -z $points[$name_arg] ]] + then + wd_print_msg $WD_BLUE "No warp point named $name_arg" + else + wd_print_msg $WD_GREEN "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]" + fi + else + # hax to create a local empty array + local wd_matches + wd_matches=() + # do a reverse lookup to check whether PWD is in $points + if [[ ${points[(r)$PWD]} == $PWD ]] + then + for name in ${(k)points} + do + if [[ $points[$name] == $PWD ]] + then + wd_matches[$(($#wd_matches+1))]=$name + fi + done + + wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}" + else + wd_print_msg $WD_BLUE "No warp points to $cwd" + fi + fi } -wd_print_msg() -{ - local color=$1 - local msg=$2 +wd_clean() { + local force=$1 + local count=0 + local wd_tmp="" + + while read line + do + if [[ $line != "" ]] + then + arr=(${(s,:,)line}) + key=${arr[1]} + val=${arr[2]} - if [[ $color == "" || $msg == "" ]] + if [ -d "$val" ] + then + wd_tmp=$wd_tmp"\n"`echo $line` + else + wd_print_msg $WD_YELLOW "Nonexistent directory: ${key} -> ${val}" + count=$((count+1)) + fi + fi + done < $WD_CONFIG + + if [[ $count -eq 0 ]] then - print " ${RED}*${NOC} Could not print message. Sorry!" + wd_print_msg $WD_BLUE "No warp points to clean, carry on!" else - print " ${color}*${NOC} ${msg}" + if $force || wd_yesorno "Removing ${count} warp points. Continue? (Y/n)" + then + echo $wd_tmp >! $WD_CONFIG + wd_print_msg $WD_GREEN "Cleanup complete. ${count} warp point(s) removed" + else + wd_print_msg $WD_BLUE "Cleanup aborted" + fi fi } -wd_print_usage() -{ - cat <<- EOF -Usage: wd [add|-a|--add] [rm|-r|--remove] +local WD_CONFIG=$HOME/.warprc +local WD_QUIET=0 +local WD_EXIT_CODE=0 +local WD_DEBUG=0 -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 -} +# Parse 'meta' options first to avoid the need to have them before +# other commands. The `-D` flag consumes recognized options so that +# the actual command parsing won't be affected. +zparseopts -D -E \ + c:=wd_alt_config -config:=wd_alt_config \ + q=wd_quiet_mode -quiet=wd_quiet_mode \ + v=wd_print_version -version=wd_print_version \ + d=wd_debug_mode -debug=wd_debug_mode -## run +if [[ ! -z $wd_print_version ]] +then + echo "wd version $WD_VERSION" +fi + +if [[ ! -z $wd_alt_config ]] +then + WD_CONFIG=$wd_alt_config[2] +fi + +# check if config file exists +if [ ! -e $WD_CONFIG ] +then + # if not, create config file + touch $WD_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 < $WD_CONFIG # get opts -args=$(getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*) +args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,ls,help,show -- $*) -# check if no arguments were given -if [[ $? -ne 0 || $#* -eq 0 ]] +# check if no arguments were given, and that version is not set +if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]] then wd_print_usage -# check if config file is writeable -elif [ ! -w $CONFIG ] + # check if config file is writeable +elif [ ! -w $WD_CONFIG ] then # do nothing # can't run `exit`, as this would exit the executing shell - wd_print_msg $RED "\'$CONFIG\' is not writeable." + wd_exit_fail "\'$WD_CONFIG\' is not writeable." else + + # parse rest of options for o do case "$o" @@ -205,7 +358,15 @@ else break ;; -s|--show|show) - wd_show + wd_show $2 + break + ;; + -c|--clean|clean) + wd_clean false + break + ;; + -c!|--clean!|clean!) + wd_clean true break ;; *) @@ -229,8 +390,19 @@ unset wd_remove unset wd_show unset wd_list_all unset wd_print_msg +unset wd_yesorno unset wd_print_usage +unset wd_alt_config +unset wd_quiet_mode +unset wd_print_version unset args unset points unset val &> /dev/null # fixes issue #1 + +if [[ ! -z $wd_debug_mode ]] +then + exit $WD_EXIT_CODE +else + unset wd_debug_mode +fi -- cgit v1.2.3-70-g09d2 From b5093859af472c04dfca33adfeba49bfbe417c9d Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Mon, 8 Sep 2014 09:09:44 +0200 Subject: [wd] Minor bug fix in show command --- plugins/wd/wd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/wd') diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index 4e45b48e1..d63e92472 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -230,7 +230,7 @@ wd_show() wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}" else - wd_print_msg $WD_BLUE "No warp points to $cwd" + wd_print_msg $WD_YELLOW "No warp point to $(echo $PWD | sed "s:$HOME:~:")" fi fi } -- cgit v1.2.3-70-g09d2 From 9f801ff48d46847e3c91e58b2ba2f5b61f6dcef8 Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Tue, 21 Oct 2014 09:51:13 +0200 Subject: [wd] fix space in path --- plugins/wd/wd.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/wd') diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index d63e92472..5ecbdc503 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -139,7 +139,7 @@ wd_add() if [[ $point =~ "^[\.]+$" ]] then wd_exit_fail "Warp point cannot be just dots" - elif [[ $point =~ "(\s|\ )+" ]] + elif [[ $point =~ "[[:space:]]+" ]] then wd_exit_fail "Warp point should not contain whitespace" elif [[ $point == *:* ]] @@ -151,7 +151,7 @@ wd_add() elif [[ ${points[$2]} == "" ]] || $force then wd_remove $point > /dev/null - printf "%q:%q\n" "${point}" "${PWD}" >> $WD_CONFIG + printf "%q:%s\n" "${point}" "${PWD}" >> $WD_CONFIG wd_print_msg $WD_GREEN "Warp point added" -- cgit v1.2.3-70-g09d2 From 4533c7a31c737622782ce3233d2eb536f9cb4f2e Mon Sep 17 00:00:00 2001 From: Markus Faerevaag Date: Mon, 26 Jan 2015 00:13:39 +0100 Subject: [wd] Update to v0.4.1 --- plugins/wd/README.md | 14 ++++++++++++-- plugins/wd/_wd.sh | 13 +++++++++++-- plugins/wd/wd.sh | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 8 deletions(-) (limited to 'plugins/wd') diff --git a/plugins/wd/README.md b/plugins/wd/README.md index bcfeab684..ed149eb3e 100644 --- a/plugins/wd/README.md +++ b/plugins/wd/README.md @@ -5,6 +5,8 @@ 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. +*NOTE*: If you are not using zsh, check out the `ruby` branch which has `wd` implemented as a gem. + ### Setup @@ -33,7 +35,7 @@ Run either in terminal: * Add `wd` function to `.zshrc` (or `.profile` etc.): wd() { - . ~/paht/to/wd/wd.sh + . ~/path/to/cloned/repo/wd/wd.sh } * Install manpage. From `wd`'s base directory (requires root permissions): @@ -84,7 +86,15 @@ Also, you may have to force a rebuild of `zcompdump` by running: * List all warp points (stored in `~/.warprc`): - $ wd ls + $ wd list + + * List files in given warp point: + + $ wd ls foo + + * Show path of given warp point: + + $ wd path foo * List warp points to current directory, or optionally, path to given warp point: diff --git a/plugins/wd/_wd.sh b/plugins/wd/_wd.sh index 2bb58dd31..b67f4a1e2 100644 --- a/plugins/wd/_wd.sh +++ b/plugins/wd/_wd.sh @@ -20,10 +20,13 @@ function _wd() { '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 or shows a specific target directory for a point' + 'list:Outputs all stored warp points' + 'ls:Show files from given warp point' + 'path:Show path to given warp point' 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point' 'help:Show this extremely helpful text' + 'clean:Remove points warping to nonexistent directories' + 'clean!:Remove nonexistent directories without confirmation' '..:Go back to last directory' ) @@ -47,6 +50,12 @@ function _wd() { show) _describe -t points "Warp points" warp_points && ret=0 ;; + ls) + _describe -t points "Warp points" warp_points && ret=0 + ;; + path) + _describe -t points "Warp points" warp_points && ret=0 + ;; esac ;; esac diff --git a/plugins/wd/wd.sh b/plugins/wd/wd.sh index 5ecbdc503..3b9548168 100755 --- a/plugins/wd/wd.sh +++ b/plugins/wd/wd.sh @@ -80,7 +80,9 @@ Commands: rm Removes the given warp point show Print warp points to current directory show Print path to given warp point - ls Print all stored warp points + list Print all stored warp points +ls Show files from given warp point +path Show the path to given warp point clean! Remove points warping to nonexistent directories -v | --version Print version @@ -96,7 +98,7 @@ wd_exit_fail() { local msg=$1 - wd_print_msg $WD_RED $1 + wd_print_msg $WD_RED $msg WD_EXIT_CODE=1 } @@ -108,6 +110,22 @@ wd_exit_warn() WD_EXIT_CODE=1 } +wd_getdir() +{ + local name_arg=$1 + + point=$(wd_show $name_arg) + dir=${point:28+$#name_arg+7} + + if [[ -z $name_arg ]]; then + wd_exit_fail "You must enter a warp point" + break + elif [[ -z $dir ]]; then + wd_exit_fail "Unknown warp point '${name_arg}'" + break + fi +} + # core wd_warp() @@ -201,6 +219,18 @@ wd_list_all() done <<< $(sed "s:${HOME}:~:g" $WD_CONFIG) } +wd_ls() +{ + wd_getdir $1 + ls $dir +} + +wd_path() +{ + wd_getdir $1 + echo $(echo $dir | sed "s:${HOME}:~:g") +} + wd_show() { local name_arg=$1 @@ -316,7 +346,7 @@ do done < $WD_CONFIG # get opts -args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,ls,help,show -- $*) +args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,list,ls:,path:,help,show -- $*) # check if no arguments were given, and that version is not set if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]] @@ -349,10 +379,18 @@ else wd_remove $2 break ;; - -l|--list|ls) + -l|list) wd_list_all break ;; + -ls|ls) + wd_ls $2 + break + ;; + -p|--path|path) + wd_path $2 + break + ;; -h|--help|help) wd_print_usage break -- cgit v1.2.3-70-g09d2