add gg.sh

This commit is contained in:
Motiejus Jakštys 2024-04-12 14:02:28 +03:00 committed by Motiejus Jakštys
parent 23ec7f5e1f
commit dc807af16e
2 changed files with 47 additions and 1 deletions

View File

@ -210,9 +210,10 @@ in {
bash = {
enable = true;
shellAliases = {
"l" = "echo -n ł | xclip -selection clipboard";
"l" = "echo -n ł | ${pkgs.xclip}/bin/xclip -selection clipboard";
"gp" = "${pkgs.git}/bin/git remote | ${pkgs.parallel}/bin/parallel --verbose git push";
};
initExtra = "source ${./gg.sh}";
};
}
)

45
shared/home/gg.sh Normal file
View File

@ -0,0 +1,45 @@
maxdepth=5
_gopath=$(git rev-parse --show-toplevel)
gg() {
paths=($(g "$@"))
path_index=0
if [ ${#paths[@]} -gt 1 ]; then
c=1
for path in "${paths[@]}"; do
echo [$c]: cd ${_gopath}/${path}
c=$((c+1))
done
echo -n "Go to which path: "
read path_index
path_index=$(($path_index-1))
fi
path=${paths[$path_index]}
cd $_gopath/$path
}
#
# Print the directories of the specified Go package name.
#
g() {
local pkg_candidates="$((cd $_gopath && find . -mindepth 1 -maxdepth ${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}'
}
#
# Bash autocomplete for g and gg functions.
#
_g_complete()
{
COMPREPLY=()
local cur
local prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=( $(compgen -W "$(for f in $(find "$_gopath" -mindepth 1 -maxdepth ${maxdepth} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur") )
return 0
}
complete -F _g_complete g
complete -F _g_complete gg