From f7cc442af4439d2156b60bc9846cb299dadae687 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 13 Oct 2021 09:27:55 -0700 Subject: [PATCH 1/5] lib/utilities: simplify `_bash-it-array-dedup()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit alsö fix usage example of `_bash-it-array-contains-element()` --- lib/utilities.bash | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 84fa4d96..9b011910 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -40,7 +40,7 @@ function _bash-it-get-component-type-from-path() { # $ _bash-it-array-contains-element apple "@{fruits[@]}" && echo 'contains apple' # contains apple # -# $ if $(_bash-it-array-contains-element pear "${fruits[@]}"); then +# $ if _bash-it-array-contains-element pear "${fruits[@]}"; then # echo "contains pear!" # fi # contains pear! @@ -54,10 +54,9 @@ function _bash-it-array-contains-element() { return 1 } -# Dedupe a simple array of words without spaces. +# Dedupe an array (without embedded newlines). function _bash-it-array-dedup() { - local IFS=$'\n' - echo "$@" | tr ' ' '\n' | sort -u + printf '%s\n' "$@" | sort -u } # Outputs a full path of the grep found on the filesystem From 7cd02781f8df8af55dc0e7c6127f624c3ea34373 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 7 Oct 2021 22:43:48 -0700 Subject: [PATCH 2/5] lib/utilities: `_bash-it-component-help()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No need to `rm` when we overwrite the file the line after next. Alsö, use `>|` in case the user sets `noclobber`; we do expressly intend to overwrite the file in this case. --- lib/utilities.bash | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 9b011910..a6701fff 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -75,9 +75,8 @@ function _bash-it-component-help() { component="$(_bash-it-pluralize-component "${1}")" file="$(_bash-it-component-cache-file "${component}")" if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then - rm -f "${file}" 2> /dev/null func="_bash-it-${component}" - "${func}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E ' \[' > "${file}" + "${func}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E ' \[' >| "${file}" fi cat "${file}" } From b986c390401f36b44630576e87551f6a84fb1e17 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 12 Oct 2021 13:16:28 -0700 Subject: [PATCH 3/5] lib/utilities: XDG_CACHE_HOME Use $XDG_CACHE_HOME environment constant instead of placing a tmp/cache folder inside the bash-it data repo. If not defined, fall back to current behavior. This resolves Bash-It/Bash-It#1904. --- lib/utilities.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index a6701fff..3ceda5f1 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -83,8 +83,8 @@ function _bash-it-component-help() { function _bash-it-component-cache-file() { local component file - component="$(_bash-it-pluralize-component "${1}")" - file="${BASH_IT?}/tmp/cache/${component}" + component="$(_bash-it-pluralize-component "${1?${FUNCNAME[0]}: component name required}")" + file="${XDG_CACHE_HOME:-${BASH_IT?}/tmp/cache}${XDG_CACHE_HOME:+/bash_it}/${component}" [[ -f "${file}" ]] || mkdir -p "${file%/*}" printf '%s' "${file}" } From b8ee63c67d1be89c9abf9e330b5b63f79328e104 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 12 Oct 2021 10:58:49 -0700 Subject: [PATCH 4/5] lib/utilities: quote SC2295 Doesn't show up on my shellcheck 0.7.2, but does for NoahGorny! --- lib/utilities.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 3ceda5f1..52c48776 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -11,7 +11,7 @@ function _bash-it-get-component-name-from-path() { # filename without path filename="${1##*/}" # filename without path or priority - filename="${filename##*${BASH_IT_LOAD_PRIORITY_SEPARATOR?}}" + filename="${filename##*"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}"}" # filename without path, priority or extension echo "${filename%.*.bash}" } From 253d1213e07ad33cfadd12fe3cff128e4b22cfb8 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 7 Oct 2021 22:39:49 -0700 Subject: [PATCH 5/5] lib/utilities: new function `_bash-it-egrep()` The existing function `_bash-it-grep()` is weird. New function `_bash-it-egrep()` just does the thing without requiring two subshells and manual invocation. --- lib/utilities.bash | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utilities.bash b/lib/utilities.bash index 52c48776..1f249b98 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -65,6 +65,12 @@ function _bash-it-grep() { printf "%s" "${BASH_IT_GREP:-'/usr/bin/grep'}" } +# Runs `grep` with extended regular expressions +function _bash-it-egrep() { + : "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}" + "${BASH_IT_GREP:-/usr/bin/grep}" -E "$@" +} + ########################################################################### # Component-specific functions (component is either an alias, a plugin, or a # completion).