diff options
author | Robby Russell <robby@planetargon.com> | 2014-08-31 10:45:40 -0700 |
---|---|---|
committer | Robby Russell <robby@planetargon.com> | 2014-08-31 10:45:40 -0700 |
commit | fcd55f3a9202ca8d47a82b4a0274bdd114d745d1 (patch) | |
tree | 2fcb38a80e64ecc1138317dd0cbeef9f31e8cfeb /plugins/wd/wd.sh | |
parent | 958c8f439058c2c253434a0860d29bf6c11cd559 (diff) | |
parent | 346f6b7d24d6021efa8c5167088f98c10f00ad3f (diff) | |
download | zsh-fcd55f3a9202ca8d47a82b4a0274bdd114d745d1.tar.gz zsh-fcd55f3a9202ca8d47a82b4a0274bdd114d745d1.tar.bz2 zsh-fcd55f3a9202ca8d47a82b4a0274bdd114d745d1.zip |
Merge pull request #2845 from mfaerevaag/master
[wd] New minor release
Diffstat (limited to 'plugins/wd/wd.sh')
-rwxr-xr-x | plugins/wd/wd.sh | 159 |
1 files changed, 93 insertions, 66 deletions
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] <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" + 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 -- $*` +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 |