Optimizations to reloader.bash (#1749)

* Optimized statement with REGEX by using `case`

REGEX is a great feature of BASH, but in this case it was energy
needlessly spent. A `case` statement suffices. Bring in REGEX when
you're going to make good use of it, otherwise it's just going to bog
down your code.

I also wanted to strip the ` || exit 1` on the last line, but I wasn't
sure if this file is meant to be sourced or not; if not, then exiting
like that is redundant because it will already exit with whichever
status the last command provides, unless `popd` specifically offers
unhelpful or no exit statuses.

* Optimize as before but with 2nd REGEX instance

This time is much like the last commit, so I won't repeat, but I will
say that you're using a double- or even triple-negative, which
obfuscates your goal. Where you were doing...

[ ! -n VAR ] &&

You were basically saying this convolution:

If it's true that it's not true that VAR is not empty.

Very confusing. Instead, I've opted for:

If it's true that Var is empty.

Makes immediate sense and is easier to parse, visually speaking.
pull/1752/head
Terminal for Life 2020-12-27 15:14:50 +00:00 committed by GitHub
parent 5aa2612ff1
commit 921ea9ac76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 19 deletions

View File

@ -11,12 +11,15 @@ function _set-prefix-based-on-path()
if [ "$1" != "skip" ] && [ -d "./enabled" ]; then if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
_bash_it_config_type="" _bash_it_config_type=""
if [[ "${1}" =~ ^(alias|completion|plugin)$ ]]; then
case $1 in
alias|completion|plugin)
_bash_it_config_type=$1 _bash_it_config_type=$1
_log_debug "Loading enabled $1 components..." _log_debug "Loading enabled $1 components..." ;;
else *|'')
_log_debug "Loading all enabled components..." _log_debug "Loading all enabled components..." ;;
fi esac
for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do for _bash_it_config_file in $(sort <(compgen -G "./enabled/*${_bash_it_config_type}.bash")); do
if [ -e "${_bash_it_config_file}" ]; then if [ -e "${_bash_it_config_file}" ]; then
_set-prefix-based-on-path "${_bash_it_config_file}" _set-prefix-based-on-path "${_bash_it_config_file}"
@ -29,8 +32,9 @@ if [ "$1" != "skip" ] && [ -d "./enabled" ]; then
done done
fi fi
if [ -n "${2}" ] && [ -d "${2}/enabled" ]; then
if [ ! -z "${2}" ] && [[ "${2}" =~ ^(aliases|completion|plugins)$ ]] && [ -d "${2}/enabled" ]; then case $2 in
aliases|completion|plugins)
_log_warning "Using legacy enabling for $2, please update your bash-it version and migrate" _log_warning "Using legacy enabling for $2, please update your bash-it version and migrate"
for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do for _bash_it_config_file in $(sort <(compgen -G "./${2}/enabled/*.bash")); do
if [ -e "$_bash_it_config_file" ]; then if [ -e "$_bash_it_config_file" ]; then
@ -41,7 +45,8 @@ if [ ! -z "${2}" ] && [[ "${2}" =~ ^(aliases|completion|plugins)$ ]] && [ -d "${
else else
echo "Unable to locate ${_bash_it_config_file}" > /dev/stderr echo "Unable to locate ${_bash_it_config_file}" > /dev/stderr
fi fi
done done ;;
esac
fi fi
unset _bash_it_config_file unset _bash_it_config_file