From 25d2be076e40794e9571a7090b87b55517fa4db2 Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Thu, 11 Aug 2022 00:11:04 -0600 Subject: [PATCH] And rewrite into fragments --- _awk | 8 +++ _goto | 7 +++ _goto_file | 2 + _label | 24 +++++++++ _sed | 9 ++++ goto | 19 ++++++++ goto.plugin.zsh | 126 ++---------------------------------------------- label | 38 +++++++++++++++ 8 files changed, 112 insertions(+), 121 deletions(-) create mode 100755 _awk create mode 100755 _goto create mode 100755 _goto_file create mode 100755 _label create mode 100755 _sed create mode 100755 goto create mode 100755 label diff --git a/_awk b/_awk new file mode 100755 index 0000000..2ee6511 --- /dev/null +++ b/_awk @@ -0,0 +1,8 @@ +#!/usr/bin/env zsh +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 diff --git a/_goto b/_goto new file mode 100755 index 0000000..d34cc2c --- /dev/null +++ b/_goto @@ -0,0 +1,7 @@ +#compdef _goto goto +function _goto { + for label in $(label ls) + do + compadd "$@" "${label}" + done +} diff --git a/_goto_file b/_goto_file new file mode 100755 index 0000000..7b01368 --- /dev/null +++ b/_goto_file @@ -0,0 +1,2 @@ +#!/usr/bin/zsh +echo "${GOTO_FILE:-${HOME}/.labels.tsv}" diff --git a/_label b/_label new file mode 100755 index 0000000..bbe48ab --- /dev/null +++ b/_label @@ -0,0 +1,24 @@ +#compdef _label label +_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) + _goto # get the labels as completions + ;; + esac + ;; + esac +} diff --git a/_sed b/_sed new file mode 100755 index 0000000..0b4da1f --- /dev/null +++ b/_sed @@ -0,0 +1,9 @@ +#!/usr/bin/zsh +# 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 diff --git a/goto b/goto new file mode 100755 index 0000000..af1f9fc --- /dev/null +++ b/goto @@ -0,0 +1,19 @@ +#!/usr/bin/zsh + +: "${1:=--help}" + +case "$1" in + -h|--help|help) + cat </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}" -} - -#################################################################################################### +ROOT="$(realpath $(dirname $0))" +# Note that since we manipulate $cwd, goto HAS to be a function. +# So we use this shim to hit the right script. function goto { - : "${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 diff --git a/label b/label new file mode 100755 index 0000000..e4e2a23 --- /dev/null +++ b/label @@ -0,0 +1,38 @@ +#!/usr/bin/zsh + +: "${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 + exit 1 + ;; +esac