aboutsummaryrefslogtreecommitdiff
path: root/lib/keyringer/completions/bash
blob: c91a8ef3dfe6b2cf10e7c311c7418d0e23f5c5ba (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
#
# Keyringer bash completion
#

_keyringer() {
  # Standard stuff
  local cur prev opts config
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  # Initial options
  config="$HOME/.keyringer"
  opts="`ls $config | sed -e 's/config//'`"

  # Available instances
  instances="`echo $opts | sed -e 's/ /|/'`"

  # The current instance
  instance="${COMP_WORDS[1]}"

  # Command completions
  if [ "${#COMP_WORDS[@]}" == "3" ] && echo "${prev}" | grep -qe "[$instances]"; then
    opts="`keyringer $instance commands`"
  elif [ "${#COMP_WORDS[@]}" == "4" ]; then
    # Process config
    source $config/config
    path="`eval echo '$'$instance`"

    case "${prev}" in
      options|preferences)
        opts="ls edit add"
        ;;
      recipients)
        opts="ls edit"
        ;;
      ls)
        # TODO
        if [ -z "${cur}" ]; then
          opts="`keyringer $instance ls -p`"
        else
          local append
          opts="`keyringer $instance ls -p -d ${cur}* 2> /dev/null`"
        fi
        ;;
      *)
        ;;
    esac
  fi

  # Return the available options
  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  [[ $COMPREPLY == */ ]] && compopt -o nospace
  return 0
}

complete -F _keyringer keyringer