/
zsh.erb
209 lines (169 loc) 路 5.67 KB
/
zsh.erb
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<%
# To make changes to the completions:
#
# - For changes to a command under `COMMANDS` or `DEVELOPER COMMANDS` sections):
# - Find the source file in `Library/Homebrew/[dev-]cmd/<command>.{rb,sh}`.
# - For `.rb` files, edit the `<command>_args` method.
# - For `.sh` files, edit the top comment, being sure to use the line prefix
# `#:` for the comments to be recognized as documentation. If in doubt,
# compare with already documented commands.
# - For other changes: Edit this file.
#
# When done, regenerate the completions by running `brew man`.
%>
#compdef brew
#autoload
# Brew ZSH completion function
# functions starting with __brew are helper functions that complete or list
# various types of items.
# functions starting with _brew_ are completions for brew commands
# this mechanism can be extended by external commands by defining a function
# named _brew_<external-name>. See _brew_cask for an example of this.
# a list of aliased internal commands
__brew_list_aliases() {
local -a aliases
aliases=(
<%= aliases.join("\n ") + "\n" %>
)
echo "${aliases}"
}
__brew_formulae_or_ruby_files() {
_alternative 'files:files:{_files -g "*.rb"}'
}
# completions remain in cache until any tap has new commits
__brew_completion_caching_policy() {
local -a tmp
# invalidate if cache file is missing or >=2 weeks old
tmp=( $1(mw-2N) )
(( $#tmp )) || return 0
# otherwise, invalidate if latest tap index file is missing or newer than cache file
tmp=( ${HOMEBREW_REPOSITORY:-/usr/local/Homebrew}/Library/Taps/*/*/.git/index(om[1]N) )
[[ -z $tmp || $tmp -nt $1 ]]
}
__brew_formulae() {
[[ -prefix '-' ]] && return 0
local -a list
local comp_cachename=brew_formulae
if ! _retrieve_cache $comp_cachename; then
list=( $(brew formulae) )
_store_cache $comp_cachename list
fi
_describe -t formulae 'all formulae' list
}
__brew_installed_formulae() {
[[ -prefix '-' ]] && return 0
local -a formulae
formulae=($(brew list --formula))
_describe -t formulae 'installed formulae' formulae
}
__brew_outdated_formulae() {
[[ -prefix '-' ]] && return 0
local -a formulae
formulae=($(brew outdated --formula))
_describe -t formulae 'outdated formulae' formulae
}
__brew_casks() {
[[ -prefix '-' ]] && return 0
local -a list
local expl
local comp_cachename=brew_casks
if ! _retrieve_cache $comp_cachename; then
list=( $(brew search --cask) )
_store_cache $comp_cachename list
fi
_wanted list expl 'all casks' compadd -a list
}
__brew_installed_casks() {
[[ -prefix '-' ]] && return 0
local -a list
local expl
list=( $(brew list --cask) )
_wanted list expl 'installed casks' compadd -a list
}
__brew_outdated_casks() {
[[ -prefix '-' ]] && return 0
local -a casks
casks=($(brew outdated --cask))
_describe -t casks 'outdated casks' casks
}
__brew_installed_taps() {
[[ -prefix '-' ]] && return 0
local -a taps
taps=($(brew tap))
_describe -t installed-taps 'installed taps' taps
}
__brew_any_tap() {
[[ -prefix '-' ]] && return 0
_alternative \
'installed-taps:installed taps:__brew_installed_taps'
}
__brew_internal_commands() {
local -a commands
commands=(
<%= builtin_command_descriptions.join("\n ") + "\n" %>
)
_describe -t internal-commands 'internal commands' commands
}
__brew_external_commands() {
local -a list
local comp_cachename=brew_all_commands
if ! _retrieve_cache $comp_cachename; then
local cache_dir=$(brew --cache)
[[ -f $cache_dir/external_commands_list.txt ]] &&
list=( $(<$cache_dir/external_commands_list.txt) )
_store_cache $comp_cachename list
fi
_describe -t all-commands 'all commands' list
}
__brew_commands() {
_alternative \
'internal-commands:command:__brew_internal_commands' \
'external-commands:command:__brew_external_commands'
}
__brew_diagnostic_checks() {
local -a diagnostic_checks
diagnostic_checks=($(brew doctor --list-checks))
_describe -t diagnostic-checks 'diagnostic checks' diagnostic_checks
}
<%= completion_functions.join("\n") %>
# The main completion function
_brew() {
local curcontext="$curcontext" state state_descr line expl
local tmp ret=1
_arguments -C : \
'(-v)-v[verbose]' \
'1:command:->command' \
'*::options:->options' && return 0
case "$state" in
command)
# set default cache policy
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp ||
zstyle ":completion:${curcontext%:*}:*" cache-policy __brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" use-cache tmp ||
zstyle ":completion:${curcontext%:*}:*" use-cache true
__brew_commands && return 0
;;
options)
local command_or_alias command
local -A aliases
# expand alias e.g. ls -> list
command_or_alias="${line[1]}"
aliases=($(__brew_list_aliases))
command="${aliases[$command_or_alias]:-$command_or_alias}"
# change context to e.g. brew-list
curcontext="${curcontext%:*}-${command}:${curcontext##*:}"
# set default cache policy (we repeat this dance because the context
# service differs from above)
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp ||
zstyle ":completion:${curcontext%:*}:*" cache-policy __brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" use-cache tmp ||
zstyle ":completion:${curcontext%:*}:*" use-cache true
# call completion for named command e.g. _brew_list
local completion_func="_brew_${command//-/_}"
_call_function ret "${completion_func}" && return ret
_message "a completion function is not defined for command or alias: ${command_or_alias}"
return 1
;;
esac
}
_brew "$@"