2016-09-30 17:19:41 +00:00
|
|
|
# goto.zsh
|
|
|
|
|
2017-04-18 00:29:17 +00:00
|
|
|
function _awk {
|
2017-12-05 23:38:36 +00:00
|
|
|
which awk &>/dev/null && awk $@ || \
|
|
|
|
which gawk &>/dev/null && gawk $@
|
2017-04-18 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 17:19:41 +00:00
|
|
|
function _gotofile {
|
2017-04-18 00:29:17 +00:00
|
|
|
echo $GOTO_FILE "$HOME/.labels.tsv" | _awk "{print \$1}"
|
2016-09-30 17:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _makeLabel {
|
2017-12-05 23:38:36 +00:00
|
|
|
printf "%s %s\n" $1 $(echo $2 | tr -d "$HOME") >> `_gotofile`
|
2016-09-30 17:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function label {
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
|
|
|
echo "Usage: $ label <name> [dir]"
|
|
|
|
echo " creates a label which goto can cd to"
|
|
|
|
elif [ -d "$2" ]
|
|
|
|
then
|
|
|
|
_makeLabel $1 $2
|
|
|
|
else
|
|
|
|
_makeLabel $1 $PWD
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function goto {
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
|
|
|
echo "Usage: $ goto <name>"
|
|
|
|
echo " jumps to a record set by label"
|
|
|
|
elif [[ "$1" == "ls" ]]
|
|
|
|
then
|
2017-04-18 00:33:10 +00:00
|
|
|
_awk "{ print \$1 }" `_gotofile` | column -t
|
2016-09-30 17:19:41 +00:00
|
|
|
else
|
2017-12-05 23:38:36 +00:00
|
|
|
cd "$HOME/$(_awk "/^$1\s/ {print \$2;exit;}" `_gotofile` | head -n 1)"
|
2016-09-30 17:19:41 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function _goto {
|
2017-04-18 00:29:17 +00:00
|
|
|
for label in $(_awk '{print $1}' `_gotofile`)
|
2016-09-30 17:19:41 +00:00
|
|
|
do
|
|
|
|
compadd "$@" $label
|
|
|
|
done
|
|
|
|
};
|
|
|
|
|
|
|
|
compdef _goto goto
|