-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnMan_completion.bash
140 lines (129 loc) · 2.48 KB
/
ConnMan_completion.bash
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
#!/usr/bin/env bash
main='ConnMan'
_ConnMan_completions()
{
mapfile -t COMPREPLY < <(compgen -W "$(__compListGen "${COMP_WORDS[@]}")" -- "${COMP_WORDS[$COMP_CWORD]}")
}
__compListGen() {
__tree | __subTree "$@" | __expandServices | __dropChildren
}
__subTree() {
# forward in to out if no args
(( $# == 0 )) && tee && return
# remove the last parameter if it's being completed
(( $# == COMP_CWORD + 1 )) && set -- "${@:1:$(($# - 1))}"
# process first entry and pipe recursively
one=$1
shift
__getNode "$one" < /dev/stdin | __subTree "$@"
}
__getNode() {
# all services match a common string in the heredoc
[[ "$(command connmanctl services)" =~ $1 ]] && set -- '@services'
# if first line matching $1 is followed by @repeat line,
# print only these two; if not, print only lines beginning
# with tab, up to the next line not beginning with tab
sed -E '/(^| )'"$1"'( |$)/,/^[^\t]/{
/^\t@repeat$/{H;x;p;Q}
h
/^\t/s/\t//p
}
d' /dev/stdin
}
__expandServices() {
input=$(< /dev/stdin)
case "$input" in
[^@]* )
echo "$input"
;;
@services* )
command connmanctl services | \
sed -E 's/^[^ ]* *([^ ].*[^ ]) *[^ ]*$/\1/
s/^ *//'
;;
# the default case is not needed, as this case
# statement is tightly bounded to the heredoc below
esac
}
__dropChildren() {
sed '/^\t/d' /dev/stdin
}
__tree() {
# The heredoc below hardcodes connmanctl's completion tree.
# The special entry @services refers to when connmanctl
# completes with the available services, whereas the
# entry @repeat refers to when connmanctl keeps giving
# the same alternative completions as the for the last
# word.
command cat <<EOF
$main
agent
on
off
clock
config
@services
autoconnect domains ipv4 ipv6 mdns nameservers proxy remove timeservers
@repeat
connect
@services
disable
ethernet
offline
wifi
disconnect
@services
enable
ethernet
offline
wifi
exit
help
monitor
manager
on
off
services
on
off
tech
on
off
vpnconnection
on
off
vpnmanager
on
off
move-after
@services
move-before
@services
peer_service
peers
quit
scan
ethernet
wifi
services
@services
session
bearers ctxid ifname srciprule type
@repeat
state
technologies
tether
ethernet
on
off
wifi
on
off
tethering_clients
vpnagent
on
off
vpnconnections
EOF
}
complete -o default -F _ConnMan_completions $main