aboutsummaryrefslogtreecommitdiff
blob: 77b9fd66c60c39d4233ab3ea24d55eaa3642a7de (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
#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 eselect_list
  local idx descr
  local arr_items_selected=$2
  local arr_items_unselected=$3

  eselect_list="$(LANG=C COLUMNS=100 eselect --colour=no $1 list 2> /dev/null)"
  while IFS="" read -r ele ; do
    echo "$ele" | read idx descr
    idx=${idx#*\[}
    idx=${idx%\]*}
    if [[ "$idx" =~ '^[0-9]+$' ]]; then
      local stripped_descr="${descr% *\**}"
      if [[ "${stripped_descr}" != "${descr}" ]] ; then
        set -A $arr_items_selected ${(P)arr_items_selected} "$idx:${(q)descr}"
      else
        set -A $arr_items_unselected ${(P)arr_items_unselected} "$idx:${(q)descr}"
      fi
    fi
  done <<< "$eselect_list"
}

_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
  else
    _eselect_complete_action_generic ${actionname}
  fi
}

_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
}

_eselect_complete_action_generic() {
  local -a sel_items
  local -a unsel_items

  if (( $NORMARG + $(_eselect_action_index) == $CURRENT )) ; then
    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
  fi

  _nothing
}

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

  case "$(_eselect_get_action)" in
    ("read"|"unread"))
      local -a extra_items=('all:Read all news items')
      if [[ $(_eselect_get_action) == "read" ]] ; then
        extra_items=('new:Read unread news items (default)' "${extra_items[@]}")
      fi
      _eselect_parse_action_list news items items

      items=(${(q)extra_items[@]} ${(Oa)items})
      if [[ -n "${items[@]}" ]] ; then
        _describe  -V 'eselect items' 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
}

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: