Skip to content

Commit

Permalink
lib/utilities: clean up _bash-it-component*()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelicWizard committed Feb 18, 2022
1 parent b028ac1 commit e822d44
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions lib/utilities.bash
Original file line number Diff line number Diff line change
Expand Up @@ -139,47 +139,58 @@ function _bash-it-clean-component-cache() {

# Returns an array of items within each compoenent.
function _bash-it-component-list() {
local all component_dir
local IFS=$' \t\n' # Weirds in Bash 3.2...
local component components
_bash-it-component-pluralize "${1?}" component

_bash-it-component-pluralize "$1" pluralized_component
component_dir="${pluralized_component/completions/completion}"
all=("$BASH_IT/$component_dir/available/"*.bash); all=("${all[@]##*/}"); all=("${all[@]%.*.bash}")
builtin printf '%s\n' "${all[@]}"
component="${component/completions/completion}"
components=("${BASH_IT}/${component}/available"/*.bash)
components=("${components[@]##*/}")

component="${component/plugins/plugin}"
components=("${components[@]%".${component}.bash"}")

printf '%s\n' "${components[@]}"
}

function _bash-it-component-list-matching() {
local component="$1"
shift
local term="$1"
local component="${1?}" term="${2?}"
_bash-it-component-list "${component}" | _bash-it-egrep -- "${term}"
}

function _bash-it-component-list-enabled() {
local component="${1/alias/aliases}" enabled
local IFS=$' \t\n' # Weirds in Bash 3.2...
local component components=()
_bash-it-component-pluralize "${1?}" component

_bash-it-component-pluralize "$1" pluralized_component
component_dir="${pluralized_component/completions/completion}"
[[ -d "$BASH_IT/$component_dir/enabled" ]] &&
enabled=($(compgen -G "$BASH_IT/$component_dir/enabled/*.$component.bash")) ||
enabled=($(compgen -G "$BASH_IT/enabled/*.$component.bash"))
enabled=("${enabled[@]##*---}"); enabled=("${enabled[@]##*/}"); enabled=("${enabled[@]%.*.bash}")
component="${component/completions/completion}"
if [[ -d "${BASH_IT}/${component}/enabled" ]]; then
components=("$BASH_IT/${component}/enabled"/*."${component//plugins/plugin}.bash")
fi
component="${component/plugins/plugin}"
components+=("${BASH_IT}/enabled"/*."${component}.bash")

components=("${components[@]##*/}")
components=("${components[@]##*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}"}")
components=("${components[@]%."${component}.bash"}")

builtin printf '%s\n' "${enabled[@]}"
printf '%s\n' "${components[@]}"
}

function _bash-it-component-list-disabled() {
local all component disabled enabled i
local IFS=$' \t\n' # Weirds in Bash 3.2...
local components enabled i

_bash-it-component-pluralize "$1" pluralized_component
component="${pluralized_component/completions/completion}"
disabled=($(compgen -G "$BASH_IT/$component/available/*.bash")); disabled=("${disabled[@]##*/}"); disabled=("${disabled[@]%.*.bash}")
enabled=($(_bash-it-component-list-enabled "$1"))
IFS=$'\n' read -d '' -ra components < <(_bash-it-component-list "${1?}")
IFS=$'\n' read -d '' -ra enabled < <(_bash-it-component-list-enabled "${1?}")

for i in "${enabled[@]}"; do
disabled=("${disabled[@]//"$i"}")
done
for i in "${!components[@]}"; do
if _bash-it-array-contains-element "${components[i]}" "${enabled[@]}"; then
unset 'components[i]'
fi
done

builtin printf '%s\n' "${disabled[@]}"
printf '%s\n' "${components[@]}"
}

# Checks if a given item is enabled for a particular component/file-type.
Expand Down

0 comments on commit e822d44

Please sign in to comment.