@@ -47,9 +47,10 @@ function _bash-it-get-component-type-from-path() {
47
47
#
48
48
#
49
49
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
53
54
done
54
55
return 1
55
56
}
@@ -78,30 +79,46 @@ function _bash-it-egrep() {
78
79
79
80
function _bash-it-component-help() {
80
81
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} "
86
87
fi
87
88
cat " ${file} "
88
89
}
89
90
90
91
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} "
96
109
}
97
110
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 )) "
101
115
# 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} "
105
122
}
106
123
107
124
function _bash-it-clean-component-cache() {
@@ -113,7 +130,7 @@ function _bash-it-clean-component-cache() {
113
130
_bash-it-clean-component-cache " ${component} "
114
131
done
115
132
else
116
- cache= " $( _bash-it-component-cache-file " ${component} " ) "
133
+ _bash-it-component-cache-file " ${component} " cache
117
134
if [[ -f " ${cache} " ]]; then
118
135
rm -f " ${cache} "
119
136
fi
@@ -144,29 +161,30 @@ function _bash-it-component-list-disabled() {
144
161
}
145
162
146
163
# Checks if a given item is enabled for a particular component/file-type.
147
- # Uses the component cache if available.
148
164
#
149
165
# Returns:
150
166
# 0 if an item of the component is enabled, 1 otherwise.
151
167
#
152
168
# Examples:
153
169
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
154
170
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
158
179
}
159
180
160
181
# Checks if a given item is disabled for a particular component/file-type.
161
- # Uses the component cache if available.
162
182
#
163
183
# Returns:
164
184
# 0 if an item of the component is enabled, 1 otherwise.
165
185
#
166
186
# Examples:
167
187
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
168
188
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 " $@ "
172
190
}
0 commit comments