From f15b072ae159628fea805c2fbf58bf0c8694086d Mon Sep 17 00:00:00 2001 From: Giovanni Bassi Date: Sat, 1 Jun 2024 19:52:46 -0300 Subject: [PATCH] Move completions to nix --- aliases/custom.aliases.bash | 2 +- completions/000_completions.bash | 17 +- completions/001_dotnet-suggest.bash | 2 - completions/{015_go.bash => 001_go.bash} | 0 completions/002_dotnet.bash | 10 - .../{017_rustup.bash => 002_rustup.bash} | 0 .../{019_angular.bash => 003_angular.bash} | 0 completions/003_helm.bash | 2 - completions/004_hub.bash_completion.bash | 368 ------------------ completions/005_istio.bash | 2 - completions/006_k8s.bash | 4 - completions/007_kubectx.bash | 10 - completions/008_npm.bash | 4 - completions/010_gh.bash | 2 - completions/011_deno.bash | 2 - completions/012_node.bash | 2 - completions/014_terraform.bash | 4 - completions/016_k3d.bash | 2 - completions/018_just.bash | 2 - completions/020_aws.bash | 3 - completions/999_wrapup.bash | 7 - 21 files changed, 6 insertions(+), 439 deletions(-) delete mode 100755 completions/001_dotnet-suggest.bash rename completions/{015_go.bash => 001_go.bash} (100%) delete mode 100755 completions/002_dotnet.bash rename completions/{017_rustup.bash => 002_rustup.bash} (100%) rename completions/{019_angular.bash => 003_angular.bash} (100%) delete mode 100755 completions/003_helm.bash delete mode 100755 completions/004_hub.bash_completion.bash delete mode 100755 completions/005_istio.bash delete mode 100755 completions/006_k8s.bash delete mode 100755 completions/007_kubectx.bash delete mode 100755 completions/008_npm.bash delete mode 100755 completions/010_gh.bash delete mode 100755 completions/011_deno.bash delete mode 100755 completions/012_node.bash delete mode 100755 completions/014_terraform.bash delete mode 100755 completions/016_k3d.bash delete mode 100755 completions/018_just.bash delete mode 100755 completions/020_aws.bash diff --git a/aliases/custom.aliases.bash b/aliases/custom.aliases.bash index 66dd751..d2048e0 100755 --- a/aliases/custom.aliases.bash +++ b/aliases/custom.aliases.bash @@ -78,7 +78,7 @@ if hash istioctl 2>/dev/null; then complete -F _complete_alias istio fi if hash terraform 2>/dev/null; then - alias tf=terraform # completions for alias are on completions file for Terraform + alias tf=terraform fi if hash pygmentize 2>/dev/null; then alias ccat='pygmentize -g -O style=vs -f console16m' diff --git a/completions/000_completions.bash b/completions/000_completions.bash index 473f7a3..6123700 100755 --- a/completions/000_completions.bash +++ b/completions/000_completions.bash @@ -1,14 +1,3 @@ -# enable programmable completion features (you don't need to enable -# this, if it's already enabled in /etc/bash.bashrc and /etc/profile -# sources /etc/bash.bashrc). -if ! shopt -oq posix; then - if [ -f /usr/share/bash-completion/bash_completion ]; then - . /usr/share/bash-completion/bash_completion - elif [ -f /etc/bash_completion ]; then - . /etc/bash_completion - fi -fi - function addCompletion { if [ -f "$1" ]; then # shellcheck source=/dev/null @@ -17,7 +6,11 @@ function addCompletion { } updateCompletionsCommands='' -export COMPLETIONS_DIR=$HOME/.completions +if [ -v XDG_DATA_HOME ]; then + export COMPLETIONS_DIR=$XDG_DATA_HOME/bash-completion/completions/ +else + export COMPLETIONS_DIR=$HOME/.local/share/bash-completion/completions/ +fi function updateCompletions { echo Updating completions... mkdir -p "$COMPLETIONS_DIR" diff --git a/completions/001_dotnet-suggest.bash b/completions/001_dotnet-suggest.bash deleted file mode 100755 index 0caae2a..0000000 --- a/completions/001_dotnet-suggest.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/dotnet-suggest -updateCompletionsCommands="$updateCompletionsCommands\nif hash dotnet-suggest 2>/dev/null; then dotnet-suggest script Bash | sed 's/\r$//' > $COMPLETIONS_DIR/dotnet-suggest; fi" diff --git a/completions/015_go.bash b/completions/001_go.bash similarity index 100% rename from completions/015_go.bash rename to completions/001_go.bash diff --git a/completions/002_dotnet.bash b/completions/002_dotnet.bash deleted file mode 100755 index 5ebac47..0000000 --- a/completions/002_dotnet.bash +++ /dev/null @@ -1,10 +0,0 @@ -# shellcheck disable=all # script is from external sources -if hash dotnet 2>/dev/null; then - _dotnet_bash_complete() { - local word=${COMP_WORDS[COMP_CWORD]} - local completions=("$(dotnet complete --position ${COMP_POINT} "${COMP_LINE}")") - - COMPREPLY=( $(compgen -W "$completions" -- "$word") ) - } - complete -f -F _dotnet_bash_complete dotnet -fi diff --git a/completions/017_rustup.bash b/completions/002_rustup.bash similarity index 100% rename from completions/017_rustup.bash rename to completions/002_rustup.bash diff --git a/completions/019_angular.bash b/completions/003_angular.bash similarity index 100% rename from completions/019_angular.bash rename to completions/003_angular.bash diff --git a/completions/003_helm.bash b/completions/003_helm.bash deleted file mode 100755 index 62d51fd..0000000 --- a/completions/003_helm.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/helm -updateCompletionsCommands="$updateCompletionsCommands\nif hash helm 2>/dev/null; then helm completion bash > $COMPLETIONS_DIR/helm;fi" diff --git a/completions/004_hub.bash_completion.bash b/completions/004_hub.bash_completion.bash deleted file mode 100755 index c95859c..0000000 --- a/completions/004_hub.bash_completion.bash +++ /dev/null @@ -1,368 +0,0 @@ -# shellcheck disable=all -# hub tab-completion script for bash. -# This script complements the completion script that ships with git. - -# If there is no git tab completion, but we have the _completion loader try to load it -if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then - _completion_loader git -fi - -# Check that git tab completion is available -if declare -F _git > /dev/null; then - # Duplicate and rename the 'list_all_commands' function - eval "$(declare -f __git_list_all_commands | \ - sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')" - - # Wrap the 'list_all_commands' function with extra hub commands - __git_list_all_commands() { - cat <<-EOF -alias -pull-request -fork -create -browse -compare -ci-status -EOF - __git_list_all_commands_without_hub - } - - # Ensure cached commands are cleared - __git_all_commands="" - - ########################## - # hub command completions - ########################## - - # hub alias [-s] [SHELL] - _git_alias() { - local i c=2 s=-s sh shells="bash zsh sh ksh csh fish" - while [ $c -lt $cword ]; do - i="${words[c]}" - case "$i" in - -s) - unset s - ;; - *) - for sh in $shells; do - if [ "$sh" = "$i" ]; then - unset shells - break - fi - done - ;; - esac - ((c++)) - done - __gitcomp "$s $shells" - } - - # hub browse [-u] [--|[USER/]REPOSITORY] [SUBPAGE] - _git_browse() { - local i c=2 u=-u repo subpage - local subpages_="commits issues tree wiki pulls branches stargazers - contributors network network/ graphs graphs/" - local subpages_network="members" - local subpages_graphs="commit-activity code-frequency punch-card" - while [ $c -lt $cword ]; do - i="${words[c]}" - case "$i" in - -u) - unset u - ;; - *) - if [ -z "$repo" ]; then - repo=$i - else - subpage=$i - fi - ;; - esac - ((c++)) - done - if [ -z "$repo" ]; then - __gitcomp "$u -- $(__hub_github_repos '\p')" - elif [ -z "$subpage" ]; then - case "$cur" in - */*) - local pfx="${cur%/*}" cur_="${cur#*/}" - local subpages_var="subpages_$pfx" - __gitcomp "${!subpages_var}" "$pfx/" "$cur_" - ;; - *) - __gitcomp "$u ${subpages_}" - ;; - esac - else - __gitcomp "$u" - fi - } - - # hub compare [-u] [USER[/REPOSITORY]] [[START...]END] - _git_compare() { - local i c=$((cword - 1)) u=-u user remote owner repo arg_repo rev - while [ $c -gt 1 ]; do - i="${words[c]}" - case "$i" in - -u) - unset u - ;; - *) - if [ -z "$rev" ]; then - # Even though the logic below is able to complete both user/repo - # and revision in the right place, when there is only one argument - # (other than -u) in the command, that argument will be taken as - # revision. For example: - # $ hub compare -u upstream - # > https://github.com/USER/REPO/compare/upstream - if __hub_github_repos '\p' | grep -Eqx "^$i(/[^/]+)?"; then - arg_repo=$i - else - rev=$i - fi - elif [ -z "$arg_repo" ]; then - arg_repo=$i - fi - ;; - esac - ((c--)) - done - - # Here we want to find out the git remote name of user/repo, in order to - # generate an appropriate revision list - if [ -z "$arg_repo" ]; then - user=$(__hub_github_user) - if [ -z "$user" ]; then - for i in $(__hub_github_repos); do - remote=${i%%:*} - repo=${i#*:} - if [ "$remote" = origin ]; then - break - fi - done - else - for i in $(__hub_github_repos); do - remote=${i%%:*} - repo=${i#*:} - owner=${repo%%/*} - if [ "$user" = "$owner" ]; then - break - fi - done - fi - else - for i in $(__hub_github_repos); do - remote=${i%%:*} - repo=${i#*:} - owner=${repo%%/*} - case "$arg_repo" in - "$repo"|"$owner") - break - ;; - esac - done - fi - - local pfx cur_="$cur" - case "$cur_" in - *..*) - pfx="${cur_%%..*}..." - cur_="${cur_##*..}" - __gitcomp_nl "$(__hub_revlist $remote)" "$pfx" "$cur_" - ;; - *) - if [ -z "${arg_repo}${rev}" ]; then - __gitcomp "$u $(__hub_github_repos '\o\n\p') $(__hub_revlist $remote)" - elif [ -z "$rev" ]; then - __gitcomp "$u $(__hub_revlist $remote)" - else - __gitcomp "$u" - fi - ;; - esac - } - - # hub create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE] - _git_create() { - local i c=2 name repo flags="-p -d -h" - while [ $c -lt $cword ]; do - i="${words[c]}" - case "$i" in - -d|-h) - ((c++)) - flags=${flags/$i/} - ;; - -p) - flags=${flags/$i/} - ;; - *) - name=$i - ;; - esac - ((c++)) - done - if [ -z "$name" ]; then - repo=$(basename "$(pwd)") - fi - case "$prev" in - -d|-h) - COMPREPLY=() - ;; - -p|*) - __gitcomp "$repo $flags" - ;; - esac - } - - # hub fork [--no-remote] - _git_fork() { - local i c=2 remote=yes - while [ $c -lt $cword ]; do - i="${words[c]}" - case "$i" in - --no-remote) - unset remote - ;; - esac - ((c++)) - done - if [ -n "$remote" ]; then - __gitcomp "--no-remote" - fi - } - - # hub pull-request [-f] [-m |-F |-i |] [-b ] [-h ] - _git_pull_request() { - local i c=2 flags="-f -m -F -i -b -h" - while [ $c -lt $cword ]; do - i="${words[c]}" - case "$i" in - -m|-F|-i|-b|-h) - ((c++)) - flags=${flags/$i/} - ;; - -f) - flags=${flags/$i/} - ;; - esac - ((c++)) - done - case "$prev" in - -i) - COMPREPLY=() - ;; - -b|-h) - # (Doesn't seem to need this...) - # Uncomment the following line when 'owner/repo:[TAB]' misbehaved - #_get_comp_words_by_ref -n : cur - __gitcomp_nl "$(__hub_heads)" - # __ltrim_colon_completions "$cur" - ;; - -F) - COMPREPLY=( "$cur"* ) - ;; - -f|*) - __gitcomp "$flags" - ;; - esac - } - - ################### - # Helper functions - ################### - - # __hub_github_user [HOST] - # Return $GITHUB_USER or the default github user defined in hub config - # HOST - Host to be looked-up in hub config. Default is "github.com" - __hub_github_user() { - if [ -n "$GITHUB_USER" ]; then - echo $GITHUB_USER - return - fi - local line h k v host=${1:-github.com} config=${HUB_CONFIG:-~/.config/hub} - if [ -f "$config" ]; then - while read line; do - if [ "$line" = "---" ]; then - continue - fi - k=${line%%:*} - v=${line#*:} - if [ -z "$v" ]; then - if [ "$h" = "$host" ]; then - break - fi - h=$k - continue - fi - k=${k#* } - v=${v#* } - if [ "$h" = "$host" ] && [ "$k" = "user" ]; then - echo "$v" - break - fi - done < "$config" - fi - } - - # __hub_github_repos [FORMAT] - # List all github hosted repository - # FORMAT - Format string contains multiple of these: - # \m remote - # \p owner/repo - # \o owner - # escaped characters (\n, \t ...etc) work - # If omitted, prints all github repos in the format of "remote:owner/repo" - __hub_github_repos() { - local f format=$1 - if [ -z "$(__gitdir)" ]; then - return - fi - if [ -z "$format" ]; then - format='\1:\2' - else - format=${format//\m/\1} - format=${format//\p/\2} - format=${format//\o/\3} - fi - command git config --get-regexp 'remote\.[^.]*\.url' | - grep -E ' ((https?|git)://|git@)github\.com[:/][^:/]+/[^/]+$' | - sed -E 's#^remote\.([^.]+)\.url +.+[:/](([^/]+)/[^.]+)(\.git)?$#'"$format"'#' - } - - # __hub_heads - # List all local "branch", and remote "owner/repo:branch" - __hub_heads() { - local i remote repo branch dir=$(__gitdir) - if [ -d "$dir" ]; then - command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \ - "refs/heads/" - for i in $(__hub_github_repos); do - remote=${i%%:*} - repo=${i#*:} - command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \ - "refs/remotes/${remote}/" | while read branch; do - echo "${repo}:${branch#${remote}/}" - done - done - fi - } - - # __hub_revlist [REMOTE] - # List all tags, and branches under REMOTE, without the "remote/" prefix - # REMOTE - Remote name to search branches from. Default is "origin" - __hub_revlist() { - local i remote=${1:-origin} dir=$(__gitdir) - if [ -d "$dir" ]; then - command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \ - "refs/remotes/${remote}/" | while read i; do - echo "${i#${remote}/}" - done - command git --git-dir="$dir" for-each-ref --format='%(refname:short)' \ - "refs/tags/" - fi - } - - # Enable completion for hub even when not using the alias - complete -o bashdefault -o default -o nospace -F _git hub 2>/dev/null \ - || complete -o default -o nospace -F _git hub -fi diff --git a/completions/005_istio.bash b/completions/005_istio.bash deleted file mode 100755 index 3c2d8d9..0000000 --- a/completions/005_istio.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/istioctl.bash -updateCompletionsCommands="$updateCompletionsCommands\nif hash istioctl 2>/dev/null; then istioctl collateral completion --bash -o $COMPLETIONS_DIR; fi" diff --git a/completions/006_k8s.bash b/completions/006_k8s.bash deleted file mode 100755 index 646d26b..0000000 --- a/completions/006_k8s.bash +++ /dev/null @@ -1,4 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/kubectl -addCompletion "$COMPLETIONS_DIR"/kubecolor -updateCompletionsCommands="$updateCompletionsCommands\nif hash kubectl 2>/dev/null; then kubectl completion bash > $COMPLETIONS_DIR/kubectl; fi" -updateCompletionsCommands="$updateCompletionsCommands\nif hash kubecolor 2>/dev/null; then kubectl completion bash | sed 's/kubectl/kubecolor/g' > $COMPLETIONS_DIR/kubecolor; fi" diff --git a/completions/007_kubectx.bash b/completions/007_kubectx.bash deleted file mode 100755 index 68ec3cf..0000000 --- a/completions/007_kubectx.bash +++ /dev/null @@ -1,10 +0,0 @@ -if hash kubectx 2>/dev/null; then - KUBECTX_DIR="$(dirname "$(realpath "$(which kubectx)")")" - # shellcheck source=/dev/null - source "$KUBECTX_DIR/completion/kubectx.bash" -fi -if hash kubens 2>/dev/null; then - KUBENS_DIR="$(dirname "$(realpath "$(which kubens)")")" - # shellcheck source=/dev/null - source "$KUBENS_DIR/completion/kubens.bash" -fi diff --git a/completions/008_npm.bash b/completions/008_npm.bash deleted file mode 100755 index 3989e19..0000000 --- a/completions/008_npm.bash +++ /dev/null @@ -1,4 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/npm -if hasBinaryInLinux npm; then - updateCompletionsCommands="$updateCompletionsCommands\nnpm completion > $COMPLETIONS_DIR/npm" -fi diff --git a/completions/010_gh.bash b/completions/010_gh.bash deleted file mode 100755 index 43f78f4..0000000 --- a/completions/010_gh.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/gh -updateCompletionsCommands="$updateCompletionsCommands\nif hash helm 2>/dev/null; then gh completion -s bash > $COMPLETIONS_DIR/gh;fi" diff --git a/completions/011_deno.bash b/completions/011_deno.bash deleted file mode 100755 index 523be12..0000000 --- a/completions/011_deno.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/deno -updateCompletionsCommands="$updateCompletionsCommands\nif hash deno 2>/dev/null; then deno completions bash > $COMPLETIONS_DIR/deno; fi" diff --git a/completions/012_node.bash b/completions/012_node.bash deleted file mode 100755 index e3b9216..0000000 --- a/completions/012_node.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/node -updateCompletionsCommands="$updateCompletionsCommands\nif hash node 2>/dev/null; then node --completion-bash > $COMPLETIONS_DIR/node; fi" diff --git a/completions/014_terraform.bash b/completions/014_terraform.bash deleted file mode 100755 index 71a8a26..0000000 --- a/completions/014_terraform.bash +++ /dev/null @@ -1,4 +0,0 @@ -if hash terraform 2>/dev/null; then - complete -C "`which terraform`" terraform - complete -C "`which terraform`" tf -fi diff --git a/completions/016_k3d.bash b/completions/016_k3d.bash deleted file mode 100755 index c90794c..0000000 --- a/completions/016_k3d.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/k3d -updateCompletionsCommands="$updateCompletionsCommands\nif hash k3d 2>/dev/null; then k3d completion bash > $COMPLETIONS_DIR/k3d; fi" diff --git a/completions/018_just.bash b/completions/018_just.bash deleted file mode 100755 index 56dc5f7..0000000 --- a/completions/018_just.bash +++ /dev/null @@ -1,2 +0,0 @@ -addCompletion "$COMPLETIONS_DIR"/just -updateCompletionsCommands="$updateCompletionsCommands\nif hash just 2>/dev/null; then just --completions bash > $COMPLETIONS_DIR/just; fi" diff --git a/completions/020_aws.bash b/completions/020_aws.bash deleted file mode 100755 index 0facc7f..0000000 --- a/completions/020_aws.bash +++ /dev/null @@ -1,3 +0,0 @@ -if hash aws 2>/dev/null; then - complete -C "`which aws_completer`" aws -fi diff --git a/completions/999_wrapup.bash b/completions/999_wrapup.bash index 19f984b..ffc3ccf 100755 --- a/completions/999_wrapup.bash +++ b/completions/999_wrapup.bash @@ -1,11 +1,4 @@ if ! [ -d "$COMPLETIONS_DIR" ]; then mkdir -p "$COMPLETIONS_DIR" updateCompletions - DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - for completionsFile in `echo "$DIR"/*.bash`; do - if [ -e "$completionsFile" ]; then - # shellcheck source=/dev/null - source "$completionsFile" - fi - done fi