summary refs log tree commit diff
path: root/Completion/Base/Utility/_pick_variant
blob: 9099e359987dc90451c2114f3dd7a699e914905f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#autoload

local output cmd pat
local -a var
local -A opts

(( $+_cmd_variant )) || typeset -gA _cmd_variant

zparseopts -D -A opts b: c: r:
: ${opts[-c]:=$words[1]}

while [[ $1 = *=* ]]; do
  var+=( "${1%%\=*}" "${1#*=}" )
  shift
done
if (( $+_cmd_variant[$opts[-c]] )); then
  (( $+opts[-r] )) && eval "${opts[-r]}=${_cmd_variant[$opts[-c]]}"
  [[ $_cmd_variant[$opts[-c]] = "$1" ]] && return 1
  return 0
fi

if [[ $+opts[-b] -eq 1 && -n $builtins[$opts[-c]] ]]; then
  _cmd_variant[$opts[-c]]=$opts[-b]
  (( $+opts[-r] )) && eval "${opts[-r]}=${_cmd_variant[$opts[-c]]}"
  return 0
fi

output="$(_call_program variant $opts[-c] "${@[2,-1]}" </dev/null 2>&1)"

for cmd pat in "$var[@]"; do
  if [[ $output = *$~pat* ]]; then
    (( $+opts[-r] )) && eval "${opts[-r]}=$cmd"
    _cmd_variant[$opts[-c]]="$cmd"
    return 0
  fi
done

(( $+opts[-r] )) && eval "${opts[-r]}=$1"
_cmd_variant[$opts[-c]]="$1"

return 1