Skip to content

Commit

Permalink
Merge pull request #2029 from gaelicWizard/completion/bash-it
Browse files Browse the repository at this point in the history
completion/bash-it: lint and simplify
  • Loading branch information
NoahGorny committed Jan 25, 2022
2 parents dbb3ea4 + 5f9a3f1 commit 457a279
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 206 deletions.
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ aliases/available/vim.aliases.bash
#
completion/available/apm.completion.bash
completion/available/awless.completion.bash
completion/available/bash-it.completion.bash
completion/available/brew.completion.bash
completion/available/cargo.completion.bash
completion/available/composer.completion.bash
Expand Down
248 changes: 84 additions & 164 deletions completion/available/bash-it.completion.bash
Original file line number Diff line number Diff line change
@@ -1,171 +1,91 @@
#!/usr/bin/env bash
# shellcheck shell=bash

_bash-it-comp-enable-disable()
{
local enable_disable_args="alias completion plugin"
COMPREPLY=( $(compgen -W "${enable_disable_args}" -- "${cur}") )
}

_bash-it-comp-list-available-not-enabled()
{
local subdirectory="$1"

local enabled_components all_things available_things

all_things=( $(compgen -G "${BASH_IT}/$subdirectory/available/*.bash") ); all_things=( "${all_things[@]##*/}" )
enabled_components=( $(command ls "${BASH_IT}"/{"$subdirectory"/,}enabled/*.bash 2>/dev/null) )
enabled_components=( "${enabled_components[@]##*/}" ); enabled_components="${enabled_components[@]##*---}"
available_things=( $(sort -d <(for i in ${enabled_components}
do
all_things=( "${all_things[@]//$i}" )
done
printf '%s\n' "${all_things[@]}")) ); available_things="${available_things[@]%.*.bash}"

COMPREPLY=( $(compgen -W "all ${available_things}" -- "${cur}") )
}

_bash-it-comp-list-enabled()
{
local subdirectory="$1"
local suffix enabled_things

suffix="${subdirectory/plugins/plugin}"

enabled_things=( $(sort -d <(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash") <(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash")) )
enabled_things=( "${enabled_things[@]##*/}" ); enabled_things=( "${enabled_things[@]##*---}" ); enabled_things="${enabled_things[@]%.*.bash}"

COMPREPLY=( $(compgen -W "all ${enabled_things}" -- "${cur}") )
}

_bash-it-comp-list-available()
{
local subdirectory="$1"

local enabled_things

enabled_things=( $(sort -d <(compgen -G "${BASH_IT}/$subdirectory/available/*.bash")) )
enabled_things=( "${enabled_things[@]##*/}" ); enabled_things="${enabled_things[@]%.*.bash}"

COMPREPLY=( $(compgen -W "${enabled_things}" -- "${cur}") )
}

_bash-it-comp-list-profiles()
{
local profiles

profiles=$(for f in `compgen -G "${BASH_IT}/profiles/*.bash_it" | sort -d`;
do
basename $f | sed -e 's/.bash_it//g'
done)
function _compreply_candidates() {
local IFS=$'\n'

COMPREPLY=( $(compgen -W "${profiles}" -- ${cur}) )
read -d '' -ra COMPREPLY < <(compgen -W "${candidates[*]}" -- "${cur}")
}

_bash-it-comp()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
chose_opt="${COMP_WORDS[1]}"
file_type="${COMP_WORDS[2]}"
opts="disable enable help migrate reload restart profile doctor search show update version"
case "${chose_opt}" in
show)
local show_args="aliases completions plugins"
COMPREPLY=( $(compgen -W "${show_args}" -- "${cur}") )
return 0
;;
help)
if [ x"${prev}" == x"aliases" ]; then
_bash-it-comp-list-available aliases
return 0
else
local help_args="aliases completions migrate plugins update"
COMPREPLY=( $(compgen -W "${help_args}" -- "${cur}") )
return 0
fi
;;
profile)
case "${file_type}" in
load)
if [[ "load" == "$prev" ]]; then
_bash-it-comp-list-profiles
fi
return 0
;;
rm)
if [[ "rm" == "$prev" ]]; then
_bash-it-comp-list-profiles
fi
return 0
;;
save)
return 0
;;
list)
return 0
;;
*)
local profile_args="load save list rm"
COMPREPLY=( $(compgen -W "${profile_args}" -- ${cur}) )
return 0
;;
esac
;;
doctor)
local doctor_args="errors warnings all"
COMPREPLY=( $(compgen -W "${doctor_args}" -- "${cur}") )
return 0
;;
update)
if [[ "${cur}" == -* ]];then
local update_args="-s --silent"
else
local update_args="stable dev"
fi
COMPREPLY=( $(compgen -W "${update_args}" -- "${cur}") )
return 0
;;
migrate | reload | restart | search | version)
return 0
;;
enable | disable)
if [ x"${chose_opt}" == x"enable" ];then
suffix="available-not-enabled"
else
suffix="enabled"
fi
case "${file_type}" in
alias)
_bash-it-comp-list-"${suffix}" aliases
return 0
;;
plugin)
_bash-it-comp-list-"${suffix}" plugins
return 0
;;
completion)
_bash-it-comp-list-"${suffix}" completion
return 0
;;
*)
_bash-it-comp-enable-disable
return 0
;;
esac
;;
esac

COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )

return 0
function _bash-it() {
local cur prev verb file_type candidates suffix
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD - 1]}"
verb="${COMP_WORDS[1]}"
file_type="${COMP_WORDS[2]:-}"
candidates=('disable' 'enable' 'help' 'migrate' 'reload' 'restart' 'profile' 'doctor' 'search' 'show' 'update' 'version')
case "${verb}" in
show)
candidates=('aliases' 'completions' 'plugins')
_compreply_candidates
;;
help)
if [[ "${prev}" == "aliases" ]]; then
candidates=('all' "$(_bash-it-component-list "${file_type}")")
_compreply_candidates
else
candidates=('aliases' 'completions' 'migrate' 'plugins' 'update')
_compreply_candidates
fi
;;
profile)
case "${file_type}" in
load | rm)
if [[ "${file_type}" == "$prev" ]]; then
candidates=("${BASH_IT}/profiles"/*.bash_it)
candidates=("${candidates[@]##*/}")
candidates=("${candidates[@]%%.bash_it}")

_compreply_candidates
fi
;;
save | list) ;;
*)
candidates=('load' 'save' 'list' 'rm')
_compreply_candidates
;;
esac
;;
doctor)
candidates=('errors' 'warnings' 'all')
_compreply_candidates
;;
update)
if [[ "${cur}" == -* ]]; then
candidates=('-s' '--silent')
else
candidates=('stable' 'dev')
fi
_compreply_candidates
;;
migrate | reload | restart | search | version) ;;
enable | disable)
if [[ "${verb}" == "enable" ]]; then
suffix="disabled"
else
suffix="enabled"
fi
case "${file_type}" in
alias | completion | plugin)
candidates=('all' "$("_bash-it-component-list-${suffix}" "${file_type}")")
_compreply_candidates
;;
*)
candidates=('alias' 'completion' 'plugin')
_compreply_candidates
;;
esac
;;
*)
_compreply_candidates
;;
esac
}

# Activate completion for bash-it and its common misspellings
complete -F _bash-it-comp bash-it
complete -F _bash-it-comp bash-ti
complete -F _bash-it-comp shit
complete -F _bash-it-comp bashit
complete -F _bash-it-comp batshit
complete -F _bash-it-comp bash_it
complete -F _bash-it bash-it
complete -F _bash-it bash-ti
complete -F _bash-it shit
complete -F _bash-it bashit
complete -F _bash-it batshit
complete -F _bash-it bash_it

0 comments on commit 457a279

Please sign in to comment.