Skip to content

Commit 68113a3

Browse files
Try replace loops with a pipeline
1 parent f63a795 commit 68113a3

1 file changed

Lines changed: 17 additions & 25 deletions

File tree

libexec/pyenv-commands

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,37 @@
33
# Usage: pyenv commands [--sh|--no-sh]
44

55
set -e
6-
[ -n "$PYENV_DEBUG" ] && set -x
6+
[[ -n $PYENV_DEBUG ]] && set -x
77

88
# Provide pyenv completions
9-
if [ "$1" = "--complete" ]; then
9+
if [[ $1 = "--complete" ]]; then
1010
echo --sh
1111
echo --no-sh
1212
exit
1313
fi
1414

15-
if [ "$1" = "--sh" ]; then
15+
if [[ $1 = "--sh" ]]; then
1616
sh=1
1717
shift
18-
elif [ "$1" = "--no-sh" ]; then
18+
elif [[ $1 = "--no-sh" ]]; then
1919
nosh=1
2020
shift
2121
fi
2222

2323
shopt -s nullglob
2424

25-
if [ -n "$sh" ]; then
26-
# Fast path: glob only pyenv-sh-* files, skip the filter
27-
{ IFS=:; for path in $PATH; do
28-
for command in "${path}"/pyenv-sh-*; do
29-
echo "${command##*/pyenv-sh-}"
30-
done
31-
done
32-
} | sort -u
25+
declare search_pattern= script_replace= script_filter=
26+
27+
if [[ -n $sh ]]; then
28+
search_prefix=pyenv-sh-
29+
script_replace='s/^.*\/pyenv-sh-([^/]+)$/\1/'
3330
else
34-
{ IFS=:; for path in $PATH; do
35-
for command in "${path}"/pyenv-*; do
36-
command="${command##*pyenv-}"
37-
if [ -n "$nosh" ]; then
38-
if [ "${command:0:3}" != "sh-" ]; then
39-
echo "$command"
40-
fi
41-
else
42-
echo "${command##sh-}"
43-
fi
44-
done
45-
done
46-
} | sort -u
31+
search_prefix=pyenv-
32+
[[ -n $nosh ]] && script_filter='/^.*\/pyenv-sh-[^/]+$/d'
33+
script_replace='s/^.*\/pyenv-(sh-)?([^/]+)/\2/'
4734
fi
35+
36+
#IFS=:; find -- $PATH -mindepth 1 -maxdepth 1 -executable -name "$search_prefix*" 2>/dev/null \
37+
compgen -c "$search_prefix" \
38+
| sed -Ee "$script_filter" -e "$script_replace" | sort -u
39+

0 commit comments

Comments
 (0)