2018-01-16 19:58:42 +00:00
|
|
|
#!/usr/bin/zsh
|
|
|
|
#
|
|
|
|
# goto.zsh, a tool for bookmarking directories
|
2016-09-30 17:19:41 +00:00
|
|
|
|
2017-04-18 00:29:17 +00:00
|
|
|
function _awk {
|
2018-01-16 19:10:49 +00:00
|
|
|
which awk &>/dev/null && awk $@ || \
|
|
|
|
which gawk &>/dev/null && gawk $@
|
|
|
|
};
|
|
|
|
|
2018-01-16 20:33:54 +00:00
|
|
|
function _goto_file {
|
|
|
|
echo "${GOTO_FILE:-${HOME}/.labels.tsv}"
|
2018-01-16 20:09:06 +00:00
|
|
|
};
|
2016-09-30 17:19:41 +00:00
|
|
|
|
2018-01-16 19:10:49 +00:00
|
|
|
function _make_label {
|
2018-01-16 20:33:54 +00:00
|
|
|
printf '%s %s\n' "$1" $(echo "$2" | tr -d "$HOME/") >> `_goto_file`
|
2018-01-16 19:10:49 +00:00
|
|
|
};
|
2016-09-30 17:19:41 +00:00
|
|
|
|
|
|
|
function label {
|
2018-01-16 20:33:54 +00:00
|
|
|
if [[ "${#}" -eq 0 ]]; then
|
2018-01-16 19:10:49 +00:00
|
|
|
echo "Usage: $ label <name> [dir]"
|
|
|
|
echo " creates a label which goto can cd to"
|
2018-01-16 20:33:54 +00:00
|
|
|
elif [[ -d "${2}" ]]; then
|
2018-01-16 19:10:49 +00:00
|
|
|
_make_label "${1}" "${2}"
|
2018-01-16 20:33:54 +00:00
|
|
|
elsep
|
2018-01-16 19:10:49 +00:00
|
|
|
_make_label "${1}" "${PWD}"
|
|
|
|
fi
|
|
|
|
};
|
2016-09-30 17:19:41 +00:00
|
|
|
|
|
|
|
function goto {
|
2018-01-16 19:10:49 +00:00
|
|
|
if [[ "${#}" -eq 0 ]]; then
|
|
|
|
echo "Usage: $ goto <name>"
|
|
|
|
echo " jumps to a record set by label"
|
|
|
|
elif [[ "$1" == "ls" ]]; then
|
2018-01-16 20:33:54 +00:00
|
|
|
_awk '{print $1;}' `_goto_file` | column -t
|
2018-01-16 19:10:49 +00:00
|
|
|
else
|
2018-01-16 20:33:54 +00:00
|
|
|
dir=$(_awk "/^$1\s/ {print \$2;exit;}" `_goto_file` | head -n 1)
|
2018-01-16 20:08:09 +00:00
|
|
|
if [[ "${dir}" != "/*" ]]; then
|
2018-01-16 20:33:54 +00:00
|
|
|
dir="${HOME}/${dir}"
|
2018-01-16 19:10:49 +00:00
|
|
|
fi
|
2018-01-16 20:08:09 +00:00
|
|
|
if [[ ! -e "${dir}" ]]; then
|
2018-01-16 20:33:54 +00:00
|
|
|
echo "Error: Label '$1' resolved to missing path '$dir'"
|
2016-09-30 17:19:41 +00:00
|
|
|
else
|
2018-01-16 20:08:09 +00:00
|
|
|
cd "${dir}"
|
2016-09-30 17:19:41 +00:00
|
|
|
fi
|
2018-01-16 19:10:49 +00:00
|
|
|
fi
|
|
|
|
};
|