diff options
Diffstat (limited to 'plugins/wd/wd.sh')
-rwxr-xr-x | plugins/wd/wd.sh | 318 |
1 files changed, 245 insertions, 73 deletions
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] <point> -## functions +Commands: + add <point> Adds the current working directory to your warp points + add! <point> Overwrites existing warp point + rm <point> Removes the given warp point + show Print warp points to current directory + show <point> 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] <point> +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 |