Debian Codegraph @

etc/completion/bash/guix

raw
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2015-2022 Ludovic Courtès <[email protected]>
3 # Copyright © 2021 Tobias Geerinck-Rice <[email protected]>
4 #
5 # This file is part of GNU Guix.
6 #
7 # GNU Guix is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # GNU Guix is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 # Bash completion for Guix commands.
21
22 declare _guix_available_packages
23 declare _guix_commands
24
25 _guix_complete_command ()
26 {
27 local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
28 if [ -z "$_guix_commands" ]
29 then
30 # Cache the list of commands to speed things up.
31 _guix_commands="$(${COMP_WORDS[0]} --help 2> /dev/null \
32 | grep '^ ' \
33 | sed '-es/^ *\([a-z-]\+\).*$/\1/g')"
34 fi
35
36 COMPREPLY+=($(compgen -W "$_guix_commands" -- "$word_at_point"))
37 }
38
39 _guix_complete_subcommand ()
40 {
41 local command="${COMP_WORDS[1]}"
42 local subcommands="$(${COMP_WORDS[0]} $command --help 2> /dev/null \
43 | grep '^ [a-z]' \
44 | sed -e's/^ \+\([a-z-]\+\).*$/\1/g')"
45 COMPREPLY+=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
46 }
47
48 _guix_complete_available_package ()
49 {
50 local prefix="$1"
51 if [ -z "$_guix_available_packages" ]
52 then
53 # Cache the complete list because it rarely changes and makes
54 # completion much faster.
55 _guix_available_packages="$(${COMP_WORDS[0]} package -A 2> /dev/null \
56 | cut -f1)"
57 fi
58 COMPREPLY+=($(compgen -W "$_guix_available_packages" -- "$prefix"))
59 }
60
61 _guix_complete_installed_package ()
62 {
63 # Here we do not cache the list of installed packages because that
64 # may change over time and the list is relatively small anyway.
65 local prefix="$1"
66 local packages="$(${COMP_WORDS[0]} package -I "^$prefix" 2> /dev/null \
67 | cut -f1)"
68 COMPREPLY+=($(compgen -W "$packages" -- "$prefix"))
69 }
70
71 _guix_complete_option ()
72 {
73 local command="${COMP_WORDS[$1]}"
74 local subcommand="${COMP_WORDS[$(($1 + 1))]}"
75
76 if [ $1 -eq 0 ]
77 then
78 command=""
79 subcommand=""
80 elif _guix_is_option "$subcommand"
81 then
82 subcommand=""
83 fi
84
85 local options="$(${COMP_WORDS[0]} $command $subcommand --help 2> /dev/null \
86 | grep '^ \+-' \
87 | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g')"
88 compopt -o nospace
89 COMPREPLY+=($(compgen -W "$options" -- "$2"))
90 }
91
92 _guix_is_option ()
93 {
94 case "$1" in
95 -*)
96 true
97 ;;
98 *)
99 false
100 ;;
101 esac
102 }
103
104 _guix_is_removing ()
105 {
106 local word
107 local result="false"
108 for word in ${COMP_WORDS[*]}
109 do
110 case "$word" in
111 --remove|--remove=*|-r)
112 result=true
113 break
114 ;;
115 esac
116 done
117 $result
118 }
119
120 _guix_is_short_option ()
121 {
122 case "${COMP_WORDS[$COMP_CWORD - 1]}" in
123 --*) false;;
124 -*$1) true ;;
125 *) false ;;
126 esac
127 }
128
129 _guix_is_long_option ()
130 {
131 # Don't handle (non-GNU?) ‘--long-option VALUE’, as Guix doesn't either.
132 case "${COMP_WORDS[$COMP_CWORD]}" in
133 --$1=*) true ;;
134 *) false ;;
135 esac
136 }
137
138 _guix_is_dash_f ()
139 {
140 _guix_is_short_option f ||
141 _guix_is_long_option file ||
142 _guix_is_long_option install-from-file
143 }
144
145 _guix_is_dash_l ()
146 {
147 _guix_is_short_option l ||
148 _guix_is_long_option load
149 }
150
151 _guix_is_dash_L ()
152 {
153 _guix_is_short_option L ||
154 _guix_is_long_option load-path
155 }
156
157 _guix_is_dash_m ()
158 {
159 _guix_is_short_option m ||
160 _guix_is_long_option manifest
161 }
162
163 _guix_is_dash_C ()
164 {
165 _guix_is_short_option C ||
166 _guix_is_long_option channels
167 }
168
169 _guix_is_dash_p ()
170 {
171 _guix_is_short_option p ||
172 _guix_is_long_option profile
173 }
174
175 _guix_complete_file ()
176 {
177 # Let Readline complete file names.
178 compopt -o default
179 COMPREPLY=()
180 }
181
182 _guix_complete_available_package_or_store_file ()
183 {
184 _guix_complete_available_package "$@"
185
186 # The current _guix_complete_file implementation doesn't compose (append to
187 # COMPREPLY), so we suggest file names only if no package names matched.
188 if [[ -z "$COMPREPLY" ]]
189 then
190 _guix_complete_file # TODO: restrict to store files
191 fi
192 }
193
194 _guix_complete_pid ()
195 {
196 local pids="$(cd /proc; echo [0-9]*)"
197 COMPREPLY+=($(compgen -W "$pids" -- "$1"))
198 }
199
200 _guix_complete ()
201 {
202 local word_count=${#COMP_WORDS[*]}
203 local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
204
205 # Find the innermost command at point, e.g. "build" in the case of
206 # "guix time-machine OPTIONS -- build<Tab>" -- but "time-machine" if
207 # point is moved before "build".
208 local command_index=0
209 local command
210 local word_index=0
211 local word
212 local expect_command="true"
213 while [[ $((++word_index)) -le COMP_CWORD ]]
214 do
215 word="${COMP_WORDS[$word_index]}"
216 if $expect_command
217 then
218 command_index=$word_index
219 command="$word"
220 expect_command="false"
221 continue
222 fi
223 if [[ "$word" = "--" ]]
224 then
225 case "$command" in
226 environment|shell)
227 break
228 ;;
229 time-machine)
230 expect_command="true"
231 ;;
232 esac
233 fi
234 done
235
236 case $COMP_CWORD in
237 $command_index)
238 _guix_complete_command
239 _guix_complete_option 0 "$word_at_point"
240 ;;
241 *)
242 if [[ "$command" = "package" ]]
243 then
244 if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_f
245 then
246 _guix_complete_file
247 elif _guix_is_removing
248 then
249 _guix_complete_installed_package "$word_at_point"
250 else
251 _guix_complete_available_package "$word_at_point"
252 fi
253 elif [[ "$command" = "install" ]]
254 then
255 if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
256 then
257 _guix_complete_file
258 else
259 _guix_complete_available_package "$word_at_point"
260 fi
261 elif [[ "$command" = "upgrade" || "$command" = "remove" ]]
262 then
263 if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
264 then
265 _guix_complete_file
266 else
267 _guix_complete_installed_package "$word_at_point"
268 fi
269 elif [[ "$command" = "build" ]]
270 then
271 if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_f
272 then
273 _guix_complete_file
274 else
275 _guix_complete_available_package_or_store_file "$word_at_point"
276 fi
277 elif [[ "$command" = "environment" || "$command" = "shell" ]]
278 then
279 if _guix_is_dash_f && [[ "$command" = "shell" ]]
280 then
281 # The otherwise identical ‘guix environment’ lacks the ‘-f’ option.
282 _guix_complete_file
283 elif _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_l
284 then
285 _guix_complete_file
286 elif _guix_is_option "$word_at_point"
287 then
288 _guix_complete_option "$command_index" "$word_at_point"
289 else
290 _guix_complete_available_package "$word_at_point"
291 fi
292 elif [[ "$command" = "download" || "$command" = "gc" || "$command" = "hash" ]]
293 then
294 _guix_complete_file
295 elif [[ "$command" = "size" ]]
296 then
297 _guix_complete_available_package_or_store_file "$word_at_point"
298 elif [[ "$command" = "system" || "$command" = "home" ]]
299 then
300 case $((COMP_CWORD - command_index)) in
301 1) _guix_complete_subcommand;;
302 *) _guix_complete_file;; # TODO: restrict to *.scm
303 esac
304 elif [[ "$command" = "pull" ]]
305 then
306 if _guix_is_dash_C || _guix_is_dash_p
307 then
308 _guix_complete_file
309 fi
310 elif [[ "$command" = "time-machine" ]]
311 then
312 if _guix_is_dash_C
313 then
314 _guix_complete_file
315 else
316 _guix_complete_option "$command_index" "$word_at_point"
317 fi
318 elif [[ "$command" = "container" ]]
319 then
320 case $((COMP_CWORD - command_index)) in
321 1) _guix_complete_subcommand;;
322 2) _guix_complete_pid "$word_at_point";;
323 *) _guix_complete_file;;
324 esac
325 elif [[ "$command" = "import" ]]
326 then
327 _guix_complete_subcommand
328 elif [[ "$command" = "weather" ]]
329 then
330 if _guix_is_dash_m
331 then
332 _guix_complete_file
333 else
334 _guix_complete_available_package "$word_at_point"
335 fi
336 else
337 _guix_complete_available_package "$word_at_point"
338 fi
339 ;;
340 esac
341
342 if [[ -z "$COMPREPLY" && COMP_CWORD -gt command_index ]] &&
343 _guix_is_option "$word_at_point"
344 then
345 _guix_complete_option "$command_index" "$word_at_point"
346 fi
347 }
348
349 complete -F _guix_complete guix