Bug 18562: Tab completion in bash
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 30 Jan 2019 12:12:36 +0000 (09:12 -0300)
committerroot <root@f1ebe1bec408>
Mon, 11 Feb 2019 18:30:10 +0000 (18:30 +0000)
This patch adds the fancy tab completion in bash.

To test:
- Run:
  $ source debian/koha-common.bash-completion
  $ sudo koha-sip <tab>
- Play with all the options
=> SUCCESS: The combinations make sense (e.g. only SIP-enabled instances
  are completed for --start, --stop, --restart, -v and --verbose are not
  offered if the other one is already in there, --enable only applies to
  SIP-disabled instances, etc).
- Sign off :-D

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
debian/koha-common.bash-completion

index 276cba0..56b428d 100644 (file)
@@ -303,6 +303,44 @@ _koha-zebra()
 }
 complete -F _koha-zebra koha-zebra
 
+_koha-sip()
+{
+    local cur opts substract
+
+    COMPREPLY=()
+    _get_comp_words_by_ref cur
+    opts="--start --stop --restart --status --enable --verbose -v --help -h"
+
+    # Build a list of the already used option switches
+    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
+        if [[ ${COMP_WORDS[i]} == -* ]]; then
+            case ${COMP_WORDS[i]} in
+                --start) _koha_sip_enabled_instances ; return 0 ;;
+                 --stop) _koha_sip_enabled_instances ; return 0 ;;
+              --restart) _koha_sip_enabled_instances ; return 0 ;;
+               --status) _koha_sip_enabled_instances ; return 0 ;;
+               --enable) _koha_sip_disabled ; return 0 ;;
+                 --help) COMPREPLY=() ; return 0 ;; # no more completions
+                     -h) COMPREPLY=() ; return 0 ;; # no more completions
+              --verbose) # filter the other quiet switches and go on
+                    substract="$substract -e -q"; ;;
+                -v)      # filter the other quiet switches and go on
+                    substract="$substract -e --verbose"; ;;
+            esac
+            substract="$substract -e ${COMP_WORDS[i]}"
+        fi
+    done
+
+    if [[ "$substract" != "" ]]; then
+        opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
+    fi
+
+    COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
+
+    return 0
+}
+complete -F _koha-sip koha-sip
+
 # Local variables:
 # mode: shell-script
 # sh-basic-offset: 4