Added GOTO_DB variable

This commit is contained in:
Omri 2018-03-11 12:03:48 +02:00
parent 860f81e432
commit 6cfe42eda3

View File

@ -26,6 +26,7 @@
function goto() function goto()
{ {
local target local target
readonly GOTO_DB="$HOME/.goto"
if [ -z "$1" ]; then if [ -z "$1" ]; then
# display usage and exit when no args # display usage and exit when no args
@ -101,8 +102,8 @@ function _goto_expand_directory()
function _goto_list_aliases() function _goto_list_aliases()
{ {
local IFS=$'\n' local IFS=$'\n'
if [ -f ~/.goto ]; then if [ -f "$GOTO_DB" ]; then
sed '/^\s*$/d' ~/.goto 2>/dev/null sed '/^\s*$/d' "$GOTO_DB" 2>/dev/null
else else
echo "You haven't configured any directory aliases yet." echo "You haven't configured any directory aliases yet."
fi fi
@ -137,7 +138,7 @@ function _goto_register_alias()
fi fi
# Append entry to file. # Append entry to file.
echo "$1 $directory" >> ~/.goto echo "$1 $directory" >> "$GOTO_DB"
echo "Alias '$1' registered successfully." echo "Alias '$1' registered successfully."
} }
@ -155,22 +156,23 @@ function _goto_unregister_alias
_goto_error "alias '$1' does not exist" _goto_error "alias '$1' does not exist"
return return
fi fi
local readonly GOTO_DB_TMP="$HOME/.goto_"
# Delete entry from file. # Delete entry from file.
sed "/^$1 /d" ~/.goto > ~/.goto_ && mv ~/.goto_ ~/.goto sed "/^$1 /d" "$GOTO_DB" > "$GOTO_DB_TMP" && mv "$GOTO_DB_TMP" "$GOTO_DB"
echo "Alias '$1' unregistered successfully." echo "Alias '$1' unregistered successfully."
} }
# Unregisters aliases whose directories no longer exist. # Unregisters aliases whose directories no longer exist.
function _goto_cleanup() function _goto_cleanup()
{ {
if ! [ -f ~/.goto ]; then if ! [ -f "$GOTO_DB" ]; then
return return
fi fi
local IFS=$'\n' match matches al dir local IFS=$'\n' match matches al dir
read -d '' -r -a matches < ~/.goto read -d '' -r -a matches < "$GOTO_DB"
IFS=' ' IFS=' '
for i in "${!matches[@]}"; do for i in "${!matches[@]}"; do
@ -203,7 +205,7 @@ function _goto_find_alias_directory()
{ {
local resolved local resolved
resolved=$(sed -n "s/^$1 \\(.*\\)/\\1/p" ~/.goto 2>/dev/null) resolved=$(sed -n "s/^$1 \\(.*\\)/\\1/p" "$GOTO_DB" 2>/dev/null)
echo "$resolved" echo "$resolved"
} }
@ -214,6 +216,17 @@ function _goto_error()
(>&2 echo "goto error: $1") (>&2 echo "goto error: $1")
} }
function _goto_print_similar()
{
local similar
similar=$(sed -n "/^$1[^ ]* .*/p" "$GOTO_DB" 2>/dev/null)
if [ -n "$similar" ]; then
(>&2 echo "Did you mean:")
(>&2 echo "$similar" | column -t)
fi
}
# Fetches alias directory, errors if it doesn't exist. # Fetches alias directory, errors if it doesn't exist.
function _goto_resolve_alias() function _goto_resolve_alias()
{ {
@ -223,6 +236,7 @@ function _goto_resolve_alias()
if [ -z "$resolved" ]; then if [ -z "$resolved" ]; then
_goto_error "unregistered alias $1" _goto_error "unregistered alias $1"
_goto_print_similar "$1"
echo "" echo ""
else else
echo "${resolved}" echo "${resolved}"
@ -244,7 +258,7 @@ function _complete_goto_aliases()
local IFS=$'\n' matches al local IFS=$'\n' matches al
# shellcheck disable=SC2207 # shellcheck disable=SC2207
matches=($(sed -n "/^$1/p" ~/.goto 2>/dev/null)) matches=($(sed -n "/^$1/p" "$GOTO_DB" 2>/dev/null))
if [ "${#matches[@]}" -eq "1" ]; then if [ "${#matches[@]}" -eq "1" ]; then
# remove the filenames attribute from the completion method # remove the filenames attribute from the completion method