From e3bd30f98d435bb1d04fb144785df3922a7ef355 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 23 Oct 2021 22:29:09 -0700 Subject: [PATCH 1/5] lib/utilities: autonomize `_bash-it-component-item-is-enabled()` Make `_bash-it-component-item-is-enabled()` operate *without* using `_bash-it-component-help()`...so it's now *much* faster. --- lib/utilities.bash | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 575787d8..42e785b9 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -144,7 +144,6 @@ function _bash-it-component-list-disabled() { } # Checks if a given item is enabled for a particular component/file-type. -# Uses the component cache if available. # # Returns: # 0 if an item of the component is enabled, 1 otherwise. @@ -152,13 +151,17 @@ function _bash-it-component-list-disabled() { # Examples: # _bash-it-component-item-is-enabled alias git && echo "git alias is enabled" function _bash-it-component-item-is-enabled() { - local component="$1" - local item="$2" - _bash-it-component-help "${component}" | _bash-it-egrep '\[x\]' | _bash-it-egrep -q -- "^${item}\s" + local component="$1" item="$2" + local each_file + + for each_file in "${BASH_IT}/enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item}.${component}"*."bash" \ + "${BASH_IT}/${component}"*/"enabled/${item}.${component}"*."bash" \ + "${BASH_IT}/${component}"*/"enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${item}.${component}"*."bash"; do + [[ -f "${each_file}" ]] && return + done } # Checks if a given item is disabled for a particular component/file-type. -# Uses the component cache if available. # # Returns: # 0 if an item of the component is enabled, 1 otherwise. @@ -166,7 +169,5 @@ function _bash-it-component-item-is-enabled() { # Examples: # _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled" function _bash-it-component-item-is-disabled() { - local component="$1" - local item="$2" - _bash-it-component-help "${component}" | _bash-it-egrep -v '\[x\]' | _bash-it-egrep -q -- "^${item}\s" + ! _bash-it-component-item-is-enabled "$@" } From c3eaa606de4e63cf75d5f1cce60bec2df7edd575 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 23 Oct 2021 22:31:32 -0700 Subject: [PATCH 2/5] lib/utilities: fix `_bash-it-component-help()` for long component names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alsö, minor tweak to `_bash-it-array-contains-element()` for clarity. This fixes Bash-It/bash-it#1978. --- lib/utilities.bash | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 42e785b9..79cb8073 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -47,9 +47,10 @@ function _bash-it-get-component-type-from-path() { # # function _bash-it-array-contains-element() { - local e - for e in "${@:2}"; do - [[ "$e" == "$1" ]] && return 0 + local e element="${1?}" + shift + for e in "$@"; do + [[ "$e" == "${element}" ]] && return 0 done return 1 } @@ -82,7 +83,7 @@ function _bash-it-component-help() { file="$(_bash-it-component-cache-file "${component}")" if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then func="_bash-it-${component}" - "${func}" | _bash-it-egrep ' \[' >| "${file}" + "${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}" fi cat "${file}" } From 4f700dfb3b527e72afddf5ac0dd4f6f559bba063 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 26 Oct 2021 11:12:34 -0700 Subject: [PATCH 3/5] lib/utilities: rewrite _bash-it-component-pluralize Fix up and rename `_bash-it-pluralize-component()` to `_bash-it-component-pluralize()`, and add matching function `_bash-it-component-singularize()`. --- lib/utilities.bash | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 79cb8073..b4b4a8a7 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -79,7 +79,8 @@ function _bash-it-egrep() { function _bash-it-component-help() { local component file func - component="$(_bash-it-pluralize-component "${1}")" + _bash-it-component-pluralize "${1}" + component="${_bash_it_component_pluralized_name?}" file="$(_bash-it-component-cache-file "${component}")" if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then func="_bash-it-${component}" @@ -90,19 +91,36 @@ function _bash-it-component-help() { function _bash-it-component-cache-file() { local component file - component="$(_bash-it-pluralize-component "${1?${FUNCNAME[0]}: component name required}")" + _bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" + component="${_bash_it_component_pluralized_name?}" file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}" [[ -f "${file}" ]] || mkdir -p "${file%/*}" printf '%s' "${file}" } -function _bash-it-pluralize-component() { - local component="${1}" - local -i len=$((${#component} - 1)) +function _bash-it-component-singularize() { + [[ -n "${2:-}" ]] && local -n _bash_it_component_singularized_name="${2?}" + local component="${1?${FUNCNAME[0]}: component name required}" + local -i len="$((${#component} - 2))" + if [[ "${component:${len}:2}" == 'ns' ]]; then + component="${component%s}" + elif [[ "${component}" == "aliases" ]]; then + component="${component%es}" + fi + printf -v _bash_it_component_singularized_name '%s' "${component}" +} + +function _bash-it-component-pluralize() { + [[ -n "${2:-}" ]] && local -n _bash_it_component_pluralized_name="${2?}" + local component="${1?${FUNCNAME[0]}: component name required}" + local -i len="$((${#component} - 1))" # pluralize component name for consistency - [[ "${component:${len}:1}" != 's' ]] && component="${component}s" - [[ "${component}" == "alias" ]] && component="aliases" - printf '%s' "${component}" + if [[ "${component:${len}:1}" != 's' ]]; then + component="${component}s" + elif [[ "${component}" == "alias" ]]; then + component="${component}es" + fi + printf -v _bash_it_component_pluralized_name '%s' "${component}" } function _bash-it-clean-component-cache() { From 2b5e5313968c0ab24495b0b30003acb2407eeaf0 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 27 Dec 2021 14:03:58 -0800 Subject: [PATCH 4/5] lib/utilities: update `_bash-it-component-cache-file()` Match idiom of `_bash-it-component-singularize()` --- lib/utilities.bash | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index b4b4a8a7..6711314c 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -81,7 +81,8 @@ function _bash-it-component-help() { local component file func _bash-it-component-pluralize "${1}" component="${_bash_it_component_pluralized_name?}" - file="$(_bash-it-component-cache-file "${component}")" + _bash-it-component-cache-file "${component}" + file="${_bash_it_component_cache_filename?}" if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then func="_bash-it-${component}" "${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}" @@ -95,7 +96,7 @@ function _bash-it-component-cache-file() { component="${_bash_it_component_pluralized_name?}" file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}" [[ -f "${file}" ]] || mkdir -p "${file%/*}" - printf '%s' "${file}" + printf -v _bash_it_component_cache_filename '%s' "${file}" } function _bash-it-component-singularize() { @@ -132,7 +133,8 @@ function _bash-it-clean-component-cache() { _bash-it-clean-component-cache "${component}" done else - cache="$(_bash-it-component-cache-file "${component}")" + _bash-it-component-cache-file "${component}" + cache="${_bash_it_component_cache_filename?}" if [[ -f "${cache}" ]]; then rm -f "${cache}" fi From 7430a06ec2bcabdb1f7303ff95c3c93266246697 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 28 Dec 2021 22:53:26 -0800 Subject: [PATCH 5/5] lib/utilities: Use variable indirection Don't use `local -n var` so that we can support v3.2... Note: function names and variable names are different namespaces, so we can have a variable named the same as the function...which makes it really easy to predict default names for results when returning this way. --- lib/utilities.bash | 56 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 6711314c..a228e412 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -79,49 +79,46 @@ function _bash-it-egrep() { function _bash-it-component-help() { local component file func - _bash-it-component-pluralize "${1}" - component="${_bash_it_component_pluralized_name?}" - _bash-it-component-cache-file "${component}" - file="${_bash_it_component_cache_filename?}" - if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then - func="_bash-it-${component}" + _bash-it-component-pluralize "${1}" component + _bash-it-component-cache-file "${component}" file + if [[ ! -s "${file?}" || -z "$(find "${file}" -mmin -300)" ]]; then + func="_bash-it-${component?}" "${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}" fi cat "${file}" } function _bash-it-component-cache-file() { - local component file - _bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" - component="${_bash_it_component_pluralized_name?}" - file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}" - [[ -f "${file}" ]] || mkdir -p "${file%/*}" - printf -v _bash_it_component_cache_filename '%s' "${file}" + local _component_to_cache _file_path _result="${2:-${FUNCNAME[0]//-/_}}" + _bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" _component_to_cache + _file_path="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${_component_to_cache?}" + [[ -f "${_file_path}" ]] || mkdir -p "${_file_path%/*}" + printf -v "${_result?}" '%s' "${_file_path}" } function _bash-it-component-singularize() { - [[ -n "${2:-}" ]] && local -n _bash_it_component_singularized_name="${2?}" - local component="${1?${FUNCNAME[0]}: component name required}" - local -i len="$((${#component} - 2))" - if [[ "${component:${len}:2}" == 'ns' ]]; then - component="${component%s}" - elif [[ "${component}" == "aliases" ]]; then - component="${component%es}" + local _result="${2:-${FUNCNAME[0]//-/_}}" + local _component_to_single="${1?${FUNCNAME[0]}: component name required}" + local -i len="$((${#_component_to_single} - 2))" + if [[ "${_component_to_single:${len}:2}" == 'ns' ]]; then + _component_to_single="${_component_to_single%s}" + elif [[ "${_component_to_single}" == "aliases" ]]; then + _component_to_single="${_component_to_single%es}" fi - printf -v _bash_it_component_singularized_name '%s' "${component}" + printf -v "${_result?}" '%s' "${_component_to_single}" } function _bash-it-component-pluralize() { - [[ -n "${2:-}" ]] && local -n _bash_it_component_pluralized_name="${2?}" - local component="${1?${FUNCNAME[0]}: component name required}" - local -i len="$((${#component} - 1))" + local _result="${2:-${FUNCNAME[0]//-/_}}" + local _component_to_plural="${1?${FUNCNAME[0]}: component name required}" + local -i len="$((${#_component_to_plural} - 1))" # pluralize component name for consistency - if [[ "${component:${len}:1}" != 's' ]]; then - component="${component}s" - elif [[ "${component}" == "alias" ]]; then - component="${component}es" + if [[ "${_component_to_plural:${len}:1}" != 's' ]]; then + _component_to_plural="${_component_to_plural}s" + elif [[ "${_component_to_plural}" == "alias" ]]; then + _component_to_plural="${_component_to_plural}es" fi - printf -v _bash_it_component_pluralized_name '%s' "${component}" + printf -v "${_result?}" '%s' "${_component_to_plural}" } function _bash-it-clean-component-cache() { @@ -133,8 +130,7 @@ function _bash-it-clean-component-cache() { _bash-it-clean-component-cache "${component}" done else - _bash-it-component-cache-file "${component}" - cache="${_bash_it_component_cache_filename?}" + _bash-it-component-cache-file "${component}" cache if [[ -f "${cache}" ]]; then rm -f "${cache}" fi