mirror of
https://github.com/iridakos/goto.git
synced 2025-05-19 08:50:22 -07:00
Merge pull request #19 from OmriHab/dev
Added duplicate note. Great work @OmriHab !
This commit is contained in:
commit
f92a4df24a
56
goto.bash
56
goto.bash
@ -26,6 +26,7 @@
|
|||||||
function goto()
|
function goto()
|
||||||
{
|
{
|
||||||
local target
|
local target
|
||||||
|
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,13 +102,22 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Lists duplicate directory aliases
|
||||||
|
function _goto_find_duplicate()
|
||||||
|
{
|
||||||
|
local duplicates=
|
||||||
|
|
||||||
|
duplicates=$(sed -n 's:[^ ]* '"$1"'$:&:p' "$GOTO_DB" 2>/dev/null)
|
||||||
|
echo "$duplicates"
|
||||||
|
}
|
||||||
|
|
||||||
# Registers and alias.
|
# Registers and alias.
|
||||||
function _goto_register_alias()
|
function _goto_register_alias()
|
||||||
{
|
{
|
||||||
@ -136,8 +146,14 @@ function _goto_register_alias()
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
local duplicate
|
||||||
|
duplicate=$(_goto_find_duplicate "$directory")
|
||||||
|
if [ -n "$duplicate" ]; then
|
||||||
|
_goto_warning "duplicate alias(es) found: \\n$duplicate"
|
||||||
|
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."
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,21 +172,23 @@ function _goto_unregister_alias
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
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 +221,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"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +229,26 @@ function _goto_find_alias_directory()
|
|||||||
# Used for common error output.
|
# Used for common error output.
|
||||||
function _goto_error()
|
function _goto_error()
|
||||||
{
|
{
|
||||||
(>&2 echo "goto error: $1")
|
(>&2 echo -e "goto error: $1")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Displays the given warning.
|
||||||
|
# Used for common warning output.
|
||||||
|
function _goto_warning()
|
||||||
|
{
|
||||||
|
(>&2 echo -e "goto warning: $1")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Displays entries with aliases starting as the given one.
|
||||||
|
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.
|
||||||
@ -223,6 +260,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 +282,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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user