versions: use associative array to hold current versions

"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
This commit is contained in:
Ville Skyttä 2020-12-05 15:09:46 +02:00
parent a804887307
commit c4e556eded

View file

@ -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"