Overhault to keep the labels database ~ relative if possible

This commit is contained in:
Reid 'arrdem' McKenzie 2018-01-16 11:10:49 -08:00
parent b776cb30ef
commit d0ad15897d

View file

@ -1,49 +1,57 @@
# goto.zsh # goto.zsh
function _awk { function _awk {
which awk &>/dev/null && awk $@ || \ which awk &>/dev/null && awk $@ || \
which gawk &>/dev/null && gawk $@ which gawk &>/dev/null && gawk $@
} };
function _strip_homedir {
};
function _gotofile { function _gotofile {
echo $GOTO_FILE "$HOME/.labels.tsv" | _awk "{print \$1}" echo $GOTO_FILE "$HOME/.labels.tsv" | _awk "{print \$1}"
} };
function _makeLabel { function _make_label {
printf "%s %s\n" $1 $(echo $2 | tr -d "$HOME") >> `_gotofile` printf "%s %s\n" $1 $(echo $2 | tr -d "$HOME/") >> `_gotofile`
} };
function label { function label {
if [ $# -eq 0 ] if [ "${#}" -eq 0 ]; then
then echo "Usage: $ label <name> [dir]"
echo "Usage: $ label <name> [dir]" echo " creates a label which goto can cd to"
echo " creates a label which goto can cd to" elif [ -d "${2}" ]; then
elif [ -d "$2" ] _make_label "${1}" "${2}"
then else
_makeLabel $1 $2 _make_label "${1}" "${PWD}"
else fi
_makeLabel $1 $PWD };
fi
}
function goto { function goto {
if [ $# -eq 0 ] if [[ "${#}" -eq 0 ]]; then
then echo "Usage: $ goto <name>"
echo "Usage: $ goto <name>" echo " jumps to a record set by label"
echo " jumps to a record set by label" elif [[ "$1" == "ls" ]]; then
elif [[ "$1" == "ls" ]] _awk "{ print \$1 }" `_gotofile` | column -t
then else
_awk "{ print \$1 }" `_gotofile` | column -t dir=$(_awk "/^$1\s/ {print \$2;exit;}" `_gotofile` | head -n 1)
else if [[ "$dir" != "/*" ]]; then
cd "$HOME/$(_awk "/^$1\s/ {print \$2;exit;}" `_gotofile` | head -n 1)" dir="${HOME}/${dir}"
fi fi
} if [[ ! -e "$dir" ]]; then
echo "Error: Label '$1' resolved to missing path '$dir'"
else
cd "$dir"
fi
fi
};
function _goto { function _goto {
for label in $(_awk '{print $1}' `_gotofile`) for label in $(_awk '{print $1}' `_gotofile`)
do do
compadd "$@" $label compadd "$@" $label
done done
}; };
compdef _goto goto compdef _goto goto