This commit is contained in:
Motiejus Jakštys 2024-04-12 14:59:47 +03:00 committed by Motiejus Jakštys
parent 301a46fa7d
commit 19e313d567

View File

@ -4,24 +4,24 @@ _GG_MAXDEPTH=5
gg() { gg() {
local _gopath local _gopath
_gopath=$(git rev-parse --show-toplevel) _gopath=$(git rev-parse --show-toplevel)
local paths=($(g "$@")) local paths=($(g "$@"))
local path_index=0 local path_index=0
if [ ${#paths[@]} -gt 1 ]; then if [ ${#paths[@]} -gt 1 ]; then
local c=1 local c=1
for path in "${paths[@]}"; do for path in "${paths[@]}"; do
echo "[$c]: cd ${_gopath}/${path}" echo "[$c]: cd ${_gopath}/${path}"
c=$((c+1)) c=$((c + 1))
done done
echo -n "Go to which path: " echo -n "Go to which path: "
read -r path_index read -r path_index
path_index=$((path_index-1)) path_index=$((path_index - 1))
fi fi
local path=${paths[$path_index]} local path=${paths[$path_index]}
cd "$_gopath/$path" || { cd "$_gopath/$path" || {
>&2 echo "?" echo >&2 "?"
exit 1 exit 1
} }
} }
@ -32,17 +32,16 @@ gg() {
g() { g() {
local pkg_candidates local pkg_candidates
pkg_candidates="$( (cd "$_gopath" && find . -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -path "*/$1" -and -not -path '*/vendor/*' -print) | sed 's/^\.\///g')" pkg_candidates="$( (cd "$_gopath" && find . -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -path "*/$1" -and -not -path '*/vendor/*' -print) | sed 's/^\.\///g')"
echo "$pkg_candidates" | awk '{print length, $0 }' | sort -n | awk '{print $2}' echo "$pkg_candidates" | awk '{print length, $0 }' | sort -n | awk '{print $2}'
} }
# #
# Bash autocomplete for g and gg functions. # Bash autocomplete for g and gg functions.
# #
_g_complete() _g_complete() {
{
COMPREPLY=() COMPREPLY=()
local cur local cur
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(for f in $(find "$_gopath" -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur") ) COMPREPLY=($(compgen -W "$(for f in $(find "$_gopath" -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur"))
return 0 return 0
} }
complete -F _g_complete g complete -F _g_complete g