Something went wrong. Try again.
Mirroring: https://github.com/MunifTanjim/scripts.sh
Something went wrong. Try again.
scripts.sh generate-completion
9.2 kB · 249 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250#!/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_dirdefault_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_templatecompletion_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%%:*}")donevalid_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 <DIR> completion script directory" log " (default: ${default_completion_dir[${shell}]})" log " --debug debug mode" log " --shell <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;; esacdone
if [[ ${#tools[@]} -eq 0 ]]; then show-help exit 1fi
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 logfi
if [[ $interactive = "yes" ]]; then read -rp "${shell} completion dir (default: ${default_completion_dir[${shell}]}): " completion_dir completion_dir="${completion_dir:-"${default_completion_dir[${shell}]}"}" logelif [[ $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!" fidone
if [[ ${shell} = "zsh" ]]; then log log "----------------------------------------" log log "If you use zed, run the following:" log log " rm -f \"\${ZED[ZCOMPDUMP_PATH]}\"" logfi