And rewrite into fragments
This commit is contained in:
parent
83456d5998
commit
25d2be076e
8 changed files with 112 additions and 121 deletions
8
_awk
Executable file
8
_awk
Executable file
|
@ -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
|
7
_goto
Executable file
7
_goto
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#compdef _goto goto
|
||||||
|
function _goto {
|
||||||
|
for label in $(label ls)
|
||||||
|
do
|
||||||
|
compadd "$@" "${label}"
|
||||||
|
done
|
||||||
|
}
|
2
_goto_file
Executable file
2
_goto_file
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/usr/bin/zsh
|
||||||
|
echo "${GOTO_FILE:-${HOME}/.labels.tsv}"
|
24
_label
Executable file
24
_label
Executable file
|
@ -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
|
||||||
|
}
|
9
_sed
Executable file
9
_sed
Executable file
|
@ -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
|
19
goto
Executable file
19
goto
Executable file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/zsh
|
||||||
|
|
||||||
|
: "${1:=--help}"
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
-h|--help|help)
|
||||||
|
cat <<EOF
|
||||||
|
Usage:
|
||||||
|
$ goto [label]
|
||||||
|
|
||||||
|
Change directory to a labeled location.
|
||||||
|
See label for label control operations
|
||||||
|
EOF
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
cd "$(label get "$1")"
|
||||||
|
;;
|
||||||
|
esac
|
126
goto.plugin.zsh
126
goto.plugin.zsh
|
@ -1,126 +1,10 @@
|
||||||
#!/usr/bin/zsh
|
#!/usr/bin/zsh
|
||||||
#
|
# this is a stub, commands are elsewhere
|
||||||
# goto.zsh, a tool for bookmarking directories
|
|
||||||
|
|
||||||
function _awk {
|
ROOT="$(realpath $(dirname $0))"
|
||||||
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}"
|
|
||||||
}
|
|
||||||
|
|
||||||
####################################################################################################
|
|
||||||
|
|
||||||
|
# Note that since we manipulate $cwd, goto HAS to be a function.
|
||||||
|
# So we use this shim to hit the right script.
|
||||||
function goto {
|
function goto {
|
||||||
: "${1:=--help}"
|
. "$ROOT/goto"
|
||||||
|
|
||||||
case "$1" in
|
|
||||||
-h|--help|help)
|
|
||||||
cat <<EOF
|
|
||||||
Usage:
|
|
||||||
$ goto [label]
|
|
||||||
|
|
||||||
Change directory to a labeled location.
|
|
||||||
See label for label control operations
|
|
||||||
EOF
|
|
||||||
;;
|
|
||||||
|
|
||||||
*)
|
|
||||||
cd "$(label get "$1")"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _goto {
|
|
||||||
for label in $(label ls)
|
|
||||||
do
|
|
||||||
compadd "$@" "${label}"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
38
label
Executable file
38
label
Executable file
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/zsh
|
||||||
|
|
||||||
|
: "${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
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
Loading…
Reference in a new issue