Compare commits

...

10 commits

9 changed files with 121 additions and 57 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

8
_awk Executable file
View 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
View file

@ -0,0 +1,7 @@
#compdef _goto goto
function _goto {
for label in $(label ls)
do
compadd "$@" "${label}"
done
}

2
_goto_file Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env zsh
echo "${GOTO_FILE:-${HOME}/.labels.tsv}"

24
_label Executable file
View 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
}

12
_zsh_goto_sed Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env zsh
set -e
# 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
View file

@ -0,0 +1,19 @@
#!/usr/bin/env 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

View file

@ -1,55 +1,10 @@
#!/usr/bin/zsh #!/usr/bin/env zsh
# # this is a stub, commands are elsewhere
# goto.zsh, a tool for bookmarking directories
function _awk { ROOT="$(realpath $(dirname $0))"
which awk &>/dev/null && awk $@ || \
which gawk &>/dev/null && gawk $@
};
function _goto_file {
echo "${GOTO_FILE:-${HOME}/.labels.tsv}"
};
function _make_label {
printf '%s %s\n' "$1" $(echo "$2" | tr -d "$HOME/") >> `_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}"
elsep
_make_label "${1}" "${PWD}"
fi
};
# 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 {
if [[ "${#}" -eq 0 ]]; then . "$ROOT/goto"
echo "Usage: $ goto <name>" }
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
};
function _goto {
for label in $(awk '{print $1}' `_goto_file`)
do
compadd "$@" $label
done
};
compdef _goto goto

39
label Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env 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"
_zsh_goto_sed -i '/^$/d' "$(_goto_file)" # Remove empty lines
_zsh_goto_sed -i -e '$a\' "$(_goto_file)" # Fix potentially missing last newline
printf '%s %s\n' "${2}" $(realpath "${3:-$PWD}" | _zsh_goto_sed "s^${HOME}^~^g") >> "$(_goto_file)"
;;
get)
realpath "$(_awk "/^$2\s/ {print \$2;exit}" `_goto_file` | _zsh_goto_sed "s|^~|$HOME|")"
;;
rm)
_zsh_goto_sed -i "/$2/d" "$(_goto_file)"
;;
ls)
_awk '{print $1;}' "$(_goto_file)"
;;
*)
echo "Unknown command: $@" >&2
exit 1
;;
esac