From c4e556ededaff1110ced3e0a091caf83c88174f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 5 Dec 2020 15:09:46 +0200 Subject: [PATCH] versions: use associative array to hold current versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "hyperfine pyenv-versions" before with my bash 4.4: Time (mean ± σ): 81.7 ms ± 2.2 ms [User: 67.8 ms, System: 15.7 ms] Range (min … max): 78.3 ms … 87.2 ms 34 runs After: Time (mean ± σ): 70.6 ms ± 2.4 ms [User: 55.6 ms, System: 16.9 ms] Range (min … max): 67.9 ms … 78.9 ms 41 runs --- libexec/pyenv-versions | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index 019c0e90..91c99512 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -64,16 +64,27 @@ if [ -d "$versions_dir" ]; then versions_dir="$(realpath "$versions_dir")" fi +if ((${BASH_VERSINFO[0]} > 3)); then + declare -A current_versions +else + current_versions=() +fi if [ -n "$bare" ]; then hit_prefix="" miss_prefix="" - current_versions=() include_system="" else hit_prefix="* " miss_prefix=" " OLDIFS="$IFS" - IFS=: current_versions=($(pyenv-version-name || true)) + IFS=: + if ((${BASH_VERSINFO[0]} > 3)); then + for i in $(pyenv-version-name || true); do + current_versions["$i"]="1" + done + else + current_versions=($(pyenv-version-name || true)) + fi IFS="$OLDIFS" include_system="1" fi @@ -93,7 +104,9 @@ exists() { } print_version() { - if exists "$1" "${current_versions[@]}"; then + if [[ ${BASH_VERSINFO[0]} -ge 4 && ${current_versions["$1"]} ]]; then + echo "${hit_prefix}$1 (set by $(pyenv-version-origin))" + elif (( ${BASH_VERSINFO[0]} < 3 )) && exists "$1" "${current_versions[@]}"; then echo "${hit_prefix}$1 (set by $(pyenv-version-origin))" else echo "${miss_prefix}$1"