aboutsummaryrefslogtreecommitdiff
blob: ce185a8ac9d81eb8d44f535cdd44b788d50683fe (plain)
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
#compdef eselect kernel-config profile-config rc-config

_eselect_parse_generic() {
  local -a mod_std mod_extra
  local -a eselect_args
  local mod_cur eselect_descr
  local mod descr etype

  eselect_args=($@)
  if [[ $1 == "modules" ]] ; then
    eselect_args=($1 usage)
  fi
  mod_cur="mod_extra"
  eselect_descr="$(LANG=C COLUMNS=100 eselect --colour=no ${eselect_args[@]} 2> /dev/null)"

  while IFS="" read -r helpdesc ; do
    case "$helpdesc" in
      ("Built-in modules:"|"Standard actions:"))
        mod_cur="mod_std"
        ;;
      ("Extra modules:"|"Extra actions:"))
        mod_cur="mod_extra"
        ;;
    esac

    if [[ "$helpdesc" =~ '^  [A-Za-z]' ]] ; then
      echo "$helpdesc" | read mod descr
      descr="$(echo "$descr" | sed -r -e 's/.*\s\s\s+//')"
      set -A $mod_cur ${(P)mod_cur} ${mod}:${(q)descr}
    fi
  done <<< "$eselect_descr"

  if [[ -z "${eselect_args[@]}" ]] ; then
    etype="modules"
  else
    etype="actions"
  fi

  if [[ -z "${mod_extra}" && -z "${mod_std}" ]] ; then
    _nothing
  else
    _describe -t eselect_extra    -V "eselect extra $etype"    mod_extra
    _describe -t eselect_standard -V "eselect standard $etype" mod_std
  fi
}

_eselect_parse_action_list() {
  local idx item tag
  local -a _sel_items
  local -a _unsel_items

  while read idx item tag descr ; do
    if [[ "$idx" == '[' && "$item" == ']' ]] ; then
      continue
    fi
    if [[ ${tag} =~ '^[*@#]$' ]] ; then
      _sel_items+=($item)
    else
      _unsel_items+=($item)
    fi
  done <<< $(LANG=C COLUMNS=100 eselect --colour=no $1 list 2> /dev/null | tail -n +2)

  set -A $2 ${_sel_items[@]}
  set -A $3 ${_unsel_items[@]}

}

_eselect_module_action() {
  if (( $+functions[_eselect_${1}_action] )) ; then
    _eselect_${1}_action
  else
    _eselect_parse_generic ${1}
  fi
}

_eselect_complete_action() {
  local actionname=$(_eselect_get_module)
  if (( $+functions[_eselect_complete_${actionname}_action] )) ; then
    _eselect_complete_${actionname}_action
    return 0
  fi

  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    _eselect_complete_action_generic ${actionname}
    return 0
  fi

  _nothing
}

_eselect_get_module() {
  if [[ $service == "eselect" ]] ; then
    echo $line[1]
  else
    echo ${service%-config}
  fi
}

_eselect_action_index() {
  if [[ $service == "eselect" ]] ; then
    echo 2
  else
    echo 1
  fi
}
_eselect_get_action () {
  echo ${line[$(_eselect_action_index)]}
}

_eselect_action() {
  _eselect_module_action $(_eselect_get_module)
}

_eselect_module() {
  _eselect_parse_generic
}

(( $+function[_eselect_complete_action_generic] )) || _eselect_complete_action_generic() {
  local -a sel_items
  local -a unsel_items

  case "$(_eselect_get_action)" in
    ("set"|"enable"))
      _eselect_parse_action_list $1 sel_items unsel_items
      if (( $#unsel_items + $#sel_items > 0 )) ; then
        _describe -t eselect_sel   -V 'eselect items' unsel_items
        if (( $#unsel_items + $#sel_items < 10 )) ; then
          _describe -t eselect_unsel -V 'eselect already selected items' sel_items
        fi
        return 0
      fi
      ;;
    ("remove"|"disable"))
      _eselect_parse_action_list $1 sel_items unsel_items
      if [[ -n "${sel_items[@]}" ]] ; then
        _describe  -V 'eselect items' sel_items
        return 0
      fi
      ;;
  esac

  _nothing
}

# custom completions:
(( $+functions[_eselect_complete_news_action] )) || _eselect_complete_news_action() {
  local -a items

  case "$(_eselect_get_action)" in
    ("read"|"unread"))
      local idx descr
      while read idx descr ; do
        idx=${idx#'['}
        idx=${idx%']'}
        items=(${idx}:${(q)descr} ${items[@]})
      done <<< $(eselect --colour=no news list 2> /dev/null | tail -n +2)
      items+=('all:Read all news items')
      if [[ $(_eselect_get_action) == "read" ]] ; then
        items+=('new:Read unread news items (default)')
      fi

      if [[ -n "${items[@]}" ]] ; then
        _describe  -V 'eselect news' items
        return 0
      fi
      ;;
  esac

  _nothing
}

(( $+functions[_eselect_complete_kernel_action] )) || _eselect_complete_kernel_action() {
  local -a items

  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    case "$(_eselect_get_action)" in
      "set")
        for k in ${(@f)$(eselect --colour=no --brief kernel list)} ; do
          items+=${k#linux-}
        done
        local -a expl=(-P linux-)
        compadd -V  'eselect kernel' -P linux- ${items[@]}
        return 0
        ;;
    esac
  fi

  _nothing
}

(( $+functions[_eselect_complete_profile_action] )) || _eselect_complete_profile_action() {
  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    case "$(_eselect_get_action)" in
      "set")
        _values  'eselect profiles' $(eselect --colour=no --brief profile list)
        return 0
        ;;
    esac
  fi

  _nothing
}

(( $+functions[_eselect_complete_timezone_action] )) || _eselect_complete_timezone_action() {
  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    case "$(_eselect_get_action)" in
      "set")
        _multi_parts /  "($(eselect --colour=no --brief timezone list))"
        return 0
        ;;
    esac
  fi

  _nothing
}

(( $+functions[_eselect_complete_php_action] )) || _eselect_complete_php_action() {
  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    case "$(_eselect_get_action)" in
      ("list"|"set"|"show"|"update"))
        _values 'eselect php modules' $(eselect --colour=no --brief php list-modules)
        return 0
        ;;
    esac
  elif (( $NORMARG + $(_eselect_action_index) + 1 == $CURRENT )) ; then
    case "$words[$CURRENT-2]" in
      "set")
        local targets=($(eselect --colour=no --brief php list "$words[$CURRENT-1]" 2> /dev/null))
        if [ -n "$targets" ]; then
          _values 'eselect php modules'  ${targets[@]}
          return 0
        fi
        ;;
    esac
  fi

  _nothing
}

(( $+functions[_eselect_complete_rc_action] )) || _eselect_complete_rc_action() {
  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    case "$(_eselect_get_action)" in
      ("list"|"show"))
        _values 'runlevels' $(command ls --color=never ${EPREFIX}/etc/runlevels)
        return 0
        ;;
      ("add"|"delete"|"pause"|"reload"|"restart"|"start"|"stop"))
        _values 'scripts' $(rc-service -l)
        return 0
        ;;
      esac
  elif (( $NORMARG + $(_eselect_action_index) + 1 == $CURRENT )) ; then
    case "$words[$CURRENT-2]" in
      ("add"|"delete"))
        _values 'runlevels' $(command ls --color=never ${EPREFIX}/etc/runlevels)
        return 0
        ;;
    esac
  fi

  _nothing
}


(( $+functions[_eselect_complete_repository_action] )) || _eselect_complete_repository_action() {
  local -a opts

  case "$(_eselect_get_action)" in
    ("disable"|"remove"))
      opts=('-f:Force potentially dangerous removals')
      ;;
    "list")
      opts=('-i:Only list installed')
      ;;
  esac

  _describe -o 'options' opts
  _eselect_complete_action_generic repository
}

eselect_comp() {
  integer NORMARG

  case "$service" in
    eselect)
      _arguments -A '-*' -n \
        '--brief[Make output shorter]' \
        '(--colour --color)'--colo{,u}r='[Enable or disable colour output]:colour:(yes no)' \
        '1:eselect_module:_eselect_module' \
        '2:eselect_action:_eselect_action' \
        '*:eselect_complete_action:_eselect_complete_action'
      ;;
    *-config)
      _arguments -n \
        "1:action:_eselect_action" \
        '*:eselect_complete_action:_eselect_complete_action'
      ;;
  esac
}

eselect_comp "$@"

# vim: set et sw=2 ts=2 ft=zsh: