#!/usr/bin/env bash set -eo pipefail declare -r normal='\e[0m' declare -r bold='\e[1m' declare -r mute='\e[2m' declare -r italic='\e[3m' declare -r underline='\e[4m' declare -r red='\e[31m' declare -r green='\e[32m' declare -r yellow='\e[33m' declare -r blue='\e[34m' declare -r magenta='\e[35m' declare -r cyan='\e[36m' function log() { >&2 command echo "${@}" } function log.info() { log -e "${cyan}${mute}[info]${normal} ${@}" } function log.warn() { log -e "${yellow}${mute}[warn]${normal} ${@}" } function log.error() { log -e "${red}${mute}[error]${normal} ${@}" } declare -A default_completion_dir default_completion_dir[bash]="${XDG_DATA_HOME:-"${HOME}/.local/share"}/bash-completion/completions" default_completion_dir[fish]="${HOME}/.config/fish/completions" default_completion_dir[zsh]="${XDG_DATA_HOME:-"${HOME}/.local/share"}/zsh/completions" declare -A completion_generator_command_template completion_generator_command_template[alacritty:bash]="curl -fsSL https://raw.githubusercontent.com/alacritty/alacritty/master/extra/completions/{bin}.{shell}" completion_generator_command_template[alacritty]="curl -fsSL https://raw.githubusercontent.com/alacritty/alacritty/master/extra/completions/{filename}" completion_generator_command_template[apt-fast]="curl -fsSL https://raw.githubusercontent.com/ilikenwf/apt-fast/master/completions/{shell}/{filename}" completion_generator_command_template[atuin]="{bin} gen-completions --shell {shell}" completion_generator_command_template[bw]="{bin} completion --shell {shell}" completion_generator_command_template[cargo]="rustup completions {shell} {bin}" completion_generator_command_template[chezmoi]="{bin} completion {shell}" completion_generator_command_template[delta]="curl -fsSL https://raw.githubusercontent.com/dandavison/delta/master/etc/completion/completion.{shell}" completion_generator_command_template[deno]="{bin} completions {shell}" completion_generator_command_template[docker-compose]="curl -fsSL https://raw.githubusercontent.com/docker/compose/master/contrib/completion/{shell}/{filename}" completion_generator_command_template[docker]="{bin} completion {shell}" completion_generator_command_template[earthly]="{bin} bootstrap --no-buildkit --source {shell}" completion_generator_command_template[ente]="{bin} completion {shell}" completion_generator_command_template[exa]="curl -fsSL https://raw.githubusercontent.com/ogham/exa/master/completions/{shell}/{filename}" completion_generator_command_template[exercism]="curl -fsSL https://raw.githubusercontent.com/exercism/cli/main/shell/{bin}_completion.{shell}" completion_generator_command_template[exercism:fish]="curl -fsSL https://raw.githubusercontent.com/exercism/cli/main/shell/{bin}.{shell}" completion_generator_command_template[eza]="curl -fsSL https://raw.githubusercontent.com/eza-community/eza/main/completions/{shell}/{filename}" completion_generator_command_template[fnm]="{bin} completions --shell {shell}" completion_generator_command_template[gh]="{bin} completion --shell {shell}" completion_generator_command_template[gopass]="{bin} completion {shell}" completion_generator_command_template[hishtory]="{bin} completion {shell}" completion_generator_command_template[kubectl]="{bin} completion {shell}" completion_generator_command_template[lf]="curl -fsSL https://raw.githubusercontent.com/gokcehan/lf/master/etc/{bin}.{shell}" completion_generator_command_template[luver]="{bin} completion {shell}" completion_generator_command_template[nc-docker:zsh]="cat ${DOTFILES}/.scripts.sh/completions/{filename}" completion_generator_command_template[ncd:zsh]="{bin} completion {shell}" completion_generator_command_template[netlify]="{bin} completion:generate --shell {shell}" completion_generator_command_template[nvm:bash]="curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/master/{shell}_completion" completion_generator_command_template[pnpm]="{bin} completion {shell}" completion_generator_command_template[poetry]="{bin} completions {shell}" completion_generator_command_template[pyenv]="curl -fsSL https://raw.githubusercontent.com/pyenv/pyenv/master/completions/{bin}.{shell}" completion_generator_command_template[rbenv:bash]="curl -fsSL https://raw.githubusercontent.com/rbenv/rbenv/master/completions/{bin}.{shell}" completion_generator_command_template[rbenv:zsh]="curl -fsSL https://raw.githubusercontent.com/rbenv/rbenv/master/completions/{filename}" completion_generator_command_template[rustup]="{bin} completions {shell}" completion_generator_command_template[rye]="{bin} self completion -s {shell}" completion_generator_command_template[snap]="curl -fsSL https://raw.githubusercontent.com/snapcore/snapd/master/data/completion/{shell}/{filename}" completion_generator_command_template[starship]="{bin} completions {shell}" completion_generator_command_template[stripe]="{bin} completion --shell {shell} --write-to-stdout" completion_generator_command_template[volta]="{bin} completions {shell}" completion_generator_command_template[warp-cli]="{bin} generate-completions {shell}" declare -a valid_shells=("bash" "fish" "zsh") is-valid-shell() { local -r shell="${1}" printf '%s\n' "${valid_shells[@]}" | grep -q -e "^${shell}$" } declare -a valid_tools=() for tool in "${!completion_generator_command_template[@]}"; do valid_tools+=("${tool%%:*}") done valid_tools=($(printf '%s\n' "${valid_tools[@]}" | sort | uniq)) is-valid-tool() { local -r tool="${1}" printf '%s\n' "${valid_tools[@]}" | grep -q -e "^${tool}$" } declare shell="$(basename "$(echo ${SHELL})")" show-help() { log "Usage:" log " generate-completion [OPTIONS] [TOOLS..]" log log "OPTIONS" log " --all generate for all available tools" log " --dir completion script directory" log " (default: ${default_completion_dir[${shell}]})" log " --debug debug mode" log " --shell name of the shell" log " (default: ${shell})" log " -y, --yes non-interactive mode" log " -h, --help print help" log log "TOOLS" for tool in ${valid_tools[@]}; do log " - ${tool}" done log log "SHELL" for shell in ${valid_shells[@]}; do log " - ${shell}" done } completion_dir="" interactive="yes" tools=() while [[ $# -gt 0 ]]; do case $1 in --all) tools=(${valid_tools[@]}) shift;; --dir) completion_dir="${2}" shift 2;; --debug) set -x shift;; -h|--help) show-help exit 0 shift;; --shell) if ! is-valid-shell "${2}"; then log.error "unsupported shell: ${2}!" exit 1 fi shell="${2}" shift 2;; -y|--yes) interactive="no" shift;; -*) shift;; *) if ! is-valid-tool "${1}"; then log.error "unsupported tool: ${1}!" exit 1 fi tools+=("${1}") shift;; esac done if [[ ${#tools[@]} -eq 0 ]]; then show-help exit 1 fi if [[ ${shell} = "fish" ]] && [[ "${interactive}" = "yes" ]]; then log.warn "${shell} support is experimental!" read -rp "do you want to continue? (yN): " proceed if [[ ${proceed} != "y" ]]; then exit 0 fi log fi if [[ $interactive = "yes" ]]; then read -rp "${shell} completion dir (default: ${default_completion_dir[${shell}]}): " completion_dir completion_dir="${completion_dir:-"${default_completion_dir[${shell}]}"}" log elif [[ $interactive = "no" ]]; then completion_dir="${completion_dir:-"${default_completion_dir[${shell}]}"}" fi if [[ ! -d ${completion_dir} ]]; then mkdir -p "${completion_dir}" fi log.info "COMPLETION_DIR=${green}${completion_dir}${normal}" for bin in ${tools[@]}; do log declare filename="${bin}" if [[ ${shell} = "fish" ]]; then filename="${filename}.fish" fi if [[ ${shell} = "zsh" ]]; then filename="_${filename}" fi # check shell specific command first declare generator_command="${completion_generator_command_template[${bin}:${shell}]}" # fallback to generic command generator_command="${generator_command:-${completion_generator_command_template[${bin}]}}" generator_command="$(echo "${generator_command}" | sed "s/{shell}/${shell}/g")" generator_command="$(echo "${generator_command}" | sed "s/{bin}/${bin}/g")" generator_command="$(echo "${generator_command}" | sed "s/{filename}/${filename}/g")" log.info "${bold}${italic}${magenta}${bin}${normal}" if [[ -z $generator_command ]]; then log.warn "missing generator command for ${shell}!" continue fi if [[ $generator_command == "${bin} "* ]]; then if ! type ${bin} >/dev/null 2>&1; then log.warn "skipped! binary not found!" continue fi fi declare content="$(eval "${generator_command}")" if test -n "${content}"; then echo "${content}" > "${completion_dir}/${filename}" log.info "completion script stored at: ${green}\${COMPLETION_DIR}/${filename}${normal}" else log.warn "failed!" fi done if [[ ${shell} = "zsh" ]]; then log log "----------------------------------------" log log "If you use zed, run the following:" log log " rm -f \"\${ZED[ZCOMPDUMP_PATH]}\"" log fi