forked from gregersrygg/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwest-completion.sh
More file actions
845 lines (753 loc) · 17.3 KB
/
Copy pathwest-completion.sh
File metadata and controls
845 lines (753 loc) · 17.3 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# Bash auto-completion for west subcommands and flags. To initialize, run
#
# source west-completion.bash
#
# To make it persistent, add it to e.g. your .bashrc.
setopt extended_glob
# The following function is based on code from:
#
# bash_completion - programmable completion functions for bash 3.2+
#
# Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
# © 2009-2010, Bash Completion Maintainers
# <bash-completion-devel@lists.alioth.debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# The latest version of this software can be obtained here:
#
# http://bash-completion.alioth.debian.org/
#
# RELEASE: 2.x
# This function can be used to access a tokenized list of words
# on the command line:
#
# __git_reassemble_comp_words_by_ref '=:'
# if test "${words_[cword_-1]}" = -w
# then
# ...
# fi
#
# The argument should be a collection of characters from the list of
# word completion separators (COMP_WORDBREAKS) to treat as ordinary
# characters.
#
# This is roughly equivalent to going back in time and setting
# COMP_WORDBREAKS to exclude those characters. The intent is to
# make option types like --date=<type> and <rev>:<path> easy to
# recognize by treating each shell word as a single token.
#
# It is best not to set COMP_WORDBREAKS directly because the value is
# shared with other completion scripts. By the time the completion
# function gets called, COMP_WORDS has already been populated so local
# changes to COMP_WORDBREAKS have no effect.
#
# Output: words_, cword_, cur_.
__west_reassemble_comp_words_by_ref()
{
local exclude i j first
# Which word separators to exclude?
exclude="${1//[^$COMP_WORDBREAKS]}"
cword_=$COMP_CWORD
if [ -z "$exclude" ]; then
words_=("${COMP_WORDS[@]}")
return
fi
# List of word completion separators has shrunk;
# re-assemble words to complete.
for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
# Append each nonempty word consisting of just
# word separator characters to the current word.
first=t
while
[ $i -gt 0 ] &&
[ -n "${COMP_WORDS[$i]}" ] &&
# word consists of excluded word separators
[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
do
# Attach to the previous token,
# unless the previous token is the command name.
if [ $j -ge 2 ] && [ -n "$first" ]; then
((j--))
fi
first=
words_[$j]=${words_[j]}${COMP_WORDS[i]}
if [ $i = $COMP_CWORD ]; then
cword_=$j
fi
if (($i < ${#COMP_WORDS[@]} - 1)); then
((i++))
else
# Done.
return
fi
done
words_[$j]=${words_[j]}${COMP_WORDS[i]}
if [ $i = $COMP_CWORD ]; then
cword_=$j
fi
done
}
if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
_get_comp_words_by_ref ()
{
local exclude cur_ words_ cword_
if [ "$1" = "-n" ]; then
exclude=$2
shift 2
fi
__west_reassemble_comp_words_by_ref "$exclude"
cur_=${words_[cword_]}
while [ $# -gt 0 ]; do
case "$1" in
cur)
cur=$cur_
;;
prev)
prev=${words_[$cword_-1]}
;;
words)
words=("${words_[@]}")
;;
cword)
cword=$cword_
;;
esac
shift
done
}
fi
if ! type _tilde >/dev/null 2>&1; then
# Perform tilde (~) completion
# @return True (0) if completion needs further processing,
# False (> 0) if tilde is followed by a valid username, completions
# are put in COMPREPLY and no further processing is necessary.
_tilde()
{
local result=0
if [[ $1 == \~* && $1 != */* ]]; then
# Try generate ~username completions
echo Generate username completions
COMPREPLY=( $( compgen -P '~' -u -- "${1#\~}" ) )
result=${#COMPREPLY[@]}
# 2>/dev/null for direct invocation, e.g. in the _tilde unit test
[[ $result -gt 0 ]] && compopt -o filenames 2>/dev/null
fi
return $result
}
fi
if ! type _quote_readline_by_ref >/dev/null 2>&1; then
# This function quotes the argument in a way so that readline dequoting
# results in the original argument. This is necessary for at least
# `compgen' which requires its arguments quoted/escaped:
#
# $ ls "a'b/"
# c
# $ compgen -f "a'b/" # Wrong, doesn't return output
# $ compgen -f "a\'b/" # Good
# a\'b/c
#
# See also:
# - http://lists.gnu.org/archive/html/bug-bash/2009-03/msg00155.html
# - http://www.mail-archive.com/bash-completion-devel@lists.alioth.\
# debian.org/msg01944.html
# @param $1 Argument to quote
# @param $2 Name of variable to return result to
_quote_readline_by_ref()
{
if [ -z "$1" ]; then
# avoid quoting if empty
printf -v $2 %s "$1"
elif [[ $1 == \'* ]]; then
# Leave out first character
printf -v $2 %s "${1:1}"
elif [[ $1 == \~* ]]; then
# avoid escaping first ~
printf -v $2 \~%q "${1:1}"
else
printf -v $2 %q "$1"
fi
# Replace double escaping ( \\ ) by single ( \ )
# This happens always when argument is already escaped at cmdline,
# and passed to this function as e.g.: file\ with\ spaces
[[ ${!2} == *\\* ]] && printf -v $2 %s "${1//\\\\/\\}"
# If result becomes quoted like this: $'string', re-evaluate in order to
# drop the additional quoting. See also: http://www.mail-archive.com/
# bash-completion-devel@lists.alioth.debian.org/msg01942.html
[[ ${!2} == \$* ]] && eval $2=${!2}
} # _quote_readline_by_ref()
fi
# This function turns on "-o filenames" behavior dynamically. It is present
# for bash < 4 reasons. See http://bugs.debian.org/272660#64 for info about
# the bash < 4 compgen hack.
_compopt_o_filenames()
{
# We test for compopt availability first because directly invoking it on
# bash < 4 at this point may cause terminal echo to be turned off for some
# reason, see https://bugzilla.redhat.com/653669 for more info.
type compopt &>/dev/null && compopt -o filenames 2>/dev/null || \
compgen -f /non-existing-dir/ >/dev/null
}
if ! type _filedir >/dev/null 2>&1; then
# This function performs file and directory completion. It's better than
# simply using 'compgen -f', because it honours spaces in filenames.
# @param $1 If `-d', complete only on directories. Otherwise filter/pick only
# completions with `.$1' and the uppercase version of it as file
# extension.
#
_filedir()
{
local IFS=$'\n'
_tilde "$cur" || return
local -a toks
local x tmp
x=$( compgen -d -- "$cur" ) &&
while read -r tmp; do
toks+=( "$tmp" )
done <<< "$x"
if [[ "$1" != -d ]]; then
local quoted
_quote_readline_by_ref "$cur" quoted
# Munge xspec to contain uppercase version too
# http://thread.gmane.org/gmane.comp.shells.bash.bugs/15294/focus=15306
local xspec=${1:+"!*.@($1|${1^^})"}
x=$( compgen -f -X "$xspec" -- $quoted ) &&
while read -r tmp; do
toks+=( "$tmp" )
done <<< "$x"
# Try without filter if it failed to produce anything and configured to
[[ -n ${COMP_FILEDIR_FALLBACK:-} && -n "$1" && ${#toks[@]} -lt 1 ]] && \
x=$( compgen -f -- $quoted ) &&
while read -r tmp; do
toks+=( "$tmp" )
done <<< "$x"
fi
if [[ ${#toks[@]} -ne 0 ]]; then
# 2>/dev/null for direct invocation, e.g. in the _filedir unit test
_compopt_o_filenames
COMPREPLY+=( "${toks[@]}" )
fi
} # _filedir()
fi
# Misc helpers taken from Docker:
# https://github.com/docker/docker-ce/blob/master/components/cli/contrib/completion/bash/docker
# __west_pos_first_nonflag finds the position of the first word that is neither
# option nor an option's argument. If there are options that require arguments,
# you should pass a glob describing those options, e.g. "--option1|-o|--option2"
# Use this function to restrict completions to exact positions after the argument list.
__west_pos_first_nonflag()
{
local argument_flags=$1
local counter=$((${subcommand_pos:-${command_pos}} + 1))
while [ "$counter" -le "$cword" ]; do
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
(( counter++ ))
# eat "=" in case of --option=arg syntax
[ "${words[$counter]}" = "=" ] && (( counter++ ))
else
case "${words[$counter]}" in
-*)
;;
*)
break
;;
esac
fi
# Bash splits words at "=", retaining "=" as a word, examples:
# "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
while [ "${words[$counter + 1]}" = "=" ] ; do
counter=$(( counter + 2))
done
(( counter++ ))
done
echo $counter
}
# __west_map_key_of_current_option returns `key` if we are currently completing the
# value of a map option (`key=value`) which matches the extglob given as an argument.
# This function is needed for key-specific completions.
__west_map_key_of_current_option()
{
local glob="$1"
local key glob_pos
if [ "$cur" = "=" ] ; then # key= case
key="$prev"
glob_pos=$((cword - 2))
elif [[ $cur == *=* ]] ; then # key=value case (OSX)
key=${cur%=*}
glob_pos=$((cword - 1))
elif [ "$prev" = "=" ] ; then
key=${words[$cword - 2]} # key=value case
glob_pos=$((cword - 3))
else
return
fi
[ "${words[$glob_pos]}" = "=" ] && ((glob_pos--)) # --option=key=value syntax
[[ ${words[$glob_pos]} == @($glob) ]] && echo "$key"
}
# __west_value_of_option returns the value of the first option matching `option_glob`.
# Valid values for `option_glob` are option names like `--log-level` and globs like
# `--log-level|-l`
# Only positions between the command and the current word are considered.
__west_value_of_option()
{
local option_extglob=$(__west_to_extglob "$1")
local counter=$((command_pos + 1))
while [ "$counter" -lt "$cword" ]; do
case ${words[$counter]} in
$option_extglob )
echo "${words[$counter + 1]}"
break
;;
esac
(( counter++ ))
done
}
# __west_to_alternatives transforms a multiline list of strings into a single line
# string with the words separated by `|`.
# This is used to prepare arguments to __west_pos_first_nonflag().
__west_to_alternatives()
{
local parts=( $1 )
local IFS='|'
echo "${parts[*]}"
}
# __west_to_extglob transforms a multiline list of options into an extglob pattern
# suitable for use in case statements.
__west_to_extglob()
{
local extglob=$( __west_to_alternatives "$1" )
echo "@($extglob)"
}
__set_comp_dirs()
{
_filedir -d
}
__set_comp_files()
{
_filedir
}
# Sets completions for $cur, from the possibilities in $1..n
__set_comp()
{
# "${*:1}" gives a single argument with arguments $1..n
COMPREPLY=($(compgen -W "${*:1}" -- "$cur"))
}
__west_x()
{
west 2>/dev/null "$@"
}
__set_comp_west_projs()
{
__set_comp "$(__west_x list --format={name} "$@")"
}
__set_comp_west_boards()
{
__set_comp "$(__west_x boards --format={name} "$@")"
}
__comp_west_west()
{
case "$prev" in
--zephyr-base|-z)
__set_comp_dirs
return
;;
# We don't know how to autocomplete any others
$(__west_to_extglob "$global_args_opts") )
return
;;
esac
case "$cur" in
-*)
__set_comp $global_bool_opts $global_args_opts
;;
*)
local counter=$( __west_pos_first_nonflag "$(__west_to_extglob "$global_args_opts")" )
if [ "$cword" -eq "$counter" ]; then
__set_comp ${cmds[*]}
fi
;;
esac
}
__comp_west_init()
{
local init_args_opts="
--manifest -m
--manifest-rev --mr
--local -l
"
case "$prev" in
--local|-l)
__set_comp_dirs
return
;;
esac
case "$cur" in
-*)
__set_comp $init_args_opts
;;
esac
}
__comp_west_update()
{
local update_bool_opts="
--keep-descendants -k
--rebase -r
"
case "$cur" in
-*)
__set_comp $update_bool_opts
;;
*)
__set_comp_west_projs
;;
esac
}
__comp_west_list()
{
local list_args_opts="
--format -f
"
case "$prev" in
# We don't know how to autocomplete those
$(__west_to_extglob "$list_args_opts") )
return
;;
esac
case "$cur" in
-*)
__set_comp $list_args_opts
;;
*)
__set_comp_west_projs
;;
esac
}
__comp_west_manifest()
{
local manifest_bool_opts="
--freeze
"
local manifest_args_opts="
--out -o
"
case "$prev" in
--out|-o)
__set_comp_files
return
;;
esac
case "$cur" in
-*)
__set_comp $manifest_bool_opts $manifest_args_opts
;;
esac
}
__comp_west_diff()
{
case "$cur" in
*)
__set_comp_west_projs
;;
esac
}
__comp_west_status()
{
case "$cur" in
*)
__set_comp_west_projs
;;
esac
}
__comp_west_forall()
{
local forall_args_opts="
-c
"
case "$prev" in
# We don't know how to autocomplete those
$(__west_to_extglob "$forall_args_opts") )
return
;;
esac
case "$cur" in
-*)
__set_comp $forall_args_opts
;;
*)
__set_comp_west_projs
;;
esac
}
__comp_west_config()
{
local config_bool_opts="
--global
--local
--system
"
case "$cur" in
-*)
__set_comp $config_bool_opts
;;
esac
}
__comp_west_help()
{
case "$cur" in
*)
local counter=$( __west_pos_first_nonflag "$(__west_to_extglob "$global_args_opts")" )
if [ "$cword" -eq "$counter" ]; then
__set_comp ${cmds[*]}
fi
;;
esac
}
# Zephyr extension commands
__comp_west_completion()
{
case "$cur" in
*)
local counter=$( __west_pos_first_nonflag "$(__west_to_extglob "$global_args_opts")" )
if [ "$cword" -eq "$counter" ]; then
__set_comp "bash"
fi
;;
esac
}
__comp_west_boards()
{
local boards_args_opts="
--format -f --name -n
--arch-root --board-root
"
case "$prev" in
--format|-f|--name|-n)
# We don't know how to autocomplete these.
return
;;
--arch-root)
__set_comp_dirs
return
;;
--board-root)
__set_comp_dirs
return
;;
esac
case "$cur" in
-*)
__set_comp $boards_args_opts
;;
esac
}
__comp_west_build()
{
local build_bool_opts="
--cmake -c
--cmake-only
-n --just-print --dry-run --recon
--force -f
"
local build_args_opts="
--board -b
--build-dir -d
--target -t
--pristine -p
--build-opt -o
"
case "$prev" in
--board|-b)
__set_comp_west_boards
return
;;
--build-dir|-d)
__set_comp_dirs
return
;;
--pristine|-p)
__set_comp "auto always never"
return
;;
# We don't know how to autocomplete those
$(__west_to_extglob "$build_args_opts") )
return
;;
esac
case "$cur" in
-*)
__set_comp $build_bool_opts $build_args_opts
;;
*)
__set_comp_dirs
;;
esac
}
__comp_west_sign()
{
local sign_bool_opts="
--force -f
--bin --no-bin
--hex --no-hex
"
local sign_args_opts="
--build-dir -d
--tool -t
--tool-path -p
-B --sbin
-H --shex
"
case "$prev" in
--build-dir|-d|--tool-path|-p)
__set_comp_dirs
return
;;
--tool|-t)
__set_comp "imgtool"
return
;;
-B|--sbin|-H|--shex)
__set_comp_files
return
;;
esac
case "$cur" in
-*)
__set_comp $sign_bool_opts $sign_args_opts
;;
esac
}
__comp_west_runner_cmd()
{
# Common arguments for runners
local runner_bool_opts="
--context -H
--skip-rebuild
"
local runner_args_opts="
--build-dir -d
--cmake-cache -c
--runner -r
--board-dir
--elf-file
--hex-file
--bin-file
--gdb
--openocd
--openocd-search
"
case "$prev" in
--build-dir|-d|--cmake-cache|-c|--board-dir|--gdb|--openocd|--openocd-search)
__set_comp_dirs
return
;;
--elf-file|--hex-file|--bin-file)
__set_comp_files
return
;;
esac
case "$cur" in
-*)
__set_comp $runner_bool_opts $runner_args_opts
;;
esac
}
__comp_west_flash()
{
__comp_west_runner_cmd
}
__comp_west_debug()
{
__comp_west_runner_cmd
}
__comp_west_debugserver()
{
__comp_west_runner_cmd
}
__comp_west_attach()
{
__comp_west_runner_cmd
}
__comp_west()
{
setopt extended_glob
# Reset to default, to make sure compgen works properly
local IFS=$' \t\n'
local builtin_cmds=(
init
update
list
manifest
diff
status
forall
config
help
)
local zephyr_ext_cmds=(
completion
boards
build
sign
flash
debug
debugserver
attach
zephyr-export
)
local cmds=(${builtin_cmds[*]} ${zephyr_ext_cmds[*]})
# Global options for all commands
local global_bool_opts="
--help -h
--verbose -v
--version -V
"
local global_args_opts="
--zephyr-base -z
"
COMPREPLY=()
local cur words cword prev
_get_comp_words_by_ref -n : cur words cword prev
local command='west' command_pos=0
local counter=1
while [ "$counter" -lt "$cword" ]; do
case "${words[$counter]}" in
west)
return 0
;;
$(__west_to_extglob "$global_args_opts") )
(( counter++ ))
;;
-*)
;;
=)
(( counter++ ))
;;
*)
command="${words[$counter]}"
command_pos=$counter
break
;;
esac
(( counter++ ))
done
# Construct the function name to be called
local completions_func=__comp_west_${command//-/_}
#echo "comp_func: ${completions_func}"
declare -F $completions_func >/dev/null && $completions_func
# Restore the user's extglob setting
unsetopt extended_glob
return 0
}
unsetopt extended_glob
complete -F __comp_west west