config/shared/home/gg.sh

53 lines
1.5 KiB
Bash
Raw Permalink Normal View History

2024-04-12 14:57:54 +03:00
# shellcheck shell=bash
_GG_MAXDEPTH=5
2024-04-12 14:02:28 +03:00
gg() {
2024-04-12 14:57:54 +03:00
local _gopath
_gopath=$(git rev-parse --show-toplevel)
2024-04-12 14:59:47 +03:00
local paths=($(g "$@"))
local path_index=0
2024-04-12 14:02:28 +03:00
2024-04-12 14:59:47 +03:00
if [ ${#paths[@]} -gt 1 ]; then
local c=1
for path in "${paths[@]}"; do
echo "[$c]: cd ${_gopath}/${path}"
c=$((c + 1))
done
echo -n "Go to which path: "
read -r path_index
2024-04-12 14:02:28 +03:00
2024-04-12 14:59:47 +03:00
path_index=$((path_index - 1))
fi
2024-04-12 14:02:28 +03:00
2024-04-12 14:59:47 +03:00
local path=${paths[$path_index]}
cd "$_gopath/$path" || {
echo >&2 "?"
2024-04-12 14:57:54 +03:00
exit 1
}
2024-04-12 14:02:28 +03:00
}
#
# Print the directories of the specified Go package name.
#
g() {
2024-04-12 14:57:54 +03:00
local pkg_candidates
2024-05-08 13:23:13 +03:00
local _gopath
_gopath=$(git rev-parse --show-toplevel)
2024-06-21 08:58:16 +03:00
pkg_candidates="$( (cd "$_gopath" && find . m3/src -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -path "*/$1" -and -not -path '*/vendor/*' -print) | sed 's/^\.\///g' | sort -u)"
2024-04-12 14:59:47 +03:00
echo "$pkg_candidates" | awk '{print length, $0 }' | sort -n | awk '{print $2}'
2024-04-12 14:02:28 +03:00
}
#
# Bash autocomplete for g and gg functions.
#
2024-04-12 14:59:47 +03:00
_g_complete() {
2024-04-12 14:02:28 +03:00
COMPREPLY=()
local cur
cur="${COMP_WORDS[COMP_CWORD]}"
2024-05-08 13:23:13 +03:00
local _gopath
_gopath=$(git rev-parse --show-toplevel)
2024-06-19 09:37:57 +03:00
COMPREPLY=($(compgen -W "$(for f in $(find "$_gopath" "${_gopath}/m3/src" -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur"))
2024-04-12 14:02:28 +03:00
return 0
}
complete -F _g_complete g
complete -F _g_complete gg