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.pull/1999/head
parent
2b5e531396
commit
7430a06ec2
|
|
@ -79,49 +79,46 @@ function _bash-it-egrep() {
|
||||||
|
|
||||||
function _bash-it-component-help() {
|
function _bash-it-component-help() {
|
||||||
local component file func
|
local component file func
|
||||||
_bash-it-component-pluralize "${1}"
|
_bash-it-component-pluralize "${1}" component
|
||||||
component="${_bash_it_component_pluralized_name?}"
|
_bash-it-component-cache-file "${component}" file
|
||||||
_bash-it-component-cache-file "${component}"
|
if [[ ! -s "${file?}" || -z "$(find "${file}" -mmin -300)" ]]; then
|
||||||
file="${_bash_it_component_cache_filename?}"
|
func="_bash-it-${component?}"
|
||||||
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
|
|
||||||
func="_bash-it-${component}"
|
|
||||||
"${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}"
|
"${func}" | _bash-it-egrep '\[[x ]\]' >| "${file}"
|
||||||
fi
|
fi
|
||||||
cat "${file}"
|
cat "${file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-component-cache-file() {
|
function _bash-it-component-cache-file() {
|
||||||
local component file
|
local _component_to_cache _file_path _result="${2:-${FUNCNAME[0]//-/_}}"
|
||||||
_bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}"
|
_bash-it-component-pluralize "${1?${FUNCNAME[0]}: component name required}" _component_to_cache
|
||||||
component="${_bash_it_component_pluralized_name?}"
|
_file_path="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${_component_to_cache?}"
|
||||||
file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}"
|
[[ -f "${_file_path}" ]] || mkdir -p "${_file_path%/*}"
|
||||||
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
|
printf -v "${_result?}" '%s' "${_file_path}"
|
||||||
printf -v _bash_it_component_cache_filename '%s' "${file}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-component-singularize() {
|
function _bash-it-component-singularize() {
|
||||||
[[ -n "${2:-}" ]] && local -n _bash_it_component_singularized_name="${2?}"
|
local _result="${2:-${FUNCNAME[0]//-/_}}"
|
||||||
local component="${1?${FUNCNAME[0]}: component name required}"
|
local _component_to_single="${1?${FUNCNAME[0]}: component name required}"
|
||||||
local -i len="$((${#component} - 2))"
|
local -i len="$((${#_component_to_single} - 2))"
|
||||||
if [[ "${component:${len}:2}" == 'ns' ]]; then
|
if [[ "${_component_to_single:${len}:2}" == 'ns' ]]; then
|
||||||
component="${component%s}"
|
_component_to_single="${_component_to_single%s}"
|
||||||
elif [[ "${component}" == "aliases" ]]; then
|
elif [[ "${_component_to_single}" == "aliases" ]]; then
|
||||||
component="${component%es}"
|
_component_to_single="${_component_to_single%es}"
|
||||||
fi
|
fi
|
||||||
printf -v _bash_it_component_singularized_name '%s' "${component}"
|
printf -v "${_result?}" '%s' "${_component_to_single}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-component-pluralize() {
|
function _bash-it-component-pluralize() {
|
||||||
[[ -n "${2:-}" ]] && local -n _bash_it_component_pluralized_name="${2?}"
|
local _result="${2:-${FUNCNAME[0]//-/_}}"
|
||||||
local component="${1?${FUNCNAME[0]}: component name required}"
|
local _component_to_plural="${1?${FUNCNAME[0]}: component name required}"
|
||||||
local -i len="$((${#component} - 1))"
|
local -i len="$((${#_component_to_plural} - 1))"
|
||||||
# pluralize component name for consistency
|
# pluralize component name for consistency
|
||||||
if [[ "${component:${len}:1}" != 's' ]]; then
|
if [[ "${_component_to_plural:${len}:1}" != 's' ]]; then
|
||||||
component="${component}s"
|
_component_to_plural="${_component_to_plural}s"
|
||||||
elif [[ "${component}" == "alias" ]]; then
|
elif [[ "${_component_to_plural}" == "alias" ]]; then
|
||||||
component="${component}es"
|
_component_to_plural="${_component_to_plural}es"
|
||||||
fi
|
fi
|
||||||
printf -v _bash_it_component_pluralized_name '%s' "${component}"
|
printf -v "${_result?}" '%s' "${_component_to_plural}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function _bash-it-clean-component-cache() {
|
function _bash-it-clean-component-cache() {
|
||||||
|
|
@ -133,8 +130,7 @@ function _bash-it-clean-component-cache() {
|
||||||
_bash-it-clean-component-cache "${component}"
|
_bash-it-clean-component-cache "${component}"
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
_bash-it-component-cache-file "${component}"
|
_bash-it-component-cache-file "${component}" cache
|
||||||
cache="${_bash_it_component_cache_filename?}"
|
|
||||||
if [[ -f "${cache}" ]]; then
|
if [[ -f "${cache}" ]]; then
|
||||||
rm -f "${cache}"
|
rm -f "${cache}"
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue