An overhaul, after all these years.

This commit is contained in:
Reid 'arrdem' McKenzie 2022-08-10 23:32:51 -06:00
parent dd84276d42
commit 83456d5998
2 changed files with 112 additions and 44 deletions

View file

@ -8,7 +8,7 @@ $ cd ~/doc/programming/web/arrdem.com
$ echo $PWD $ echo $PWD
/home/reid/doc/programming/web/arrdem.com /home/reid/doc/programming/web/arrdem.com
# that's long as hell... let's fix that # that's long as hell... let's fix that
$ label arrdem.com $ label add arrdem.com
$ cd $ cd
$ echo $PWD $ echo $PWD
/home/reid /home/reid
@ -17,8 +17,6 @@ $ echo $PWD
/home/reid/doc/programming/web/arrdem.com /home/reid/doc/programming/web/arrdem.com
``` ```
This is a relatively alpha script and could use some serious attention. This is a alpha script and could use some serious attention.
- no way to delete labels once you set them besides editing the labels file - the labels file (~/.labels.tsv) _must_ be tab separated
- the labels file (~/.labels.tsv) _must_ be tab seperated
- the error messages on a missing label are useless

View file

@ -3,54 +3,124 @@
# goto.zsh, a tool for bookmarking directories # goto.zsh, a tool for bookmarking directories
function _awk { function _awk {
which awk &>/dev/null && awk $@ || \ if command -v gawk &>/dev/null; then
which gawk &>/dev/null && gawk $@ gawk "${@}"
}; elif command -v awk &>/dev/null; then
awk "${@}"
else
echo "Unable to find an awk!" >&2; return 1
fi
}
function _sed {
# MacOS support - prefer GNU sed eg. from Homebrew to 'sed'
if command -v gsed &>/dev/null; then
gsed "$@"
elif command -v sed &>/dev/null; then
sed "$@"
else
echo "Unable to find a sed!" >&2; return 1
fi
}
function _goto_file { function _goto_file {
echo "${GOTO_FILE:-${HOME}/.labels.tsv}" echo "${GOTO_FILE:-${HOME}/.labels.tsv}"
}; }
function _make_label { ####################################################################################################
sed -i '' -e '$a\' `_goto_file` # Fix potentially missing last newline
printf '%s %s\n' "$1" $(echo "$2" | sed "s^$HOME^^g") >> `_goto_file`
};
function label {
if [[ "${#}" -eq 0 ]]; then
echo "Usage: $ label <name> [dir]"
echo " creates a label which goto can cd to"
elif [[ -d "${2}" ]]; then
_make_label "${1}" "${2}"
else
_make_label "${1}" "${PWD}"
fi
};
function goto { function goto {
if [[ "${#}" -eq 0 ]]; then : "${1:=--help}"
echo "Usage: $ goto <name>"
echo " jumps to a record set by label" case "$1" in
elif [[ "$1" == "ls" ]]; then -h|--help|help)
_awk '{print $1;}' `_goto_file` | column -t cat <<EOF
else Usage:
dir=$(_awk "/^$1\s/ {print \$2;exit;}" `_goto_file` | head -n 1) $ goto [label]
if [[ "${dir}" != "/*" ]]; then
dir="${HOME}/${dir}" Change directory to a labeled location.
fi See label for label control operations
if [[ ! -e "${dir}" ]]; then EOF
echo "Error: Label '$1' resolved to missing path '$dir'" ;;
else
cd "${dir}" *)
fi cd "$(label get "$1")"
fi ;;
}; esac
}
function _goto { function _goto {
for label in $(awk '{print $1}' `_goto_file`) for label in $(label ls)
do do
compadd "$@" $label compadd "$@" "${label}"
done done
}; }
compdef _goto goto compdef _goto goto
####################################################################################################
function label {
: "${1:=--help}"
case "$1" in
-h|--help|help)
cat <<EOF
Usage:
$ label [add | get | rm | ls]
Set, list or manipulate configured labeles.
Note: the words 'help' and 'ls' among others are reserved, and cannot be labeled.
EOF
;;
add)
label rm "$2"
_sed -i '' -e '$a\' `_goto_file` # Fix potentially missing last newline
printf '%s %s\n' "${1}" $(echo "$2" | _sed "s^${HOME}^~^g") >> `_goto_file`
;;
get)
realpath "$(_awk "/^$2\s/ {print \$2;exit}" `_goto_file` | _sed "s|^~|$HOME|")"
;;
rm)
_sed -i "^$2^d" `_goto_file`
;;
ls)
_awk '{print $1;}' `_goto_file`
;;
*)
echo "Unknown command: $@" >&2
return 1
;;
esac
}
_label() {
local line state
_arguments -C \
"1: :->cmds" \
"*::arg:->args"
case "$state" in
cmds)
_values "label command" \
"add[Create or overwrite a label.]" \
"ls[List existing labels.]" \
"get[Fetch the location of a label.]" \
"rm[Delete a label.]"
;;
args)
case "$line[1]" in
rm|get)
_labels
;;
esac
;;
esac
}
compdef _label label