Skip to content

Commit

Permalink
♻️ refactor: optimize git bash shell loading time when using completions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquindev committed Jan 11, 2025
1 parent d0cbda9 commit 8722753
Show file tree
Hide file tree
Showing 14 changed files with 9,073 additions and 63 deletions.
93 changes: 30 additions & 63 deletions config/bash/.bash_completions
Original file line number Diff line number Diff line change
@@ -1,71 +1,38 @@
#!/usr/bin/env bash

# shellcheck disable=SC1090,SC1091
# Builtin Git-Bash Completion
source /mingw64/share/git/completion/git-completion.bash

# __ltrim_colon_completions
if ! type __ltrim_colon_completions >/dev/null 2>&1; then
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
# word-to-complete.
# With a colon in COMP_WORDBREAKS, words containing
# colons are always completed as entire words if the word to complete contains
# a colon. This function fixes this, by removing the colon-containing-prefix
# from COMPREPLY items.
# The preferred solution is to remove the colon (:) from COMP_WORDBREAKS in
# your .bashrc:
#
# # Remove colon (:) from list of word completion separators
# COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
#
# See also: Bash FAQ - E13) Why does filename completion misbehave if a colon
# appears in the filename? - http://tiswww.case.edu/php/chet/bash/FAQ
# @param $1 current word to complete (cur)
# @modifies global array $COMPREPLY
#
__ltrim_colon_completions() {
if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
# Remove colon-word prefix from COMPREPLY items
local colon_word=${1%"${1##*:}"}
local i=${#COMPREPLY[*]}
while [[ $((--i)) -ge 0 ]]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
done
fi
}
fi
# Builtin Git Bash completion
source /mingw64/share/git/completion/git-completion.bash
if type __git_complete >/dev/null 2>&1; then __git_complete g git; fi

# Custom Locations for additional completion files
# Custom locations for additional completion files
CUSTOM_COMPLETIONS="$DOTFILES/config/bash/custom/completions"

shopt -s extglob
for arg in $(compgen -ac | sort -u); do
case "$arg" in
__git_complete*) __git_complete g git ;;
faas-cli?(.exe)) source <(faas-cli completion --shell bash) ;;
oh-my-posh?(.exe)) source <(oh-my-posh completion bash) ;;
gh?(.exe)) eval "$(gh completion -s bash)" ;;
git-lfs?(.exe)) source <(git lfs completion bash) ;;
gitleaks?(.exe)) source <(gitleaks completion bash) ;;
glow?(.exe)) source <(glow completion bash) ;;
gum?(.exe)) source <(gum completion bash) ;;
ssh?(.exe)?*) . "$CUSTOM_COMPLETIONS/ssh.bash" ;;
dotnet?(.exe)) . "$CUSTOM_COMPLETIONS/dotnet.bash" ;;
docker?(.exe)) source <(docker completion bash) ;;
hugo?(.exe)) source <(hugo completion bash) ;;
kubectl?(.exe)) source <(kubectl completion bash) ;;
@(bun)) source "$CUSTOM_COMPLETIONS/bun.bash" ;;
@(npm)) source <(npm completion) ;;
@(pnpm)) source <(pnpm completion bash) ;;
@(yarn)) . "$CUSTOM_COMPLETIONS/yarn.bash" ;;
pip?(3*)) . "$CUSTOM_COMPLETIONS/pip.bash" ;;
pipenv*) eval "$(_PIPENV_COMPLETE=bash_source pipenv)" ;;
pipx?(.exe)) eval "$(register-python-argcomplete pipx)" ;;
pdm?(.exe)) source <(pdm completion bash) ;;
terraform?(.exe)) complete -C "$(which terraform)" terraform ;;
uv?(x)?(.exe)) source <(uv generate-shell-completion bash) ;;
yq?(.exe)) source <(yq completion bash) ;;
[Vv][Bb]ox[Mm]anage?(.exe)) . "$CUSTOM_COMPLETIONS/vboxmanage.bash" ;;
# vagrant?(.exe)) . "/c/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.3/contrib/bash/completion.sh" ;;
esac
for file in "$CUSTOM_COMPLETIONS"/*.bash; do
filename=$(basename "$file")
if command -v "${filename%.*}" >/dev/null 2>&1; then
. "$file"
fi
done

# Vagrant
if type vagrant >/dev/null 2>&1; then
vagrant_complete=$(find "/c/Program Files/Vagrant" -type f | grep completion.sh)
if [ -f "$vagrant_complete" ]; then . "$vagrant_complete"; fi
fi

# Terraform
if type terraform >/dev/null 2>&1; then
complete -C "$(which terraform)" terraform
fi

# pipenv
if type pipenv >/dev/null 2>&1; then
eval "$(_PIPENV_COMPLETE=bash_source pipenv)"
fi

# pipx
if type pipx >/dev/null 2>&1; then
eval "$(register-python-argcomplete pipx)"
fi
Loading

0 comments on commit 8722753

Please sign in to comment.