1
- #! /usr/bin/env bash
1
+ # shellcheck shell= bash
2
2
#
3
3
# A collection of reusable functions.
4
4
5
5
# ##########################################################################
6
6
# Generic utilies
7
7
# ##########################################################################
8
8
9
- _bash-it-get-component-name-from-path () {
10
- # filename without path
11
- filename=${1##*/ }
12
- # filename without path or priority
13
- filename=${filename##* ---}
14
- # filename without path, priority or extension
15
- echo ${filename% .* .bash}
9
+ function _bash-it-get-component-name-from-path() {
10
+ local filename
11
+ # filename without path
12
+ filename=" ${1##*/ } "
13
+ # filename without path or priority
14
+ filename=" ${filename##* ${BASH_IT_LOAD_PRIORITY_SEPARATOR?} } "
15
+ # filename without path, priority or extension
16
+ echo " ${filename% .* .bash} "
16
17
}
17
18
18
- _bash-it-get-component-type-from-path () {
19
- # filename without path
20
- filename=${1##*/ }
21
- # filename without path or priority
22
- filename=${filename##* ---}
23
- # extension
24
- echo ${filename} | cut -d ' .' -f 2
19
+ function _bash-it-get-component-type-from-path() {
20
+ local filename
21
+ # filename without path
22
+ filename=" ${1##*/ } "
23
+ # filename without extension
24
+ filename=" ${filename% .bash} "
25
+ # extension without priority or name
26
+ filename=" ${filename##* .} "
27
+ echo " ${filename} "
25
28
}
26
29
27
30
# This function searches an array for an exact match against the term passed
@@ -43,96 +46,97 @@ _bash-it-get-component-type-from-path() {
43
46
# contains pear!
44
47
#
45
48
#
46
- _bash-it-array-contains-element () {
47
- local e
48
- for e in " ${@: 2} " ; do
49
- [[ " $e " == " $1 " ]] && return 0
50
- done
51
- return 1
49
+ function _bash-it-array-contains-element() {
50
+ local e
51
+ for e in " ${@: 2} " ; do
52
+ [[ " $e " == " $1 " ]] && return 0
53
+ done
54
+ return 1
52
55
}
53
56
54
57
# Dedupe a simple array of words without spaces.
55
- _bash-it-array-dedup () {
56
- echo " $* " | tr ' ' ' \n' | sort -u | tr ' \n' ' '
58
+ function _bash-it-array-dedup() {
59
+ local IFS=$' \n '
60
+ echo " $@ " | tr ' ' ' \n' | sort -u
57
61
}
58
62
59
63
# Outputs a full path of the grep found on the filesystem
60
- _bash-it-grep () {
61
- if [[ -z " ${BASH_IT_GREP:- } " ]] ; then
62
- export BASH_IT_GREP=" $( which egrep || which grep || ' /usr/bin/grep' ) "
63
- fi
64
- printf " %s " " ${BASH_IT_GREP} "
64
+ function _bash-it-grep() {
65
+ : " ${BASH_IT_GREP:= $(type -p egrep || type -p grep)} "
66
+ printf " %s" " ${BASH_IT_GREP:- ' /usr/bin/grep' } "
65
67
}
66
68
67
-
68
69
# ##########################################################################
69
70
# Component-specific functions (component is either an alias, a plugin, or a
70
71
# completion).
71
72
# ##########################################################################
72
73
73
- _bash-it-component-help () {
74
- local component=" $( _bash-it-pluralize-component " ${1} " ) "
75
- local file=" $( _bash-it-component-cache-file " ${component} " ) "
76
- if [[ ! -s " ${file} " || -z $( find " ${file} " -mmin -300) ]] ; then
77
- rm -f " ${file} " 2> /dev/null
78
- local func=" _bash-it-${component} "
79
- " ${func} " | $( _bash-it-grep) -E ' \[' | cat > " ${file} "
80
- fi
81
- cat " ${file} "
74
+ function _bash-it-component-help() {
75
+ local component file func
76
+ component=" $( _bash-it-pluralize-component " ${1} " ) "
77
+ file=" $( _bash-it-component-cache-file " ${component} " ) "
78
+ if [[ ! -s " ${file} " || -z " $( find " ${file} " -mmin -300) " ]]; then
79
+ rm -f " ${file} " 2> /dev/null
80
+ func=" _bash-it-${component} "
81
+ " ${func} " | ${BASH_IT_GREP:- $(_bash-it-grep)} -E ' \[' > " ${file} "
82
+ fi
83
+ cat " ${file} "
82
84
}
83
85
84
- _bash-it-component-cache-file () {
85
- local component=$( _bash-it-pluralize-component " ${1} " )
86
- local file=" ${BASH_IT} /tmp/cache/${component} "
87
- [[ -f " ${file} " ]] || mkdir -p " ${file%/* } "
88
- printf " ${file} "
86
+ function _bash-it-component-cache-file() {
87
+ local component file
88
+ component=" $( _bash-it-pluralize-component " ${1} " ) "
89
+ file=" ${BASH_IT?} /tmp/cache/${component} "
90
+ [[ -f " ${file} " ]] || mkdir -p " ${file%/* } "
91
+ printf ' %s' " ${file} "
89
92
}
90
93
91
- _bash-it-pluralize-component () {
92
- local component=" ${1} "
93
- local len=$(( ${# component} - 1 ))
94
- # pluralize component name for consistency
95
- [[ ${component: ${len} : 1} != ' s' ]] && component=" ${component} s"
96
- [[ ${component} == " alias" ]] && component=" aliases"
97
- printf ${component}
94
+ function _bash-it-pluralize-component() {
95
+ local component=" ${1} "
96
+ local -i len=$(( ${# component} - 1 ))
97
+ # pluralize component name for consistency
98
+ [[ " ${component: ${len} : 1} " != ' s' ]] && component=" ${component} s"
99
+ [[ " ${component} " == " alias" ]] && component=" aliases"
100
+ printf ' %s ' " ${component} "
98
101
}
99
102
100
- _bash-it-clean-component-cache () {
101
- local component=" $1 "
102
- local cache
103
- local -a BASH_IT_COMPONENTS=(aliases plugins completions)
104
- if [[ -z ${component} ]] ; then
105
- for component in " ${BASH_IT_COMPONENTS[@]} " ; do
106
- _bash-it-clean-component-cache " ${component} "
107
- done
108
- else
109
- cache=" $( _bash-it-component-cache-file ${component} ) "
110
- if [[ -f " ${cache} " ]] ; then
111
- rm -f " ${cache} "
112
- fi
113
- fi
103
+ function _bash-it-clean-component-cache() {
104
+ local component=" $1 "
105
+ local cache
106
+ local -a BASH_IT_COMPONENTS=(aliases plugins completions)
107
+ if [[ -z " ${component} " ]]; then
108
+ for component in " ${BASH_IT_COMPONENTS[@]} " ; do
109
+ _bash-it-clean-component-cache " ${component} "
110
+ done
111
+ else
112
+ cache=" $( _bash-it-component-cache-file " ${component} " ) "
113
+ if [[ -f " ${cache} " ]]; then
114
+ rm -f " ${cache} "
115
+ fi
116
+ fi
114
117
}
115
118
116
119
# Returns an array of items within each compoenent.
117
- _bash-it-component-list () {
118
- local component=" $1 "
119
- _bash-it-component-help " ${component} " | awk ' {print $1}' | uniq | sort | tr ' \n ' ' '
120
+ function _bash-it-component-list() {
121
+ local IFS= $' \n ' component=" $1 "
122
+ _bash-it-component-help " ${component} " | awk ' {print $1}' | sort -u
120
123
}
121
124
122
- _bash-it-component-list-matching () {
123
- local component=" $1 " ; shift
124
- local term=" $1 "
125
- _bash-it-component-help " ${component} " | $( _bash-it-grep) -E -- " ${term} " | awk ' {print $1}' | sort | uniq
125
+ function _bash-it-component-list-matching() {
126
+ local component=" $1 "
127
+ shift
128
+ local term=" $1 "
129
+ _bash-it-component-help " ${component} " | ${BASH_IT_GREP:- $(_bash-it-grep)} -E -- " ${term} " | awk ' {print $1}' | sort -u
126
130
}
127
131
128
- _bash-it-component-list-enabled () {
129
- local component=" $1 "
130
- _bash-it-component-help " ${component} " | $( _bash-it-grep) -E ' \[x\]' | awk ' {print $1}' | uniq | sort | tr ' \n ' ' '
132
+ function _bash-it-component-list-enabled() {
133
+ local IFS= $' \n ' component=" $1 "
134
+ _bash-it-component-help " ${component} " | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E ' \[x\]' | awk ' {print $1}' | sort -u
131
135
}
132
136
133
- _bash-it-component-list-disabled () {
134
- local component=" $1 "
135
- _bash-it-component-help " ${component} " | $( _bash-it-grep) -E -v ' \[x\]' | awk ' {print $1}' | uniq | sort | tr ' \n ' ' '
137
+ function _bash-it-component-list-disabled() {
138
+ local IFS= $' \n ' component=" $1 "
139
+ _bash-it-component-help " ${component} " | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E -v ' \[x\]' | awk ' {print $1}' | sort -u
136
140
}
137
141
138
142
# Checks if a given item is enabled for a particular component/file-type.
@@ -143,10 +147,10 @@ _bash-it-component-list-disabled() {
143
147
#
144
148
# Examples:
145
149
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
146
- _bash-it-component-item-is-enabled () {
147
- local component=" $1 "
148
- local item=" $2 "
149
- _bash-it-component-help " ${component} " | $( _bash-it-grep) -E ' \[x\]' | $ ( _bash-it-grep) -E -q -- " ^${item} \s"
150
+ function _bash-it-component-item-is-enabled() {
151
+ local component=" $1 "
152
+ local item=" $2 "
153
+ _bash-it-component-help " ${component} " | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E ' \[x\]' | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E -q -- " ^${item} \s"
150
154
}
151
155
152
156
# Checks if a given item is disabled for a particular component/file-type.
@@ -157,8 +161,8 @@ _bash-it-component-item-is-enabled() {
157
161
#
158
162
# Examples:
159
163
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
160
- _bash-it-component-item-is-disabled () {
161
- local component=" $1 "
162
- local item=" $2 "
163
- _bash-it-component-help " ${component} " | $( _bash-it-grep) -E -v ' \[x\]' | $ ( _bash-it-grep) -E -q -- " ^${item} \s"
164
+ function _bash-it-component-item-is-disabled() {
165
+ local component=" $1 "
166
+ local item=" $2 "
167
+ _bash-it-component-help " ${component} " | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E -v ' \[x\]' | ${BASH_IT_GREP :- $ (_bash-it-grep)} -E -q -- " ^${item} \s"
164
168
}
0 commit comments