File: midiwala

package info (click to toggle)
midiminder 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 844 kB
  • sloc: cpp: 11,511; makefile: 86; sh: 45
file content (149 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download
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
# bash completion for midiminder(1) & midiminder(8)        -*- shell-script -*-

# see apt's completion functions for the general structure followed here
# see bash-completion's _usergroup() function for handling client:port

# fixme: Doesn't handle port names with colons in all cases
# fixme: Doesn't handle client names with colons in them at all


_midiwala_complete_clients()
{
  local items
  readarray -t items < <( \
    midiwala list --ports --plain \
    | sed -e 's/:.*//' \
    | sort -u
  )

  local i item
  COMPREPLY=()
  for (( i=0; i < ${#items[@]}; i++ )); do
    item="${items[i]}"
    item="$(printf "%q" "$item")"
    if [[ "$item" =~ ^"$cur" ]]; then
      COMPREPLY+=("$item")
    fi
  done
}

_midiwala_complete_ports()
{
  local prefix
  prefix="$1"

  local items
  readarray -t items < <(midiwala list --ports --plain)

  local i item
  COMPREPLY=()
  for (( i=0; i < ${#items[@]}; i++ )); do
    item="${items[i]}"
    item="$(printf "%q" "$item")"
    if [[ "$item" == "$client:"* ]]; then
      item="${item#"$client:"}"
      if [[ $item == *:* ]]; then
        item="\\'$item\\'"
      fi
      if [[ $item == "$port"* ]]; then
        COMPREPLY+=("$prefix$item")
      fi
    fi
  done
}

_midiwala_ports()
{
  local client port
  if [[ $cur == *\\\\* || $cur == *:*:* ]]; then
    # Give up early on if something seems horribly wrong.
    return
  elif [[ $cur == *\\:* ]]; then
    # Completing port after 'client\:po<TAB>'.
    # Reply w/list of ports that include 'client\\:'
    client="${cur%%\\:*}"
    port="${cur#*\\:}"
    _midiwala_complete_ports "$client\\:"
  elif [[ $cur == *:* ]]; then
    # Completing port after 'client:po<TAB>'
    # Replly w/list of ports w/o 'client:'
    local client
    client="${cur%%:*}"
    port="${cur#*:}"
    _midiwala_complete_ports ""
  else
    # Completing client after 'cli<TAB>'
    # Do NOT suffix with :, as readline will escape it. Instead, the user
    # gets 'client<SPACE>'. They can backspace and add the : if they want.
    _midiwala_complete_clients "$cur"
  fi
}

_midiwala()
{
  local cur prev words cword;
  _init_completion -s -n : || return

  local GLOBAL_OPTIONS='
    -v --verbose
    -h --help
  '

  local COMMANDS=(
    "view"
    "list"        # [options]
    "connect"     # sender dest
    "disconnect"  # sender dest
    "remove-all"
    "help"
  )

  local ALT_COMMANDS=( "-l" "-c" "-d" "-x" )

  local LIST_OPTIONS='
    --clients
    -p --ports
    -c --connections
    -a --all
    -n --numeric
    --long-port-names
    --plain
    --details
  '

  # see if the user selected a command already
  local command i
  for (( i=1; i < ${#words[@]}; i++ )); do
      if [[ " ${COMMANDS[*]} ${ALT_COMMANDS[*]} " == *" ${words[i]} "* ]]; then
          command=${words[i]}
          break
      fi
  done

  # When the cursor (cword) is before the command word (i), only suggest
  # global options:
  if [[ ( $cur == -* && ( -z command || $cword -le $i ) ) ]]; then
    COMPREPLY=( $(compgen -W "$GLOBAL_OPTIONS" -- "$cur") )
    return 0;
  fi

  # arguments for a specific command
  if [[ -v command && $cword -gt $i ]]; then
    case ${command:-} in
      list|-l)
        COMPREPLY=( $(compgen -W "$LIST_OPTIONS" -- "$cur") )
        return 0;
        ;;
      connect|disconnect|-c|-d)
        _midiwala_ports
        ;;
      view|remove-all|-x|help)
        return 0;
        ;;
    esac
  else
    COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur") )
    return 0;
  fi
} &&
complete -F _midiwala midiwala