summaryrefslogtreecommitdiff
path: root/shell/bash-completion
blob: 72337058f0f196b89e940f3aa18673e2763cb1de (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
#!/bin/bash

in_array() {
  for _ in "${@:2}"; do
    [[ $_ = "$1" ]] && return 0
  done
  return 1
}

_asp() {
  local verb='' i cur prev comps

  _get_comp_words_by_ref cur prev

  # top level commands
  local -A verbs=(
    [ALL_PACKAGES]='checkout difflog export list-arches list-repos log shortlog show ls-files'
    [LOCAL_PACKAGES]='untrack update'
    [NONE]='disk-usage gc help list-all list-local'
  )

  # flags
  local -A opts=(
    [UNKNOWN]='-a'
    [NONE]='-f -h -V'
  )

  if in_array "$prev" ${opts[UNKNOWN]}; then
    return 0
  fi

  if [[ $cur = -* ]]; then
    COMPREPLY=( $(compgen -W '${opts[*]}' -- "$cur") )
    return 0
  fi

  # verb completion
  for (( i = 0; i < ${#COMP_WORDS[@]}; ++i )); do
    word=${COMP_WORDS[i]}
    if in_array "$word" ${verbs[ALL_PACKAGES]}; then
      verb=$word
      comps=$(\asp list-all | sed 's,.*/,,')
      break
    elif in_array "$word" ${verbs[LOCAL_PACKAGES]}; then
      verb=$word
      comps=$(\asp list-local | sed 's,.*/,,')
      break
    elif in_array "$word" ${verbs[NONE]}; then
      verb=$word
      break
    fi
  done

  # sub-verb completion
  case $verb in
    show)
      if (( i < ${#COMP_WORDS[@]} - 2 )); then
        comps=$(\asp ls-files "${COMP_WORDS[i+1]}" 2>/dev/null)
      fi
      ;;
    '')
      comps=${verbs[*]}
      ;;
  esac

  if [[ $comps ]]; then
    COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
  fi
}

complete -F _asp asp