2024-09-15 06:16:50 +00:00
|
|
|
#!/usr/bin/env zsh
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-07-15 15:42:30 +00:00
|
|
|
# WARP DIRECTORY
|
|
|
|
# ==============
|
2013-11-16 01:15:17 +00:00
|
|
|
# Jump to custom directories in terminal
|
|
|
|
# because `cd` takes too long...
|
|
|
|
#
|
|
|
|
# @github.com/mfaerevaag/wd
|
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# version
|
2024-11-24 12:25:13 +00:00
|
|
|
readonly WD_VERSION=0.9.2
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-07-15 15:42:30 +00:00
|
|
|
# colors
|
2014-09-07 19:56:34 +00:00
|
|
|
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"
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
## functions
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# 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
|
2020-06-03 16:35:51 +00:00
|
|
|
"Y"|"y"|"YES"|"yes"|"Yes" )
|
2014-09-07 19:56:34 +00:00
|
|
|
RETVAL=${yes_RETVAL} && \
|
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"N"|"n"|"NO"|"no"|"No" )
|
2014-09-07 19:56:34 +00:00
|
|
|
RETVAL=${no_RETVAL} && \
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
echo "Please provide a valid answer (y or n)"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
return ${RETVAL}
|
|
|
|
}
|
2013-11-25 23:45:24 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_print_msg()
|
|
|
|
{
|
|
|
|
if [[ -z $wd_quiet_mode ]]
|
|
|
|
then
|
2024-05-12 10:42:59 +00:00
|
|
|
local color="${1:-$WD_BLUE}" # Default to blue if no color is provided
|
|
|
|
local msg="$2"
|
2024-10-13 19:10:29 +00:00
|
|
|
|
2024-05-12 10:42:59 +00:00
|
|
|
if [[ -z "$msg" ]]; then
|
|
|
|
print "${WD_RED}*${WD_NOC} Could not print message. Sorry!"
|
2014-09-07 19:56:34 +00:00
|
|
|
else
|
|
|
|
print " ${color}*${WD_NOC} ${msg}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_print_usage()
|
|
|
|
{
|
2020-10-09 15:38:02 +00:00
|
|
|
command cat <<- EOF
|
2017-11-01 13:03:34 +00:00
|
|
|
Usage: wd [command] [point]
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
Commands:
|
2024-05-21 18:48:54 +00:00
|
|
|
<point> Warps to the directory specified by the warp point
|
|
|
|
<point> <path> Warps to the directory specified by the warp point with path appended
|
|
|
|
add <point> 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 <path> Adds a path to your warp points with the directory's name
|
|
|
|
addcd <path> <point> Adds a path to your warp points with a custom name
|
|
|
|
rm <point> Removes the given warp point
|
|
|
|
rm Removes the given warp point with current directory's name
|
|
|
|
show <point> Print path to given warp point
|
|
|
|
show Print warp points to current directory
|
|
|
|
list Print all stored warp points
|
|
|
|
ls <point> Show files from given warp point (ls)
|
|
|
|
path <point> Show the path to given warp point (pwd)
|
|
|
|
clean Remove points warping to nonexistent directories (will prompt unless --force is used)
|
2017-11-01 13:03:34 +00:00
|
|
|
|
|
|
|
-v | --version Print version
|
|
|
|
-c | --config Specify config file (default ~/.warprc)
|
|
|
|
-q | --quiet Suppress all output
|
2020-09-09 12:24:43 +00:00
|
|
|
-f | --force Allows overwriting without warning (for add & clean)
|
2017-11-01 13:03:34 +00:00
|
|
|
|
|
|
|
help Show this extremely helpful text
|
2014-09-07 19:56:34 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
wd_exit_fail()
|
|
|
|
{
|
|
|
|
local msg=$1
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_RED" "$msg"
|
2014-09-07 19:56:34 +00:00
|
|
|
WD_EXIT_CODE=1
|
|
|
|
}
|
|
|
|
|
|
|
|
wd_exit_warn()
|
|
|
|
{
|
|
|
|
local msg=$1
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_YELLOW" "$msg"
|
2014-09-07 19:56:34 +00:00
|
|
|
WD_EXIT_CODE=1
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:13:39 +00:00
|
|
|
wd_getdir()
|
|
|
|
{
|
|
|
|
local name_arg=$1
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
point=$(wd_show "$name_arg")
|
2015-01-25 23:13:39 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# core
|
2013-11-16 01:15:17 +00:00
|
|
|
|
|
|
|
wd_warp()
|
|
|
|
{
|
2014-07-15 15:42:30 +00:00
|
|
|
local point=$1
|
2017-11-01 13:03:34 +00:00
|
|
|
local sub=$2
|
2014-07-15 15:42:30 +00:00
|
|
|
|
|
|
|
if [[ $point =~ "^\.+$" ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2017-11-01 13:03:34 +00:00
|
|
|
if [[ $#1 < 2 ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_exit_warn "Warping to current directory?"
|
2013-11-16 01:15:17 +00:00
|
|
|
else
|
|
|
|
(( n = $#1 - 1 ))
|
|
|
|
cd -$n > /dev/null
|
2024-11-24 12:25:13 +00:00
|
|
|
WD_EXIT_CODE=$?
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
2014-07-15 15:42:30 +00:00
|
|
|
elif [[ ${points[$point]} != "" ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2017-11-01 13:03:34 +00:00
|
|
|
if [[ $sub != "" ]]
|
|
|
|
then
|
|
|
|
cd ${points[$point]/#\~/$HOME}/$sub
|
2024-11-24 12:25:13 +00:00
|
|
|
WD_EXIT_CODE=$?
|
2017-11-01 13:03:34 +00:00
|
|
|
else
|
|
|
|
cd ${points[$point]/#\~/$HOME}
|
2024-11-24 12:25:13 +00:00
|
|
|
WD_EXIT_CODE=$?
|
2017-11-01 13:03:34 +00:00
|
|
|
fi
|
2013-11-16 01:15:17 +00:00
|
|
|
else
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_exit_fail "Unknown warp point '${point}'"
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
wd_add()
|
|
|
|
{
|
2020-09-09 12:24:43 +00:00
|
|
|
local point=$1
|
|
|
|
local force=$2
|
2022-11-06 19:52:53 +00:00
|
|
|
cmdnames=(add rm show list ls path clean help)
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2017-11-01 13:03:34 +00:00
|
|
|
if [[ $point == "" ]]
|
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
point=$(basename "$PWD")
|
2017-11-01 13:03:34 +00:00
|
|
|
fi
|
|
|
|
|
2014-07-15 15:42:30 +00:00
|
|
|
if [[ $point =~ "^[\.]+$" ]]
|
|
|
|
then
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_exit_fail "Warp point cannot be just dots"
|
2014-10-21 07:51:13 +00:00
|
|
|
elif [[ $point =~ "[[:space:]]+" ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_exit_fail "Warp point should not contain whitespace"
|
2020-10-09 15:38:02 +00:00
|
|
|
elif [[ $point =~ : ]] || [[ $point =~ / ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2020-10-09 15:38:02 +00:00
|
|
|
wd_exit_fail "Warp point contains illegal character (:/)"
|
2022-11-06 19:52:53 +00:00
|
|
|
elif (($cmdnames[(Ie)$point]))
|
|
|
|
then
|
|
|
|
wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)"
|
2020-09-09 12:24:43 +00:00
|
|
|
elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ]
|
2014-07-15 15:42:30 +00:00
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_remove "$point" > /dev/null
|
2024-11-24 12:25:13 +00:00
|
|
|
printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$wd_config_file"
|
2020-08-05 12:19:26 +00:00
|
|
|
if (whence sort >/dev/null); then
|
|
|
|
local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
|
2024-11-24 12:25:13 +00:00
|
|
|
# use 'cat' below to ensure we respect $wd_config_file as a symlink
|
|
|
|
command sort -o "${config_tmp}" "$wd_config_file" && command cat "${config_tmp}" >| "$wd_config_file" && command rm "${config_tmp}"
|
2020-08-05 12:19:26 +00:00
|
|
|
fi
|
2014-09-07 19:56:34 +00:00
|
|
|
|
2020-06-03 16:35:51 +00:00
|
|
|
wd_export_static_named_directories
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_GREEN" "Warp point added"
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# 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
|
2013-11-16 01:15:17 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite."
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-21 18:48:54 +00:00
|
|
|
wd_addcd() {
|
|
|
|
local folder="$1"
|
|
|
|
local point=$2
|
|
|
|
local force=$3
|
|
|
|
local currentdir=$PWD
|
|
|
|
|
|
|
|
if [[ -z "$folder" ]]; then
|
|
|
|
wd_exit_fail "You must specify a path"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -d "$folder" ]]; then
|
|
|
|
wd_exit_fail "The directory does not exist"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd "$folder" || return
|
|
|
|
wd_add "$point" "$force"
|
|
|
|
cd "$currentdir" || return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-16 01:15:17 +00:00
|
|
|
wd_remove()
|
|
|
|
{
|
2020-09-09 12:24:43 +00:00
|
|
|
local point_list=$1
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
if [[ "$point_list" == "" ]]
|
2017-11-01 13:03:34 +00:00
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
point_list=$(basename "$PWD")
|
2017-11-01 13:03:34 +00:00
|
|
|
fi
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
for point_name in $point_list ; do
|
|
|
|
if [[ ${points[$point_name]} != "" ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
|
|
|
|
# Copy and delete in two steps in order to preserve symlinks
|
2024-11-24 12:25:13 +00:00
|
|
|
if sed -n "/^${point_name}:.*$/!p" "$wd_config_file" >| "$config_tmp" && command cp "$config_tmp" "$wd_config_file" && command rm "$config_tmp"
|
2020-09-09 12:24:43 +00:00
|
|
|
then
|
|
|
|
wd_print_msg "$WD_GREEN" "Warp point removed"
|
|
|
|
else
|
|
|
|
wd_exit_fail "Something bad happened! Sorry."
|
|
|
|
fi
|
2013-11-16 01:15:17 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_exit_fail "Warp point was not found"
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
2020-09-09 12:24:43 +00:00
|
|
|
done
|
2013-11-16 01:15:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 10:42:59 +00:00
|
|
|
wd_browse() {
|
|
|
|
if ! command -v fzf >/dev/null; then
|
|
|
|
echo "This functionality requires fzf. Please install fzf first."
|
|
|
|
return 1
|
|
|
|
fi
|
2024-11-24 12:25:13 +00:00
|
|
|
local entries=("${(@f)$(sed "s:${HOME}:~:g" "$wd_config_file" | awk -F ':' '{print $1 " -> " $2}')}")
|
2024-05-21 18:48:54 +00:00
|
|
|
local script_path="${${(%):-%x}:h}"
|
|
|
|
local wd_remove_output=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
|
2024-06-16 06:44:44 +00:00
|
|
|
entries=("All warp points:" "Press enter to select. Press delete to remove" "${entries[@]}")
|
2024-05-21 18:48:54 +00:00
|
|
|
local fzf_bind="delete:execute(echo {} | awk -F ' -> ' '{print \$1}' | xargs -I {} "$script_path/wd.sh" rm {} > "$wd_remove_output")+abort"
|
2024-06-16 06:44:44 +00:00
|
|
|
local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf --height 100% --reverse --header-lines=2 --bind="$fzf_bind")
|
2024-05-21 18:48:54 +00:00
|
|
|
if [[ -e $wd_remove_output ]]; then
|
|
|
|
cat "$wd_remove_output"
|
|
|
|
rm "$wd_remove_output"
|
|
|
|
fi
|
2024-05-12 10:42:59 +00:00
|
|
|
if [[ -n $selected_entry ]]; then
|
|
|
|
local selected_point="${selected_entry%% ->*}"
|
|
|
|
selected_point=$(echo "$selected_point" | xargs)
|
|
|
|
wd $selected_point
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-21 18:48:54 +00:00
|
|
|
wd_browse_widget() {
|
2024-11-24 12:25:13 +00:00
|
|
|
if [[ -e $wd_config_file ]]; then
|
2024-05-21 18:48:54 +00:00
|
|
|
wd_browse
|
|
|
|
saved_buffer=$BUFFER
|
|
|
|
saved_cursor=$CURSOR
|
|
|
|
BUFFER=
|
|
|
|
zle redisplay
|
|
|
|
zle accept-line
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
wd_restore_buffer() {
|
2024-06-16 06:44:44 +00:00
|
|
|
if [[ -n $saved_buffer ]]; then
|
|
|
|
BUFFER=$saved_buffer
|
|
|
|
CURSOR=$saved_cursor
|
|
|
|
fi
|
2024-05-21 18:48:54 +00:00
|
|
|
saved_buffer=
|
|
|
|
saved_cursor=1
|
|
|
|
}
|
|
|
|
|
2013-11-16 01:15:17 +00:00
|
|
|
wd_list_all()
|
|
|
|
{
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_BLUE" "All warp points:"
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2024-11-24 12:25:13 +00:00
|
|
|
entries=$(sed "s:${HOME}:~:g" "$wd_config_file")
|
2015-11-23 21:22:45 +00:00
|
|
|
|
|
|
|
max_warp_point_length=0
|
|
|
|
while IFS= read -r line
|
|
|
|
do
|
|
|
|
arr=(${(s,:,)line})
|
|
|
|
key=${arr[1]}
|
|
|
|
|
|
|
|
length=${#key}
|
|
|
|
if [[ length -gt max_warp_point_length ]]
|
|
|
|
then
|
|
|
|
max_warp_point_length=$length
|
|
|
|
fi
|
2020-09-09 12:24:43 +00:00
|
|
|
done <<< "$entries"
|
2015-11-23 21:22:45 +00:00
|
|
|
|
2014-07-15 15:42:30 +00:00
|
|
|
while IFS= read -r line
|
2013-11-16 01:15:17 +00:00
|
|
|
do
|
|
|
|
if [[ $line != "" ]]
|
|
|
|
then
|
|
|
|
arr=(${(s,:,)line})
|
|
|
|
key=${arr[1]}
|
2022-11-06 19:52:53 +00:00
|
|
|
val=${line#"${arr[1]}:"}
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
if [[ -z $wd_quiet_mode ]]
|
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
printf "%${max_warp_point_length}s -> %s\n" "$key" "$val"
|
2014-09-07 19:56:34 +00:00
|
|
|
fi
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
2020-09-09 12:24:43 +00:00
|
|
|
done <<< "$entries"
|
2014-07-15 15:42:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 23:13:39 +00:00
|
|
|
wd_ls()
|
|
|
|
{
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_getdir "$1"
|
|
|
|
ls "${dir/#\~/$HOME}"
|
2015-01-25 23:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wd_path()
|
|
|
|
{
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_getdir "$1"
|
2020-10-09 15:38:02 +00:00
|
|
|
echo "$(echo "$dir" | sed "s:~:${HOME}:g")"
|
2015-01-25 23:13:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 15:42:30 +00:00
|
|
|
wd_show()
|
|
|
|
{
|
2014-09-07 19:56:34 +00:00
|
|
|
local name_arg=$1
|
2024-10-13 19:10:29 +00:00
|
|
|
local show_pwd
|
2014-09-07 19:56:34 +00:00
|
|
|
# if there's an argument we look up the value
|
2020-09-09 12:24:43 +00:00
|
|
|
if [[ -n $name_arg ]]
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
|
|
|
if [[ -z $points[$name_arg] ]]
|
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_BLUE" "No warp point named $name_arg"
|
2014-09-07 19:56:34 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_GREEN" "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
|
2014-09-07 19:56:34 +00:00
|
|
|
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
|
2024-10-13 19:10:29 +00:00
|
|
|
show_pwd="${PWD/$HOME/~}"
|
|
|
|
if [[ ${points[(r)$show_pwd]} == "$show_pwd" ]]
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
|
|
|
for name in ${(k)points}
|
|
|
|
do
|
2024-10-13 19:10:29 +00:00
|
|
|
if [[ $points[$name] == "$show_pwd" ]]
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
|
|
|
wd_matches[$(($#wd_matches+1))]=$name
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_BLUE" "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
|
2014-09-07 19:56:34 +00:00
|
|
|
else
|
2024-10-13 19:10:29 +00:00
|
|
|
wd_print_msg "$WD_YELLOW" "No warp point to $show_pwd"
|
2014-09-07 19:56:34 +00:00
|
|
|
fi
|
|
|
|
fi
|
2013-11-16 01:15:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
wd_clean() {
|
|
|
|
local force=$1
|
|
|
|
local count=0
|
|
|
|
local wd_tmp=""
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
while read -r line
|
2014-09-07 19:56:34 +00:00
|
|
|
do
|
|
|
|
if [[ $line != "" ]]
|
|
|
|
then
|
|
|
|
arr=(${(s,:,)line})
|
|
|
|
key=${arr[1]}
|
|
|
|
val=${arr[2]}
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2017-11-01 13:03:34 +00:00
|
|
|
if [ -d "${val/#\~/$HOME}" ]
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_tmp=$wd_tmp"\n"`echo "$line"`
|
2014-09-07 19:56:34 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_YELLOW" "Nonexistent directory: ${key} -> ${val}"
|
2014-09-07 19:56:34 +00:00
|
|
|
count=$((count+1))
|
|
|
|
fi
|
|
|
|
fi
|
2024-11-24 12:25:13 +00:00
|
|
|
done < "$wd_config_file"
|
2014-09-07 19:56:34 +00:00
|
|
|
|
|
|
|
if [[ $count -eq 0 ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_BLUE" "No warp points to clean, carry on!"
|
2013-11-16 01:15:17 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
if [ ! -z "$force" ] || wd_yesorno "Removing ${count} warp points. Continue? (y/n)"
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
2024-11-24 12:25:13 +00:00
|
|
|
echo "$wd_tmp" >! "$wd_config_file"
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_GREEN" "Cleanup complete. ${count} warp point(s) removed"
|
2014-09-07 19:56:34 +00:00
|
|
|
else
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_print_msg "$WD_BLUE" "Cleanup aborted"
|
2014-09-07 19:56:34 +00:00
|
|
|
fi
|
2013-11-16 01:15:17 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:35:51 +00:00
|
|
|
wd_export_static_named_directories() {
|
2020-09-09 12:24:43 +00:00
|
|
|
if [[ ! -z $WD_EXPORT ]]
|
2020-06-03 16:35:51 +00:00
|
|
|
then
|
2024-11-24 12:25:13 +00:00
|
|
|
command grep '^[0-9a-zA-Z_-]\+:' "$wd_config_file" | sed -e "s,~,$HOME," -e 's/:/=/' | while read -r warpdir ; do
|
2020-08-05 12:19:26 +00:00
|
|
|
hash -d "$warpdir"
|
2020-06-03 16:35:51 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-21 18:48:54 +00:00
|
|
|
WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
|
2014-09-07 19:56:34 +00:00
|
|
|
local WD_QUIET=0
|
|
|
|
local WD_EXIT_CODE=0
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# 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.
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
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 \
|
2020-09-09 12:24:43 +00:00
|
|
|
f=wd_force_mode -force=wd_force_mode
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
if [[ ! -z $wd_print_version ]]
|
|
|
|
then
|
|
|
|
echo "wd version $WD_VERSION"
|
|
|
|
fi
|
|
|
|
|
2024-11-24 12:25:13 +00:00
|
|
|
# set the config file from variable or default
|
|
|
|
typeset wd_config_file=${WD_CONFIG:-$HOME/.warprc}
|
2014-09-07 19:56:34 +00:00
|
|
|
if [[ ! -z $wd_alt_config ]]
|
|
|
|
then
|
2024-11-24 12:25:13 +00:00
|
|
|
# prefer the flag if provided
|
|
|
|
wd_config_file=$wd_alt_config[2]
|
2014-09-07 19:56:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check if config file exists
|
2024-11-24 12:25:13 +00:00
|
|
|
if [ ! -e "$wd_config_file" ]
|
2014-09-07 19:56:34 +00:00
|
|
|
then
|
|
|
|
# if not, create config file
|
2024-11-24 12:25:13 +00:00
|
|
|
touch "$wd_config_file"
|
2020-06-03 16:35:51 +00:00
|
|
|
else
|
|
|
|
wd_export_static_named_directories
|
2014-09-07 19:56:34 +00:00
|
|
|
fi
|
|
|
|
|
2022-11-06 19:52:53 +00:00
|
|
|
# disable extendedglob for the complete wd execution time
|
|
|
|
setopt | grep -q extendedglob
|
|
|
|
wd_extglob_is_set=$?
|
2024-05-12 10:42:59 +00:00
|
|
|
if (( wd_extglob_is_set == 0 )); then
|
|
|
|
setopt noextendedglob
|
|
|
|
fi
|
2022-11-06 19:52:53 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# load warp points
|
|
|
|
typeset -A points
|
|
|
|
while read -r line
|
|
|
|
do
|
|
|
|
arr=(${(s,:,)line})
|
|
|
|
key=${arr[1]}
|
2017-11-01 13:03:34 +00:00
|
|
|
# join the rest, in case the path contains colons
|
|
|
|
val=${(j,:,)arr[2,-1]}
|
2014-09-07 19:56:34 +00:00
|
|
|
|
|
|
|
points[$key]=$val
|
2024-11-24 12:25:13 +00:00
|
|
|
done < "$wd_config_file"
|
2013-11-16 01:15:17 +00:00
|
|
|
|
|
|
|
# get opts
|
2020-09-09 12:24:43 +00:00
|
|
|
args=$(getopt -o a:r:c:lhs -l add:,rm:,clean,list,ls:,path:,help,show -- $*)
|
2013-11-16 01:15:17 +00:00
|
|
|
|
2014-09-07 19:56:34 +00:00
|
|
|
# check if no arguments were given, and that version is not set
|
|
|
|
if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
|
2013-11-16 01:15:17 +00:00
|
|
|
then
|
|
|
|
wd_print_usage
|
|
|
|
|
2020-09-09 12:24:43 +00:00
|
|
|
# check if config file is writeable
|
2024-11-24 12:25:13 +00:00
|
|
|
elif [ ! -w "$wd_config_file" ]
|
2013-11-25 23:45:24 +00:00
|
|
|
then
|
2014-07-15 15:42:30 +00:00
|
|
|
# do nothing
|
2013-11-25 23:45:24 +00:00
|
|
|
# can't run `exit`, as this would exit the executing shell
|
2024-11-24 12:25:13 +00:00
|
|
|
wd_exit_fail "\'$wd_config_file\' is not writeable."
|
2013-11-25 23:45:24 +00:00
|
|
|
|
|
|
|
else
|
2014-09-07 19:56:34 +00:00
|
|
|
# parse rest of options
|
2020-06-03 16:35:51 +00:00
|
|
|
local wd_o
|
|
|
|
for wd_o
|
2013-11-16 01:15:17 +00:00
|
|
|
do
|
2020-06-03 16:35:51 +00:00
|
|
|
case "$wd_o"
|
2014-03-04 19:25:54 +00:00
|
|
|
in
|
2020-06-03 16:35:51 +00:00
|
|
|
"-a"|"--add"|"add")
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_add "$2" "$wd_force_mode"
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
2024-05-12 10:42:59 +00:00
|
|
|
"-b"|"browse")
|
|
|
|
wd_browse
|
|
|
|
break
|
|
|
|
;;
|
2024-05-21 18:48:54 +00:00
|
|
|
"-c"|"--addcd"|"addcd")
|
|
|
|
wd_addcd "$2" "$3" "$wd_force_mode"
|
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-e"|"export")
|
|
|
|
wd_export_static_named_directories
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
"-r"|"--remove"|"rm")
|
2020-09-09 12:24:43 +00:00
|
|
|
# Passes all the arguments as a single string separated by whitespace to wd_remove
|
|
|
|
wd_remove "${@:2}"
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-l"|"list")
|
2014-03-04 19:25:54 +00:00
|
|
|
wd_list_all
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-ls"|"ls")
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_ls "$2"
|
2015-01-25 23:13:39 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-p"|"--path"|"path")
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_path "$2"
|
2015-01-25 23:13:39 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-h"|"--help"|"help")
|
2014-03-04 19:25:54 +00:00
|
|
|
wd_print_usage
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-s"|"--show"|"show")
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_show "$2"
|
2014-09-07 19:56:34 +00:00
|
|
|
break
|
|
|
|
;;
|
2020-06-03 16:35:51 +00:00
|
|
|
"-c"|"--clean"|"clean")
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_clean "$wd_force_mode"
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
2020-09-09 12:24:43 +00:00
|
|
|
wd_warp "$wd_o" "$2"
|
2013-11-16 01:15:17 +00:00
|
|
|
break
|
|
|
|
;;
|
2014-03-04 19:25:54 +00:00
|
|
|
--)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
2013-11-16 01:15:17 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
## garbage collection
|
|
|
|
# if not, next time warp will pick up variables from this run
|
|
|
|
# remember, there's no sub shell
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2024-05-12 10:42:59 +00:00
|
|
|
if (( wd_extglob_is_set == 0 )); then
|
|
|
|
setopt extendedglob
|
|
|
|
fi
|
2022-11-06 19:52:53 +00:00
|
|
|
|
|
|
|
unset wd_extglob_is_set
|
2014-07-15 15:42:30 +00:00
|
|
|
unset wd_warp
|
|
|
|
unset wd_add
|
2024-05-21 18:48:54 +00:00
|
|
|
unset wd_addcd
|
2014-07-15 15:42:30 +00:00
|
|
|
unset wd_remove
|
|
|
|
unset wd_show
|
|
|
|
unset wd_list_all
|
|
|
|
unset wd_print_msg
|
2014-09-07 19:56:34 +00:00
|
|
|
unset wd_yesorno
|
2014-07-15 15:42:30 +00:00
|
|
|
unset wd_print_usage
|
2014-09-07 19:56:34 +00:00
|
|
|
unset wd_alt_config
|
2024-11-24 12:25:13 +00:00
|
|
|
unset wd_config_file
|
2014-09-07 19:56:34 +00:00
|
|
|
unset wd_quiet_mode
|
|
|
|
unset wd_print_version
|
2024-11-24 12:25:13 +00:00
|
|
|
unset wd_force_mode
|
2020-06-03 16:35:51 +00:00
|
|
|
unset wd_export_static_named_directories
|
|
|
|
unset wd_o
|
2014-07-15 15:42:30 +00:00
|
|
|
|
2014-03-04 19:25:54 +00:00
|
|
|
unset args
|
2014-07-15 15:42:30 +00:00
|
|
|
unset points
|
2014-03-04 19:25:54 +00:00
|
|
|
unset val &> /dev/null # fixes issue #1
|
2014-09-07 19:56:34 +00:00
|
|
|
|
2024-09-15 06:16:50 +00:00
|
|
|
return $WD_EXIT_CODE
|