Skip to content

Commit 8d5d907

Browse files
authored
Merge pull request #1999 from gaelicWizard/utilities
lib/utilities: refactor `_bash-it-pluralize-component()` & other improvements
2 parents bdf9a2b + 7430a06 commit 8d5d907

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

lib/utilities.bash

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ function _bash-it-get-component-type-from-path() {
4747
#
4848
#
4949
function _bash-it-array-contains-element() {
50-
local e
51-
for e in "${@:2}"; do
52-
[[ "$e" == "$1" ]] && return 0
50+
local e element="${1?}"
51+
shift
52+
for e in "$@"; do
53+
[[ "$e" == "${element}" ]] && return 0
5354
done
5455
return 1
5556
}
@@ -78,30 +79,46 @@ function _bash-it-egrep() {
7879

7980
function _bash-it-component-help() {
8081
local component file func
81-
component="$(_bash-it-pluralize-component "${1}")"
82-
file="$(_bash-it-component-cache-file "${component}")"
83-
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
84-
func="_bash-it-${component}"
85-
"${func}" | _bash-it-egrep ' \[' >| "${file}"
82+
_bash-it-component-pluralize "${1}" component
83+
_bash-it-component-cache-file "${component}" file
84+
if [[ ! -s "${file?}" || -z "$(find "${file}" -mmin -300)" ]]; then
85+
func="_bash-it-${component?}"
86+
"${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}"
8687
fi
8788
cat "${file}"
8889
}
8990

9091
function _bash-it-component-cache-file() {
91-
local component file
92-
component="$(_bash-it-pluralize-component "${1?${FUNCNAME[0]}: component name required}")"
93-
file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}"
94-
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
95-
printf '%s' "${file}"
92+
local _component_to_cache _file_path _result="${2:-${FUNCNAME[0]//-/_}}"
93+
_bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" _component_to_cache
94+
_file_path="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${_component_to_cache?}"
95+
[[ -f "${_file_path}" ]] || mkdir -p "${_file_path%/*}"
96+
printf -v "${_result?}" '%s' "${_file_path}"
97+
}
98+
99+
function _bash-it-component-singularize() {
100+
local _result="${2:-${FUNCNAME[0]//-/_}}"
101+
local _component_to_single="${1?${FUNCNAME[0]}: component name required}"
102+
local -i len="$((${#_component_to_single} - 2))"
103+
if [[ "${_component_to_single:${len}:2}" == 'ns' ]]; then
104+
_component_to_single="${_component_to_single%s}"
105+
elif [[ "${_component_to_single}" == "aliases" ]]; then
106+
_component_to_single="${_component_to_single%es}"
107+
fi
108+
printf -v "${_result?}" '%s' "${_component_to_single}"
96109
}
97110

98-
function _bash-it-pluralize-component() {
99-
local component="${1}"
100-
local -i len=$((${#component} - 1))
111+
function _bash-it-component-pluralize() {
112+
local _result="${2:-${FUNCNAME[0]//-/_}}"
113+
local _component_to_plural="${1?${FUNCNAME[0]}: component name required}"
114+
local -i len="$((${#_component_to_plural} - 1))"
101115
# pluralize component name for consistency
102-
[[ "${component:${len}:1}" != 's' ]] && component="${component}s"
103-
[[ "${component}" == "alias" ]] && component="aliases"
104-
printf '%s' "${component}"
116+
if [[ "${_component_to_plural:${len}:1}" != 's' ]]; then
117+
_component_to_plural="${_component_to_plural}s"
118+
elif [[ "${_component_to_plural}" == "alias" ]]; then
119+
_component_to_plural="${_component_to_plural}es"
120+
fi
121+
printf -v "${_result?}" '%s' "${_component_to_plural}"
105122
}
106123

107124
function _bash-it-clean-component-cache() {
@@ -113,7 +130,7 @@ function _bash-it-clean-component-cache() {
113130
_bash-it-clean-component-cache "${component}"
114131
done
115132
else
116-
cache="$(_bash-it-component-cache-file "${component}")"
133+
_bash-it-component-cache-file "${component}" cache
117134
if [[ -f "${cache}" ]]; then
118135
rm -f "${cache}"
119136
fi
@@ -144,29 +161,30 @@ function _bash-it-component-list-disabled() {
144161
}
145162

146163
# Checks if a given item is enabled for a particular component/file-type.
147-
# Uses the component cache if available.
148164
#
149165
# Returns:
150166
# 0 if an item of the component is enabled, 1 otherwise.
151167
#
152168
# Examples:
153169
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
154170
function _bash-it-component-item-is-enabled() {
155-
local component="$1"
156-
local item="$2"
157-
_bash-it-component-help "${component}" | _bash-it-egrep '\[x\]' | _bash-it-egrep -q -- "^${item}\s"
171+
local component="$1" item="$2"
172+
local each_file
173+
174+
for each_file in "${BASH_IT}/enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item}.${component}"*."bash" \
175+
"${BASH_IT}/${component}"*/"enabled/${item}.${component}"*."bash" \
176+
"${BASH_IT}/${component}"*/"enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item}.${component}"*."bash"; do
177+
[[ -f "${each_file}" ]] && return
178+
done
158179
}
159180

160181
# Checks if a given item is disabled for a particular component/file-type.
161-
# Uses the component cache if available.
162182
#
163183
# Returns:
164184
# 0 if an item of the component is enabled, 1 otherwise.
165185
#
166186
# Examples:
167187
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
168188
function _bash-it-component-item-is-disabled() {
169-
local component="$1"
170-
local item="$2"
171-
_bash-it-component-help "${component}" | _bash-it-egrep -v '\[x\]' | _bash-it-egrep -q -- "^${item}\s"
189+
! _bash-it-component-item-is-enabled "$@"
172190
}

0 commit comments

Comments
 (0)