diff --git a/README.md b/README.md index 9a4fc75..019d5a9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ $ cd ~/doc/programming/web/arrdem.com $ echo $PWD /home/reid/doc/programming/web/arrdem.com # that's long as hell... let's fix that -$ label arrdem.com +$ label add arrdem.com $ cd $ echo $PWD /home/reid @@ -17,8 +17,6 @@ $ echo $PWD /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 seperated -- the error messages on a missing label are useless +- the labels file (~/.labels.tsv) _must_ be tab separated diff --git a/goto.plugin.zsh b/goto.plugin.zsh index 576a7dd..2cb2fa7 100644 --- a/goto.plugin.zsh +++ b/goto.plugin.zsh @@ -3,54 +3,124 @@ # goto.zsh, a tool for bookmarking directories function _awk { - which awk &>/dev/null && awk $@ || \ - which gawk &>/dev/null && gawk $@ -}; + if command -v gawk &>/dev/null; then + 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 { 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 [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 { - if [[ "${#}" -eq 0 ]]; then - echo "Usage: $ goto " - echo " jumps to a record set by label" - elif [[ "$1" == "ls" ]]; then - _awk '{print $1;}' `_goto_file` | column -t - else - dir=$(_awk "/^$1\s/ {print \$2;exit;}" `_goto_file` | head -n 1) - if [[ "${dir}" != "/*" ]]; then - dir="${HOME}/${dir}" - fi - if [[ ! -e "${dir}" ]]; then - echo "Error: Label '$1' resolved to missing path '$dir'" - else - cd "${dir}" - fi - fi -}; + : "${1:=--help}" + + case "$1" in + -h|--help|help) + cat <> `_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