From 47368ee743db13ad14d4849b8f3ee91c4f255e98 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 03:19:54 -0400 Subject: [PATCH 01/31] Browse with fzf Browse with fzf --- wd.plugin.zsh | 7 +- wd.sh | 322 ++++++++++++++++++++++++++------------------------ 2 files changed, 176 insertions(+), 153 deletions(-) diff --git a/wd.plugin.zsh b/wd.plugin.zsh index 87d1d08..a194896 100644 --- a/wd.plugin.zsh +++ b/wd.plugin.zsh @@ -7,4 +7,9 @@ # # @github.com/mfaerevaag/wd -eval "wd() { source '${0:A:h}/wd.sh' }" +source ${0:A:h}/wd.sh + +# Register the function as a Zsh widget +zle -N wd_browse +# Bind the widget to a key combination +bindkey '^G' wd_browse diff --git a/wd.sh b/wd.sh index 435ae7a..8848975 100755 --- a/wd.sh +++ b/wd.sh @@ -230,6 +230,17 @@ wd_remove() done } +wd_browse() { + local entries=("${(@f)$(sed "s:${HOME}:~:g" "$WD_CONFIG" | awk -F ':' '{print $1 " -> " $2}')}") + local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf-tmux --height 40% --reverse) + if [[ -n $selected_entry ]]; then + local selected_point="${selected_entry%% ->*}" + selected_point=$(echo "$selected_point" | xargs) + eval "wd $selected_point" + fi +} + + wd_list_all() { wd_print_msg "$WD_BLUE" "All warp points:" @@ -358,156 +369,163 @@ wd_export_static_named_directories() { fi } -local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} -local WD_QUIET=0 -local WD_EXIT_CODE=0 -local WD_DEBUG=0 - -# 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 \ - f=wd_force_mode -force=wd_force_mode - -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" -else - wd_export_static_named_directories -fi - -# disable extendedglob for the complete wd execution time -setopt | grep -q extendedglob -wd_extglob_is_set=$? -(( ! $wd_extglob_is_set )) && setopt noextendedglob - -# load warp points -typeset -A points -while read -r line -do - arr=(${(s,:,)line}) - key=${arr[1]} - # join the rest, in case the path contains colons - val=${(j,:,)arr[2,-1]} - - points[$key]=$val -done < "$WD_CONFIG" - -# get opts -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 ]] -then - wd_print_usage - -# 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_exit_fail "\'$WD_CONFIG\' is not writeable." - -else - # parse rest of options - local wd_o - for wd_o +# Main wd function to handle different commands +wd() { + local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} + local WD_QUIET=0 + local WD_EXIT_CODE=0 + local WD_DEBUG=0 + + # 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 \ + f=wd_force_mode -force=wd_force_mode + + 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" + else + wd_export_static_named_directories + fi + + # disable extendedglob for the complete wd execution time + setopt | grep -q extendedglob + wd_extglob_is_set=$? + (( ! $wd_extglob_is_set )) && setopt noextendedglob + + # load warp points + typeset -A points + while read -r line do - case "$wd_o" - in - "-a"|"--add"|"add") - wd_add "$2" "$wd_force_mode" - break - ;; - "-e"|"export") - wd_export_static_named_directories - break - ;; - "-r"|"--remove"|"rm") - # Passes all the arguments as a single string separated by whitespace to wd_remove - wd_remove "${@:2}" - break - ;; - "-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 - ;; - "-s"|"--show"|"show") - wd_show "$2" - break - ;; - "-c"|"--clean"|"clean") - wd_clean "$wd_force_mode" - break - ;; - *) - wd_warp "$wd_o" "$2" - 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 - -(( ! $wd_extglob_is_set )) && setopt extendedglob - -unset wd_extglob_is_set -unset wd_warp -unset wd_add -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 wd_export_static_named_directories -unset wd_o - -unset args -unset points -unset val &> /dev/null # fixes issue #1 - -if [[ -n $wd_debug_mode ]] -then - exit $WD_EXIT_CODE -else - unset wd_debug_mode -fi + arr=(${(s,:,)line}) + key=${arr[1]} + # join the rest, in case the path contains colons + val=${(j,:,)arr[2,-1]} + + points[$key]=$val + done < "$WD_CONFIG" + + # get opts + 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 ]] + then + wd_print_usage + + # 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_exit_fail "\'$WD_CONFIG\' is not writeable." + + else + # parse rest of options + local wd_o + for wd_o + do + case "$wd_o" + in + "-a"|"--add"|"add") + wd_add "$2" "$wd_force_mode" + break + ;; + "-b"|"browse") + wd_browse + break + ;; + "-e"|"export") + wd_export_static_named_directories + break + ;; + "-r"|"--remove"|"rm") + # Passes all the arguments as a single string separated by whitespace to wd_remove + wd_remove "${@:2}" + break + ;; + "-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 + ;; + "-s"|"--show"|"show") + wd_show "$2" + break + ;; + "-c"|"--clean"|"clean") + wd_clean "$wd_force_mode" + break + ;; + *) + wd_warp "$wd_o" "$2" + 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 + + (( ! $wd_extglob_is_set )) && setopt extendedglob + + unset wd_extglob_is_set + unset wd_warp + unset wd_add + 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 wd_export_static_named_directories + unset wd_o + + unset args + unset points + unset val &> /dev/null # fixes issue #1 + + if [[ -n $wd_debug_mode ]] + then + exit $WD_EXIT_CODE + else + unset wd_debug_mode + fi +} \ No newline at end of file From f66545df5e5ba843922351f59dc60eaac418368f Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 03:24:41 -0400 Subject: [PATCH 02/31] Update wd.sh --- wd.sh | 314 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 157 insertions(+), 157 deletions(-) diff --git a/wd.sh b/wd.sh index 8848975..b6fb093 100755 --- a/wd.sh +++ b/wd.sh @@ -371,161 +371,161 @@ wd_export_static_named_directories() { # Main wd function to handle different commands wd() { - local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} - local WD_QUIET=0 - local WD_EXIT_CODE=0 - local WD_DEBUG=0 - - # 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 \ - f=wd_force_mode -force=wd_force_mode - - 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" - else - wd_export_static_named_directories - fi - - # disable extendedglob for the complete wd execution time - setopt | grep -q extendedglob - wd_extglob_is_set=$? - (( ! $wd_extglob_is_set )) && setopt noextendedglob - - # load warp points - typeset -A points - while read -r line +local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} +local WD_QUIET=0 +local WD_EXIT_CODE=0 +local WD_DEBUG=0 + +# 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 \ + f=wd_force_mode -force=wd_force_mode + +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" +else + wd_export_static_named_directories +fi + +# disable extendedglob for the complete wd execution time +setopt | grep -q extendedglob +wd_extglob_is_set=$? +(( ! $wd_extglob_is_set )) && setopt noextendedglob + +# load warp points +typeset -A points +while read -r line +do + arr=(${(s,:,)line}) + key=${arr[1]} + # join the rest, in case the path contains colons + val=${(j,:,)arr[2,-1]} + + points[$key]=$val +done < "$WD_CONFIG" + +# get opts +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 ]] +then + wd_print_usage + +# 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_exit_fail "\'$WD_CONFIG\' is not writeable." + +else + # parse rest of options + local wd_o + for wd_o do - arr=(${(s,:,)line}) - key=${arr[1]} - # join the rest, in case the path contains colons - val=${(j,:,)arr[2,-1]} - - points[$key]=$val - done < "$WD_CONFIG" - - # get opts - 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 ]] - then - wd_print_usage - - # 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_exit_fail "\'$WD_CONFIG\' is not writeable." - - else - # parse rest of options - local wd_o - for wd_o - do - case "$wd_o" - in - "-a"|"--add"|"add") - wd_add "$2" "$wd_force_mode" - break - ;; - "-b"|"browse") - wd_browse - break - ;; - "-e"|"export") - wd_export_static_named_directories - break - ;; - "-r"|"--remove"|"rm") - # Passes all the arguments as a single string separated by whitespace to wd_remove - wd_remove "${@:2}" - break - ;; - "-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 - ;; - "-s"|"--show"|"show") - wd_show "$2" - break - ;; - "-c"|"--clean"|"clean") - wd_clean "$wd_force_mode" - break - ;; - *) - wd_warp "$wd_o" "$2" - 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 - - (( ! $wd_extglob_is_set )) && setopt extendedglob - - unset wd_extglob_is_set - unset wd_warp - unset wd_add - 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 wd_export_static_named_directories - unset wd_o - - unset args - unset points - unset val &> /dev/null # fixes issue #1 - - if [[ -n $wd_debug_mode ]] - then - exit $WD_EXIT_CODE - else - unset wd_debug_mode - fi -} \ No newline at end of file + case "$wd_o" + in + "-a"|"--add"|"add") + wd_add "$2" "$wd_force_mode" + break + ;; + "-b"|"browse") + wd_browse + break + ;; + "-e"|"export") + wd_export_static_named_directories + break + ;; + "-r"|"--remove"|"rm") + # Passes all the arguments as a single string separated by whitespace to wd_remove + wd_remove "${@:2}" + break + ;; + "-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 + ;; + "-s"|"--show"|"show") + wd_show "$2" + break + ;; + "-c"|"--clean"|"clean") + wd_clean "$wd_force_mode" + break + ;; + *) + wd_warp "$wd_o" "$2" + 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 + +(( ! $wd_extglob_is_set )) && setopt extendedglob + +unset wd_extglob_is_set +unset wd_warp +unset wd_add +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 wd_export_static_named_directories +unset wd_o + +unset args +unset points +unset val &> /dev/null # fixes issue #1 + +if [[ -n $wd_debug_mode ]] +then + exit $WD_EXIT_CODE +else + unset wd_debug_mode +fi +} From ea1958774a2d4360319cb53d35b32ccf30abaffd Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 03:25:19 -0400 Subject: [PATCH 03/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index b6fb093..4788b8f 100755 --- a/wd.sh +++ b/wd.sh @@ -240,7 +240,6 @@ wd_browse() { fi } - wd_list_all() { wd_print_msg "$WD_BLUE" "All warp points:" From 0a5df371e2c30c6dae6375a8f930990817d847ce Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 04:11:24 -0400 Subject: [PATCH 04/31] fixed print messages --- wd.sh | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/wd.sh b/wd.sh index 4788b8f..ef6c499 100755 --- a/wd.sh +++ b/wd.sh @@ -11,11 +11,11 @@ readonly WD_VERSION=0.5.2 # colors -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" +WD_BLUE="\033[96m" +WD_GREEN="\033[92m" +WD_YELLOW="\033[93m" +WD_RED="\033[91m" +WD_NOC="\033[m" ## functions @@ -53,22 +53,20 @@ wd_yesorno() return ${RETVAL} } -wd_print_msg() -{ - if [[ -z $wd_quiet_mode ]] - then - local color=$1 - local msg=$2 - if [[ $color == "" || $msg == "" ]] - then - print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!" - else - print " ${color}*${WD_NOC} ${msg}" - fi +wd_print_msg() { + local color="${1:-$WD_BLUE}" # Default to blue if no color is provided + local msg="$2" + + if [[ -z "$msg" ]]; then + print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" + else + print "${color}*${WD_NOC} ${msg}" fi } + + wd_print_usage() { command cat <<- EOF @@ -240,6 +238,7 @@ wd_browse() { fi } + wd_list_all() { wd_print_msg "$WD_BLUE" "All warp points:" From eea193f7fe92722ea7a8ad7944053f7f2dd31dc3 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 04:14:26 -0400 Subject: [PATCH 05/31] Update wd.sh --- wd.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/wd.sh b/wd.sh index ef6c499..cd57d3d 100755 --- a/wd.sh +++ b/wd.sh @@ -54,19 +54,21 @@ wd_yesorno() } -wd_print_msg() { - local color="${1:-$WD_BLUE}" # Default to blue if no color is provided - local msg="$2" - - if [[ -z "$msg" ]]; then - print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" - else - print "${color}*${WD_NOC} ${msg}" +wd_print_msg() +{ + if [[ -z $wd_quiet_mode ]] + then + local color="${1:-$WD_BLUE}" # Default to blue if no color is provided + local msg="$2" + + if [[ -z "$msg" ]]; then + print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" + else + print "${color}*${WD_NOC} ${msg}" + fi fi } - - wd_print_usage() { command cat <<- EOF @@ -238,7 +240,6 @@ wd_browse() { fi } - wd_list_all() { wd_print_msg "$WD_BLUE" "All warp points:" From 61a0e243464f4afe12015db9360b730e931cba73 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 5 May 2024 04:15:58 -0400 Subject: [PATCH 06/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index cd57d3d..44ce1ea 100755 --- a/wd.sh +++ b/wd.sh @@ -53,7 +53,6 @@ wd_yesorno() return ${RETVAL} } - wd_print_msg() { if [[ -z $wd_quiet_mode ]] From a8ec4744035d829f01c8fa4e1040fc31c56454f1 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 21:59:35 -0400 Subject: [PATCH 07/31] Update wd.sh --- wd.sh | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/wd.sh b/wd.sh index 44ce1ea..3fbb447 100755 --- a/wd.sh +++ b/wd.sh @@ -11,11 +11,11 @@ readonly WD_VERSION=0.5.2 # colors -WD_BLUE="\033[96m" -WD_GREEN="\033[92m" -WD_YELLOW="\033[93m" -WD_RED="\033[91m" -WD_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 @@ -56,14 +56,15 @@ wd_yesorno() wd_print_msg() { if [[ -z $wd_quiet_mode ]] + then + local color=$1 + local msg=$2 + + if [[ $color == "" || $msg == "" ]] then - local color="${1:-$WD_BLUE}" # Default to blue if no color is provided - local msg="$2" - - if [[ -z "$msg" ]]; then - print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" + print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!" else - print "${color}*${WD_NOC} ${msg}" + print " ${color}*${WD_NOC} ${msg}" fi fi } @@ -230,6 +231,10 @@ wd_remove() } wd_browse() { + if ! command -v fzf >/dev/null; then + echo "This functionality requires fzf. Please install fzf first." + return 1 + fi local entries=("${(@f)$(sed "s:${HOME}:~:g" "$WD_CONFIG" | awk -F ':' '{print $1 " -> " $2}')}") local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf-tmux --height 40% --reverse) if [[ -n $selected_entry ]]; then @@ -367,8 +372,6 @@ wd_export_static_named_directories() { fi } -# Main wd function to handle different commands -wd() { local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} local WD_QUIET=0 local WD_EXIT_CODE=0 @@ -495,11 +498,15 @@ else done fi + ## garbage collection # if not, next time warp will pick up variables from this run # remember, there's no sub shell -(( ! $wd_extglob_is_set )) && setopt extendedglob +wd_extglob_is_set=0 +setopt | grep -q extendedglob && wd_extglob_is_set=1 +# Use a default value in your condition to ensure it doesn't fail +(( ! ${wd_extglob_is_set:-0} )) && setopt extendedglob unset wd_extglob_is_set unset wd_warp @@ -520,10 +527,10 @@ unset args unset points unset val &> /dev/null # fixes issue #1 + if [[ -n $wd_debug_mode ]] then exit $WD_EXIT_CODE else unset wd_debug_mode fi -} From c873d11e16247c8bd0baa79d57109d7f151ffa9a Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 21:59:57 -0400 Subject: [PATCH 08/31] Update wd.plugin.zsh --- wd.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wd.plugin.zsh b/wd.plugin.zsh index a194896..f1a3496 100644 --- a/wd.plugin.zsh +++ b/wd.plugin.zsh @@ -7,8 +7,8 @@ # # @github.com/mfaerevaag/wd -source ${0:A:h}/wd.sh - +eval "wd() { source '${0:A:h}/wd.sh' }" +wd > /dev/null # Register the function as a Zsh widget zle -N wd_browse # Bind the widget to a key combination From f2d284495ed401550cf92fd41a9ab05a72107f1b Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 22:01:08 -0400 Subject: [PATCH 09/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index 3fbb447..459cdc5 100755 --- a/wd.sh +++ b/wd.sh @@ -527,7 +527,6 @@ unset args unset points unset val &> /dev/null # fixes issue #1 - if [[ -n $wd_debug_mode ]] then exit $WD_EXIT_CODE From 8e795d445a06d5a50eb3129e0f8ced85e798fe06 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 22:01:37 -0400 Subject: [PATCH 10/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index 459cdc5..bb5c188 100755 --- a/wd.sh +++ b/wd.sh @@ -498,7 +498,6 @@ else done fi - ## garbage collection # if not, next time warp will pick up variables from this run # remember, there's no sub shell From 5add2c9b6b70fc84d079ae980e5c1eee3b0bbcae Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 22:03:15 -0400 Subject: [PATCH 11/31] Update wd.sh --- wd.sh | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/wd.sh b/wd.sh index bb5c188..e449161 100755 --- a/wd.sh +++ b/wd.sh @@ -57,14 +57,13 @@ wd_print_msg() { if [[ -z $wd_quiet_mode ]] then - local color=$1 - local msg=$2 - - if [[ $color == "" || $msg == "" ]] - then - print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!" + local color="${1:-$WD_BLUE}" # Default to blue if no color is provided + local msg="$2" + + if [[ -z "$msg" ]]; then + print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" else - print " ${color}*${WD_NOC} ${msg}" + print "${color}*${WD_NOC} ${msg}" fi fi } From dd930c9954c41d2e8d3e2ab5c7843f63d38ac00e Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 22:35:03 -0400 Subject: [PATCH 12/31] Update wd.sh --- wd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wd.sh b/wd.sh index e449161..b1b61c6 100755 --- a/wd.sh +++ b/wd.sh @@ -235,7 +235,7 @@ wd_browse() { return 1 fi local entries=("${(@f)$(sed "s:${HOME}:~:g" "$WD_CONFIG" | awk -F ':' '{print $1 " -> " $2}')}") - local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf-tmux --height 40% --reverse) + local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf --height 40% --reverse) if [[ -n $selected_entry ]]; then local selected_point="${selected_entry%% ->*}" selected_point=$(echo "$selected_point" | xargs) From b8d421b861cd6be6cc09577498a17538e0dcc9dc Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 23:00:08 -0400 Subject: [PATCH 13/31] Update wd.sh --- wd.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wd.sh b/wd.sh index b1b61c6..52305b1 100755 --- a/wd.sh +++ b/wd.sh @@ -68,6 +68,7 @@ wd_print_msg() fi } + wd_print_usage() { command cat <<- EOF @@ -239,7 +240,7 @@ wd_browse() { if [[ -n $selected_entry ]]; then local selected_point="${selected_entry%% ->*}" selected_point=$(echo "$selected_point" | xargs) - eval "wd $selected_point" + wd_warp $selected_point fi } From 8a906b27f44c7cb5c60375fd29284ce26f12605f Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 23:00:44 -0400 Subject: [PATCH 14/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index 52305b1..e335893 100755 --- a/wd.sh +++ b/wd.sh @@ -68,7 +68,6 @@ wd_print_msg() fi } - wd_print_usage() { command cat <<- EOF From eba4145a9e476b4096d1749f03ced8c74fb0c95b Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 23:04:12 -0400 Subject: [PATCH 15/31] Update wd.sh --- wd.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wd.sh b/wd.sh index e335893..c46e32b 100755 --- a/wd.sh +++ b/wd.sh @@ -68,6 +68,7 @@ wd_print_msg() fi } + wd_print_usage() { command cat <<- EOF @@ -239,7 +240,7 @@ wd_browse() { if [[ -n $selected_entry ]]; then local selected_point="${selected_entry%% ->*}" selected_point=$(echo "$selected_point" | xargs) - wd_warp $selected_point + wd $selected_point fi } From d230ca2629ed100f722cb43b37f53484f5246cf4 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 6 May 2024 23:04:41 -0400 Subject: [PATCH 16/31] Update wd.sh --- wd.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/wd.sh b/wd.sh index c46e32b..d761e7d 100755 --- a/wd.sh +++ b/wd.sh @@ -68,7 +68,6 @@ wd_print_msg() fi } - wd_print_usage() { command cat <<- EOF From 94d4028cdc72d868f29c03aa6c1825d27e2bb25c Mon Sep 17 00:00:00 2001 From: p1r473 Date: Tue, 7 May 2024 13:37:50 -0400 Subject: [PATCH 17/31] Update wd.sh --- wd.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/wd.sh b/wd.sh index d761e7d..f2bb492 100755 --- a/wd.sh +++ b/wd.sh @@ -63,7 +63,7 @@ wd_print_msg() if [[ -z "$msg" ]]; then print "${WD_RED}*${WD_NOC} Could not print message. Sorry!" else - print "${color}*${WD_NOC} ${msg}" + print " ${color}*${WD_NOC} ${msg}" fi fi } @@ -409,7 +409,9 @@ fi # disable extendedglob for the complete wd execution time setopt | grep -q extendedglob wd_extglob_is_set=$? -(( ! $wd_extglob_is_set )) && setopt noextendedglob +if (( wd_extglob_is_set == 0 )); then + setopt noextendedglob +fi # load warp points typeset -A points @@ -501,10 +503,9 @@ fi # if not, next time warp will pick up variables from this run # remember, there's no sub shell -wd_extglob_is_set=0 -setopt | grep -q extendedglob && wd_extglob_is_set=1 -# Use a default value in your condition to ensure it doesn't fail -(( ! ${wd_extglob_is_set:-0} )) && setopt extendedglob +if (( wd_extglob_is_set == 0 )); then + setopt extendedglob +fi unset wd_extglob_is_set unset wd_warp From cbb701f6773f3c3f991370a758aa0c146dc806e8 Mon Sep 17 00:00:00 2001 From: alpha-tango-kilo Date: Wed, 8 May 2024 19:59:55 +0100 Subject: [PATCH 18/31] Document wd_browse in README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 13c6b7a..0c71c05 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,14 @@ Also, you may have to force a rebuild of `zcompdump` by running: rm -f ~/.zcompdump; compinit ``` +## Browse + +If you want to make use of the `fzf`-powered browse feature to fuzzy search through all your warp points, set up a keybind in your `.zshrc`: + +```zsh +bindkey '^G' wd_browse +``` + ## Usage * Add warp point to current working directory: From 038a368f81ccc4d48e0db3986134d3cacd79d4c4 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:34:39 -0400 Subject: [PATCH 19/31] Update wd.plugin.zsh --- wd.plugin.zsh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wd.plugin.zsh b/wd.plugin.zsh index f1a3496..39c8fc2 100644 --- a/wd.plugin.zsh +++ b/wd.plugin.zsh @@ -9,7 +9,8 @@ eval "wd() { source '${0:A:h}/wd.sh' }" wd > /dev/null -# Register the function as a Zsh widget -zle -N wd_browse -# Bind the widget to a key combination -bindkey '^G' wd_browse +zle -N wd_browse_widget +zle -N wd_restore_buffer +autoload -Uz add-zle-hook-widget +add-zle-hook-widget line-init wd_restore_buffer +bindkey ${FZF_WD_BINDKEY:-'^B'} fuzzy_wd_widget From 28f3be9c06762b644b023efe545a5a25f684a5ba Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:36:41 -0400 Subject: [PATCH 20/31] Update _wd.sh --- _wd.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_wd.sh b/_wd.sh index 8d5cf15..323d801 100644 --- a/_wd.sh +++ b/_wd.sh @@ -31,6 +31,7 @@ function _wd() { commands=( 'add:Adds the current working directory to your warp points' + 'addcd:Adds a directory to your warp points' 'add!:Overwrites existing warp point' 'export:Export warp points as static named directories' 'rm:Removes the given warp point' @@ -63,6 +64,9 @@ function _wd() { add) _message 'Write the name of your warp point' && ret=0 ;; + add) + _message 'Write the name of your path' && ret=0 + ;; show) _describe -t points "Warp points" warp_points && ret=0 ;; From 411b4a9598a39f55c5580226c760115d6ad652bf Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:37:01 -0400 Subject: [PATCH 21/31] Update wd.1 --- wd.1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wd.1 b/wd.1 index 2d697f2..b3f9a4d 100644 --- a/wd.1 +++ b/wd.1 @@ -20,6 +20,12 @@ Add a new warp point which will warp to the current working directory with curre .IP "add, -a, --add " Add a new warp point which will warp to the current working directory with \fIname\fR as identifier. . +.IP "addcd, -c, --addcd " +Add a new warp point which will warp to the current working directory with current directory name as identifier. +. +.IP "addcd, -c, --addcd " +Add a new warp point which will warp to the current working directory with \fIname\fR as identifier. +. .IP "rm, -r, --remove" Remove any warp point with current directory name as identifier. . @@ -72,7 +78,7 @@ https://github.com/mfaerevaag/wd/issues . . .SH AUTHORS -Created and maintained by Markus Færevaag and Simon Altschuler, with the help of various contributors: +Created and maintained by Markus Færevaag and Simon Altschuler, with the help of various contributors: .nf https://github.com/mfaerevaag/wd/graphs/contributors. . From e67ac930f2b999ebc3b939041d99fd00ded243c1 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:37:36 -0400 Subject: [PATCH 22/31] Update wd.sh --- wd.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 13 deletions(-) diff --git a/wd.sh b/wd.sh index f2bb492..39d9375 100755 --- a/wd.sh +++ b/wd.sh @@ -74,18 +74,20 @@ wd_print_usage() Usage: wd [command] [point] Commands: - Warps to the directory specified by the warp point - Warps to the directory specified by the warp point with path appended - add Adds the current working directory to your warp points - add Adds the current working directory to your warp points with current directory's name - rm Removes the given warp point - rm Removes the given warp point with current directory's name - show Print path to given warp point - show Print warp points to current directory - list Print all stored warp points - ls Show files from given warp point (ls) - path Show the path to given warp point (pwd) - clean Remove points warping to nonexistent directories (will prompt unless --force is used) + Warps to the directory specified by the warp point + Warps to the directory specified by the warp point with path appended + add Adds the current working directory to your warp points + add Adds the current working directory to your warp points with current directory's name + addcd Adds a path to your warp points with the directory's name + addcd Adds a path to your warp points with a custom name + rm Removes the given warp point + rm Removes the given warp point with current directory's name + show Print path to given warp point + show Print warp points to current directory + list Print all stored warp points + ls Show files from given warp point (ls) + path Show the path to given warp point (pwd) + clean Remove points warping to nonexistent directories (will prompt unless --force is used) -v | --version Print version -d | --debug Exit after execution with exit codes (for testing) @@ -203,6 +205,67 @@ wd_add() fi } +wd_addcd() +{ + local folder=$1 + local point=$2 + local force=$3 + cmdnames=(add rm show list ls path clean help) + + # Check if the path is actually provided, if not, exit with an error + if [[ -z "$folder" ]] + then + wd_exit_fail "You must specify a path" + return + fi + + # Check if the directory exists + if [[ ! -d "$folder" ]] + then + wd_exit_fail "The directory does not exist" + return + fi + + if [[ $point == "" ]] + then + point=$(basename $folder) + fi + + if [[ $point =~ "^[\.]+$" ]] + then + wd_exit_fail "Warp point cannot be just dots" + elif [[ $point =~ "[[:space:]]+" ]] + then + wd_exit_fail "Warp point should not contain whitespace" + elif [[ $point =~ : ]] || [[ $point =~ / ]] + then + wd_exit_fail "Warp point contains illegal character (:/)" + elif (($cmdnames[(Ie)$point])) + then + wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)" + elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ] + then + wd_remove "$point" > /dev/null + printf "%q:%s\n" "${point}" "${folder/#$HOME/~}" >> "$WD_CONFIG" + if (whence sort >/dev/null); then + local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") + # use 'cat' below to ensure we respect $WD_CONFIG as a symlink + command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" + fi + + wd_export_static_named_directories + + wd_print_msg "$WD_GREEN" "Warp point '$point' added for path '$folder'" + + # 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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." + fi +} + + wd_remove() { local point_list=$1 @@ -243,6 +306,24 @@ wd_browse() { fi } +wd_browse_widget() { + if [[ -e $WD_CONFIG ]]; then + wd_browse + saved_buffer=$BUFFER + saved_cursor=$CURSOR + BUFFER= + zle redisplay + zle accept-line + fi +} + +wd_restore_buffer() { + BUFFER=$saved_buffer + CURSOR=$saved_cursor + saved_buffer= + saved_cursor=1 +} + wd_list_all() { wd_print_msg "$WD_BLUE" "All warp points:" @@ -371,7 +452,7 @@ wd_export_static_named_directories() { fi } -local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} +WD_CONFIG=${WD_CONFIG:-$HOME/.warprc} local WD_QUIET=0 local WD_EXIT_CODE=0 local WD_DEBUG=0 @@ -455,6 +536,10 @@ else wd_browse break ;; + "-c"|"--addcd"|"addcd") + wd_addcd "$2" "$3" "$wd_force_mode" + break + ;; "-e"|"export") wd_export_static_named_directories break @@ -510,6 +595,7 @@ fi unset wd_extglob_is_set unset wd_warp unset wd_add +unset wd_addcd unset wd_remove unset wd_show unset wd_list_all From cab3dadba635a5e3a917cd2df22341c2d3218701 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:43:22 -0400 Subject: [PATCH 23/31] Update README.md --- README.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c71c05..1859e1a 100644 --- a/README.md +++ b/README.md @@ -139,10 +139,10 @@ rm -f ~/.zcompdump; compinit ## Browse -If you want to make use of the `fzf`-powered browse feature to fuzzy search through all your warp points, set up a keybind in your `.zshrc`: +If you want to make use of the `fzf`-powered browse feature to fuzzy search through all your warp points, use the provided keybind, or change it: ```zsh -bindkey '^G' wd_browse +bindkey ${FZF_WD_BINDKEY:-'^B'} fuzzy_wd_widget ``` ## Usage @@ -158,6 +158,19 @@ If a warp point with the same name exists, use `wd add foo --force` to overwrite **Note:** a warp point cannot contain colons, or consist of only spaces and dots. The first will conflict in how `wd` stores the warp points, and the second will conflict with other features, as below. +* Add warp point to any directory with default name: + +```zsh +wd addcd /foo/ bar +``` + +* Add warp point to any directory with a custom name: + +```zsh +wd addcd /foo/ +``` + + You can omit point name to automatically use the current directory's name instead. * From any directory, warp to `foo` with: From ca4960e43fa56c541ae4d5b575132882bade4e83 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:43:37 -0400 Subject: [PATCH 24/31] Update wd.1 --- wd.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wd.1 b/wd.1 index b3f9a4d..1986615 100644 --- a/wd.1 +++ b/wd.1 @@ -78,7 +78,7 @@ https://github.com/mfaerevaag/wd/issues . . .SH AUTHORS -Created and maintained by Markus Færevaag and Simon Altschuler, with the help of various contributors: +Created and maintained by Markus Færevaag and Simon Altschuler, with the help of various contributors: .nf https://github.com/mfaerevaag/wd/graphs/contributors. . From e81b9df774456fba51ba2d61be66c2241bd832e4 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 00:44:59 -0400 Subject: [PATCH 25/31] Update _wd.sh --- _wd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_wd.sh b/_wd.sh index 323d801..d52a692 100644 --- a/_wd.sh +++ b/_wd.sh @@ -64,7 +64,7 @@ function _wd() { add) _message 'Write the name of your warp point' && ret=0 ;; - add) + addcd) _message 'Write the name of your path' && ret=0 ;; show) From f1cc6dd396d0e465f17fa3b90877295b8c6ea512 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 01:22:20 -0400 Subject: [PATCH 26/31] Update wd.plugin.zsh --- wd.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wd.plugin.zsh b/wd.plugin.zsh index 3299376..9910cb9 100644 --- a/wd.plugin.zsh +++ b/wd.plugin.zsh @@ -18,4 +18,4 @@ zle -N wd_browse_widget zle -N wd_restore_buffer autoload -Uz add-zle-hook-widget add-zle-hook-widget line-init wd_restore_buffer -bindkey ${FZF_WD_BINDKEY:-'^B'} fuzzy_wd_widget +bindkey ${FZF_WD_BINDKEY:-'^B'} wd_browse_widget From f4902d65b493784f143594d098603e8b6b05af7c Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 14:20:51 -0400 Subject: [PATCH 27/31] Update wd.sh --- wd.sh | 62 +++++++++++++++-------------------------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/wd.sh b/wd.sh index 7c76a91..3437869 100755 --- a/wd.sh +++ b/wd.sh @@ -205,67 +205,35 @@ wd_add() fi } -wd_addcd() -{ +wd_addcd() { local folder=$1 local point=$2 local force=$3 - cmdnames=(add rm show list ls path clean help) + local currentdir=$PWD + + if [[ "$2" == "-f" ]] || [[ "$2" == "--force" ]]; then + point="" + force="$2" + elif [[ "$3" == "-f" ]] || [[ "$3" == "--force" ]]; then + point="$2" + force="$3" + fi - # Check if the path is actually provided, if not, exit with an error - if [[ -z "$folder" ]] - then + if [[ -z "$folder" ]]; then wd_exit_fail "You must specify a path" return fi - # Check if the directory exists - if [[ ! -d "$folder" ]] - then + if [[ ! -d "$folder" ]]; then wd_exit_fail "The directory does not exist" return fi - - if [[ $point == "" ]] - then - point=$(basename $folder) - fi - if [[ $point =~ "^[\.]+$" ]] - then - wd_exit_fail "Warp point cannot be just dots" - elif [[ $point =~ "[[:space:]]+" ]] - then - wd_exit_fail "Warp point should not contain whitespace" - elif [[ $point =~ : ]] || [[ $point =~ / ]] - then - wd_exit_fail "Warp point contains illegal character (:/)" - elif (($cmdnames[(Ie)$point])) - then - wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)" - elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ] - then - wd_remove "$point" > /dev/null - printf "%q:%s\n" "${point}" "${folder/#$HOME/~}" >> "$WD_CONFIG" - if (whence sort >/dev/null); then - local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") - # use 'cat' below to ensure we respect $WD_CONFIG as a symlink - command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" - fi - - wd_export_static_named_directories - - wd_print_msg "$WD_GREEN" "Warp point '$point' added for path '$folder'" - - # 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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." - fi + cd "$folder" || return + wd_add "$point" "$force" + cd "$currentdir" || return } - wd_remove() { local point_list=$1 From 7c52a900cc6fb7fbbb4d45e766c94f2b9571874c Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 14:21:27 -0400 Subject: [PATCH 28/31] Update tests.sh --- test/tests.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/tests.sh b/test/tests.sh index 9b02497..fe77dc8 100755 --- a/test/tests.sh +++ b/test/tests.sh @@ -250,6 +250,44 @@ test_valid_identifiers() "$pipestatus" } +test_wd_addcd_path_only() +{ + create_test_wp + wd -q addcd "$WD_TEST_DIR" + assertTrue "should successfully add default wp for directory" \ + "$(wp_exists "$(basename "$WD_TEST_DIR")")" + destroy_test_wp +} + +test_wd_addcd_path_and_point() +{ + create_test_wp + wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" + assertTrue "should successfully add specified wp for directory" \ + "$(wp_exists "$WD_TEST_WP_2")" + destroy_test_wp +} + +test_wd_addcd_path_forced() +{ + create_test_wp + wd -q addcd "$WD_TEST_DIR" + wd -q addcd "$WD_TEST_DIR" -f + assertTrue "should successfully force-add wp for directory" \ + "$(wp_exists "$(basename "$WD_TEST_DIR")")" + destroy_test_wp +} + +test_wd_addcd_path_and_point_forced() +{ + create_test_wp + wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" + wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" -f + assertTrue "should successfully force-add specified wp for directory" \ + "$(wp_exists "$WD_TEST_WP_2")" + destroy_test_wp +} + test_removal() { wd -q add foo From 9aba35a9407c4bb80349d1050ad4645fe5425a36 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 14:25:23 -0400 Subject: [PATCH 29/31] Update wd.sh --- wd.sh | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/wd.sh b/wd.sh index 3437869..bec8a4f 100755 --- a/wd.sh +++ b/wd.sh @@ -8,7 +8,7 @@ # @github.com/mfaerevaag/wd # version -readonly WD_VERSION=0.6.1 +readonly WD_VERSION=0.5.2 # colors readonly WD_BLUE="\033[96m" @@ -184,25 +184,22 @@ wd_add() then wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)" elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ] - then - wd_remove "$point" > /dev/null - printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG" - if (whence sort >/dev/null); then - local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") - # use 'cat' below to ensure we respect $WD_CONFIG as a symlink - command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" - fi - - wd_export_static_named_directories - - wd_print_msg "$WD_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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." - fi + then + wd_remove "$point" > /dev/null + printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG" + if (whence sort >/dev/null); then + local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") + # use 'cat' below to ensure we respect $WD_CONFIG as a symlink + command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" + fi + wd_export_static_named_directories + wd_print_msg "$WD_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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." + fi } wd_addcd() { @@ -211,14 +208,6 @@ wd_addcd() { local force=$3 local currentdir=$PWD - if [[ "$2" == "-f" ]] || [[ "$2" == "--force" ]]; then - point="" - force="$2" - elif [[ "$3" == "-f" ]] || [[ "$3" == "--force" ]]; then - point="$2" - force="$3" - fi - if [[ -z "$folder" ]]; then wd_exit_fail "You must specify a path" return @@ -234,6 +223,7 @@ wd_addcd() { cd "$currentdir" || return } + wd_remove() { local point_list=$1 From 1b24a83179f9ef2500d768e5983d5c71e6424ebf Mon Sep 17 00:00:00 2001 From: p1r473 Date: Sun, 12 May 2024 14:28:32 -0400 Subject: [PATCH 30/31] Update wd.sh --- wd.sh | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/wd.sh b/wd.sh index bec8a4f..3653585 100755 --- a/wd.sh +++ b/wd.sh @@ -184,22 +184,25 @@ wd_add() then wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)" elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ] - then - wd_remove "$point" > /dev/null - printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG" - if (whence sort >/dev/null); then - local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") - # use 'cat' below to ensure we respect $WD_CONFIG as a symlink - command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" - fi - wd_export_static_named_directories - wd_print_msg "$WD_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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." - fi + then + wd_remove "$point" > /dev/null + printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG" + if (whence sort >/dev/null); then + local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX") + # use 'cat' below to ensure we respect $WD_CONFIG as a symlink + command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}" + fi + + wd_export_static_named_directories + + wd_print_msg "$WD_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_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite." + fi } wd_addcd() { From 0fafd43c28d4175e8cae7ee4d78b5be137048ca8 Mon Sep 17 00:00:00 2001 From: p1r473 Date: Mon, 13 May 2024 17:40:09 -0400 Subject: [PATCH 31/31] Update tests.sh --- test/tests.sh | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/test/tests.sh b/test/tests.sh index fe77dc8..c269875 100755 --- a/test/tests.sh +++ b/test/tests.sh @@ -250,44 +250,57 @@ test_valid_identifiers() "$pipestatus" } -test_wd_addcd_path_only() +test_wd_addcd() { + # Create a base directory for testing create_test_wp + + # Test with basic directory wd -q addcd "$WD_TEST_DIR" assertTrue "should successfully add default wp for directory" \ "$(wp_exists "$(basename "$WD_TEST_DIR")")" - destroy_test_wp -} -test_wd_addcd_path_and_point() -{ - create_test_wp + # Test with a specific warp point name wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" assertTrue "should successfully add specified wp for directory" \ "$(wp_exists "$WD_TEST_WP_2")" - destroy_test_wp -} -test_wd_addcd_path_forced() -{ - create_test_wp - wd -q addcd "$WD_TEST_DIR" + # Test with force option wd -q addcd "$WD_TEST_DIR" -f assertTrue "should successfully force-add wp for directory" \ "$(wp_exists "$(basename "$WD_TEST_DIR")")" - destroy_test_wp -} -test_wd_addcd_path_and_point_forced() -{ - create_test_wp - wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" + # Test with a specific warp point name and force option wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" -f assertTrue "should successfully force-add specified wp for directory" \ "$(wp_exists "$WD_TEST_WP_2")" + + # Test with absolute path + local abs_path="$PWD/$WD_TEST_DIR" + wd -q addcd "$abs_path" + assertTrue "should successfully add default wp for absolute directory path" \ + "$(wp_exists "$(basename "$abs_path")")" + + # Test with relative path including navigation + mkdir -p "$WD_TEST_DIR/nested/extra" + local rel_path="../$(basename "$PWD")/$WD_TEST_DIR" + cd "$WD_TEST_DIR/nested/extra" + wd -q addcd "$rel_path" + assertTrue "should successfully add wp for relative path with navigation" \ + "$(wp_exists "$(basename "$rel_path")")" + cd - > /dev/null + + # Test with nested directory paths + local nested_path="$WD_TEST_DIR/nested/extra" + wd -q addcd "$nested_path" + assertTrue "should successfully add wp for nested directory path" \ + "$(wp_exists "$(basename "$nested_path")")" + + # Cleanup destroy_test_wp } + test_removal() { wd -q add foo