|
3 | 3 | # Usage: pyenv commands [--sh|--no-sh] |
4 | 4 |
|
5 | 5 | set -e |
6 | | -[ -n "$PYENV_DEBUG" ] && set -x |
| 6 | +[[ -n $PYENV_DEBUG ]] && set -x |
7 | 7 |
|
8 | 8 | # Provide pyenv completions |
9 | | -if [ "$1" = "--complete" ]; then |
| 9 | +if [[ $1 = "--complete" ]]; then |
10 | 10 | echo --sh |
11 | 11 | echo --no-sh |
12 | 12 | exit |
13 | 13 | fi |
14 | 14 |
|
15 | | -if [ "$1" = "--sh" ]; then |
| 15 | +if [[ $1 = "--sh" ]]; then |
16 | 16 | sh=1 |
17 | 17 | shift |
18 | | -elif [ "$1" = "--no-sh" ]; then |
| 18 | +elif [[ $1 = "--no-sh" ]]; then |
19 | 19 | nosh=1 |
20 | 20 | shift |
21 | 21 | fi |
22 | 22 |
|
23 | 23 | shopt -s nullglob |
24 | 24 |
|
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/' |
33 | 30 | 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/' |
47 | 34 | 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