[Koha-patches] [PATCH] Bug 10003: bash-completion for koha-list and some other script

Tomas Cohen Arazi tomascohen at gmail.com
Fri Aug 30 16:45:22 CEST 2013


This is an initial commit submitted for peer review. It implements bash-completion for the following commands:

 - koha-list
 - koha-enable
 - koha-disable
 - koha-email-enable
 - koha-email-disable
 - koha-enable-sip
 - koha-start-sip
 - koha-restart-sip
 - koha-stop-sip
 - koha-start-zebra
 - koha-stop-zebra
 - koha-restart-zebra

It is implemented in a way that it removes already used or mutually exclusive parameters (instance names, option switches).
I already have written completion for other (more complex) commands, But I belive a simpler patch is better to start with.

IMPORTANT: this patch relies on having the koha-list command available in the path. Also 10622 is needed for the SIP-related option switches.

To test:
 - Make sure you have bash-completion installed and enabled (IRC might help us if you encounter problems).
 - Apply the patch.
Option 1:
 - Pick the debian/koha-common.bash-completion file and do
   $ cp debian/koha-common.bash-completion /etc/bash_completion.d/koha-common
 - Open a new bash shell (I do it opening a new terminal on my Ubuntu box).
 - Type one of the listed commands...
  And repeatedly press <TAB>.
 - Enjoy, and signoff if you belive it is usable. Otherwise report back.
Option 2:
 - run:
   $ . debian/koha-common.bash-completion
 - Type one of the listed commands...
  And repeatedly press <TAB>.
 - Enjoy, and signoff if you belive it is usable. Otherwise report back.

Tests:
- Some koha-list option switches are mutually exclusive, -h should be available in any context
- koha-enable should only autocomplete disabled instances
- koha-disable should only autocomplete enabled instances
- koha-email-enable should only autocomplete email-disabled instances
- koha-email-disable should only autocomplete email-enabled instances
- koha-*-zebra scripts should only autocomplete enabled instances.
- koha-*-sip scripts should only autocomplete sip-enabled instances.

Regards
To+

Note: writing bash-completion routines is a bit hacky, I tried to make it the simplest way I could. Your comments are welcome.

Sponsored-by: Universidad Nacional de Cordoba
---
 debian/koha-common.bash-completion |  160 ++++++++++++++++++++++++++++++++++++
 debian/rules                       |    2 +-
 2 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 debian/koha-common.bash-completion

diff --git a/debian/koha-common.bash-completion b/debian/koha-common.bash-completion
new file mode 100644
index 0000000..cfaaffb
--- /dev/null
+++ b/debian/koha-common.bash-completion
@@ -0,0 +1,160 @@
+#!/bin/bash
+#
+# koha-common.bash-completion script for koha-* commands
+#
+# This file is part of Koha.
+#
+# Copyright 2013 Universidad Nacional de Cordoba
+#                Tomas Cohen Arazi
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, see <http://www.gnu.org/licenses>.
+
+_build_substract_switches()
+{
+    local substract
+
+    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
+        if [[ ${COMP_WORDS[i]} == -* ]]; then
+            substract="$substract -e ${COMP_WORDS[i]}"
+        fi
+    done
+
+    echo "$substract"
+}
+
+_koha_list_cmd()
+{
+    local filter=$1
+
+    local cur substract instancelist
+    _get_comp_words_by_ref cur
+
+    # Build a list of the already used words
+    substract=`_build_substract_switches`
+
+    if [[ "$substract" != "" ]]; then
+        instancelist=$( koha-list $filter | grep -v -x $substract )
+    else
+        instancelist=$( koha-list $filer )
+    fi
+
+    COMPREPLY=( $(compgen -W "$instancelist" -- $cur ) )
+}
+
+_koha_email_disable()
+{
+    _koha_list_cmd "--email"
+    return 0
+}
+complete -F _koha_email_disable koha-email-disable
+
+_koha_email_enable()
+{
+    _koha_list_cmd "--noemail"
+    return 0
+}
+complete -F _koha_email_enable koha-email-enable
+
+_koha_sip_enabled_instances()
+{
+    _koha_list_cmd "--sip"
+    return 0
+}
+
+# koha-*-sip autocomplete with sip-enabled instances
+complete -F _koha_sip_enabled_instances koha-start-sip
+complete -F _koha_sip_enabled_instances koha-restart-sip
+complete -F _koha_sip_enabled_instances koha-stop-sip
+
+_koha_sip_disabled()
+{
+    _koha_list_cmd "--nosip"
+    return 0
+}
+
+# koha-enable-sip autocompletes with sip-disabled instances
+complete -F _koha_sip_disabled koha-enable-sip
+
+_koha_disabled_instances()
+{
+    _koha_list_cmd "--disabled"
+    return 0
+}
+
+_koha_enabled_instances()
+{
+    _koha_list_cmd "--enabled"
+    return 0
+}
+
+# koha-enable autocompletes with disabled instances
+complete -F _koha_disabled_instances koha-enable
+
+# koha-disable autocompletes with enabled instances
+complete -F _koha_enabled_instances koha-disable
+
+# koha-*-zebra autocomplete with enabled instances
+complete -F _koha_enabled_instances koha-start-zebra
+complete -F _koha_enabled_instances koha-restart-zebra
+complete -F _koha_enabled_instances koha-stop-zebra
+
+_koha_list()
+{
+    local cur opts substract
+
+    COMPREPLY=()
+    _get_comp_words_by_ref cur
+    opts="--enabled --disabled --email --noemail --sip --nosip --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
+                --disabled)
+                    substract="$substract -e --enabled"; ;;
+                --enabled)
+                    substract="$substract -e --disabled"; ;;
+                --email)
+                    substract="$substract -e --noemail"; ;;
+                --noemail)
+                    substract="$substract -e --email"; ;;
+                --sip)
+                    substract="$substract -e --nosip"; ;;
+                --nosip)
+                    substract="$substract -e --sip"; ;;
+                --help)
+                    substract="$substract -e -h"; ;;
+                -h)
+                    substract="$substract -e --help"; ;;
+            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_list koha-list
+
+# Local variables:
+# mode: shell-script
+# sh-basic-offset: 4
+# sh-indent-comment: t
+# indent-tabs-mode: nil
+# End:
+# ex: ts=4 sw=4 et filetype=sh
diff --git a/debian/rules b/debian/rules
index cbcfaaf..ed1d34d 100755
--- a/debian/rules
+++ b/debian/rules
@@ -6,7 +6,7 @@ TMP = $(CURDIR)/debian/tmp
 export PERL_MM_USE_DEFAULT=1
 
 %:
-	dh $@ --fail-missing
+	dh $@ --fail-missing --with bash-completion
 
 override_dh_gencontrol:
 	debian/bd-to-depends >> debian/koha-common.substvars
-- 
1.7.9.5



More information about the Koha-patches mailing list