Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: bash_completion

Issue 3185003: Implement basic tab-completion for cros_workon arguments. (Closed) Base URL: http://src.chromium.org/git/crosutils.git
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 # Add programmable completion to some Chromium OS build scripts 5 # Add programmable completion to some Chromium OS build scripts
6 6
7 7
8 # Echo a list of -- flags that the current command accepts. The 8 # Echo a list of -- flags that the current command accepts. The
9 # function assumes that the command supports shflags' --help flag. 9 # function assumes that the command supports shflags' --help flag.
10 # 10 _flags() {
11 _flags() 11 echo $(command "${COMP_WORDS[0]}" --help 2>&1 \
12 { 12 | egrep -o -- --\[^\ \]+: \
13 echo $(command "${COMP_WORDS[0]}" --help 2>&1 | \ 13 | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/')
14 egrep -o -- --\[^\ \]+: | \
15 sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/')
16 } 14 }
17 15
18 16
19 # Complete flags, i.e., current words starting with --. Return 1 if 17 # Complete flags, i.e., current words starting with --. Return 1 if
20 # the current word doesn't start with --, 0 otherwise. 18 # the current word doesn't start with --, 0 otherwise.
21 # 19 _flag_complete() {
22 _flag_complete()
23 {
24 COMPREPLY=() 20 COMPREPLY=()
25 local cur="${COMP_WORDS[COMP_CWORD]}" 21 local cur="${COMP_WORDS[COMP_CWORD]}"
26 if [[ "${cur}" == --* ]]; then 22 if [[ "${cur}" == --* ]]; then
27 COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) ) 23 COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) )
28 return 0 24 return 0
29 fi 25 fi
30 return 1 26 return 1
31 } 27 }
32 28
33 29
34 # Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the 30 # Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
35 # word to be completed. If found, echo "--arg=foo". 31 # word to be completed. If found, echo "--arg=foo".
36 # 32 _argeq() {
37 _argeq()
38 {
39 local arg=$1 33 local arg=$1
40 local w0="${COMP_WORDS[COMP_CWORD]}" 34 local w0="${COMP_WORDS[COMP_CWORD]}"
41 local w1="${COMP_WORDS[COMP_CWORD-1]}" 35 local w1="${COMP_WORDS[COMP_CWORD-1]}"
42 36
43 # Check for completing "--arg=" 37 # Check for completing "--arg="
44 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then 38 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
45 echo "${w1}${w0}" 39 echo "${w1}${w0}"
46 return 0 40 return 0
47 fi 41 fi
48 42
49 # Check for completing "--arg foo" 43 # Check for completing "--arg foo"
50 if [ "${w1}" == ${arg} ]; then 44 if [ "${w1}" == ${arg} ]; then
51 echo "${w1}=${w0}" 45 echo "${w1}=${w0}"
52 return 0 46 return 0
53 fi 47 fi
54 48
55 # Check for completing "--arg=foo" 49 # Check for completing "--arg=foo"
56 if [ ${COMP_CWORD} -gt 2 ]; then 50 if [ ${COMP_CWORD} -gt 2 ]; then
57 local w2="${COMP_WORDS[COMP_CWORD-2]}" 51 local w2="${COMP_WORDS[COMP_CWORD-2]}"
58 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then 52 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
59 echo "${w2}${w1}${w0}" 53 echo "${w2}${w1}${w0}"
60 return 0 54 return 0
61 fi 55 fi
62 fi 56 fi
63 } 57 }
64 58
65 59
66 # echo the existing target board sysroots 60 # echo the existing target board sysroots
67 # 61 _board_sysroots() {
68 _board_sysroots()
69 {
70 local builddir=/build 62 local builddir=/build
71 if [ -d ${builddir} ]; then 63 if [ -d ${builddir} ]; then
72 echo $(command ls "${builddir}") 64 echo $(command ls "${builddir}")
73 fi 65 fi
74 } 66 }
75 67
76 _complete_board_sysroot_flag() 68 _complete_board_sysroot_flag() {
77 {
78 COMPREPLY=() 69 COMPREPLY=()
79 local arg=$(_argeq --board) 70 local arg=$(_argeq --board)
80 if [[ ${arg} == --board=* ]]; then 71 if [[ ${arg} == --board=* ]]; then
81 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) ) 72 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
82 return 0 73 return 0
83 fi 74 fi
84 return 1 75 return 1
85 } 76 }
86 77
87 # Completion for --board= argument for existing board sysroots 78 # Completion for --board= argument for existing board sysroots
88 # 79 _board_sysroot() {
89 _board_sysroot()
90 {
91 _flag_complete && return 0 80 _flag_complete && return 0
92 _complete_board_sysroot_flag && return 0 81 _complete_board_sysroot_flag && return 0
93 } 82 }
94 83
95 complete -o bashdefault -o default -F _board_sysroot \
96 build_autotest.sh \
97 build_image \
98 build_packages \
99 image_to_usb.sh \
100 mod_image_for_test.sh
101
102
103 # echo the existing target board overlays 84 # echo the existing target board overlays
104 # 85 _board_overlays() {
105 _board_overlays()
106 {
107 local overlaydir=../overlays 86 local overlaydir=../overlays
108 if [ -d ${overlaydir} ]; then 87 if [ -d ${overlaydir} ]; then
109 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) 88 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,)
110 fi 89 fi
111 } 90 }
112 91
113 # Completion for --board= argument for existing board overlays 92 # Completion for --board= argument for existing board overlays
114 # 93 _board_overlay() {
115 _board_overlay()
116 {
117 _flag_complete && return 0 94 _flag_complete && return 0
118 95
119 COMPREPLY=() 96 COMPREPLY=()
120 local arg=$(_argeq --board) 97 local arg=$(_argeq --board)
121 if [[ ${arg} == --board=* ]]; then 98 if [[ ${arg} == --board=* ]]; then
122 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) 99 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
123 fi 100 fi
124 } 101 }
125 102
126 complete -o bashdefault -o default -F _board_overlay setup_board
127
128 # Completion for -c and -s argument for autotest script 103 # Completion for -c and -s argument for autotest script
129 _ls_autotest() { 104 _ls_autotest() {
130 local autotest_dir=../third_party/autotest/files 105 local autotest_dir=../third_party/autotest/files
131 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null |\ 106 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
132 sed s/"..\/third_party\/autotest\/files\/"//g 107 | sed s/"..\/third_party\/autotest\/files\/"//g
133 } 108 }
134 109
135 _autotest_complete() 110 _autotest_complete() {
136 {
137 _flag_complete && return 0 111 _flag_complete && return 0
138 112
139 local arg=$(_argeq -c) 113 local arg=$(_argeq -c)
140 if [[ ${arg} == -c=* ]]; then 114 if [[ ${arg} == -c=* ]]; then
141 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})")) 115 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
142 return 0 116 return 0
143 fi 117 fi
144 118
145 arg=$(_argeq -s) 119 arg=$(_argeq -s)
146 if [[ ${arg} == -s=* ]]; then 120 if [[ ${arg} == -s=* ]]; then
147 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})")) 121 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
148 return 0 122 return 0
149 fi 123 fi
150 124
151 _complete_board_sysroot_flag && return 0 125 _complete_board_sysroot_flag && return 0
152 } 126 }
153 127
128 # Complete the cros_workon <command> argument.
129 #
130 # TODO(petkov): We should probably extract the list of commands from
131 # cros_workon --help, just like we do for flags (see _flag_complete).
132 #
133 # TODO(petkov): Currently, this assumes that the command is the first
134 # argument. In practice, the command is the first non-flag
135 # argument. I.e., this should be fixed to support something like
136 # "./cros_workon --all list".
137 _cros_workon_command_complete() {
138 [ ${COMP_CWORD} -eq 1 ] || return 1
139 local command="${COMP_WORDS[1]}"
140 COMPREPLY=($(compgen -W "start stop list iterate" -- "$command"))
141 return 0
142 }
143
144 # Lists the workon (or live, if --all is passed in) ebuilds. Lists
145 # both the full names (e.g., chromeos-base/metrics) as well as just
146 # the ebuild names (e.g., metrics).
147 _cros_workon_list() {
148 local cros_workon="${COMP_WORDS[0]}"
149 ${cros_workon} list $1 | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
150 }
151
152 # Completes the current cros_workon argument assuming it's a
153 # package/ebuild name.
154 _cros_workon_package_complete() {
155 [ ${COMP_CWORD} -gt 1 ] || return 1
156 local package="${COMP_WORDS[COMP_CWORD]}"
157 local command="${COMP_WORDS[1]}"
158 # If "start", complete based on all workon packages.
159 if [[ ${command} == "start" ]]; then
160 COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
161 return 0
162 fi
163 # If "stop" or "iterate", complete based on all live packages.
164 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
165 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
166 return 0
167 fi
168 return 1
169 }
170
171 # Complete the cros_workon arguments.
172 _cros_workon() {
173 COMPREPLY=()
174 _cros_workon_command_complete && return 0
175 _flag_complete && return 0
176 _complete_board_sysroot_flag && return 0
177 _cros_workon_package_complete && return 0
178 return 0
179 }
180
181 complete -o bashdefault -o default -F _board_sysroot \
182 build_autotest.sh \
183 build_image \
184 build_packages \
185 image_to_usb.sh \
186 mod_image_for_test.sh
187 complete -o bashdefault -o default -F _board_overlay setup_board
154 complete -o bashdefault -o default -o nospace -F _autotest_complete autotest 188 complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
189 complete -o bashdefault -o default -F _cros_workon cros_workon
155 190
156 ### Local Variables: 191 ### Local Variables:
157 ### mode: shell-script 192 ### mode: shell-script
158 ### End: 193 ### End:
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698