ref: refs/heads/master
commit 7cbd4def398ffb38732e0d935a2539e9fbe93ff6
Author: Werner Fink
Date: Mon Mar 2 13:26:41 2009 +0100
bash-completion: Allow empty spaces in repos names.
---
src/bash-completion.sh | 148 ++++++++++++++++++++++++++++++++++++------------
1 files changed, 111 insertions(+), 37 deletions(-)
diff --git a/src/bash-completion.sh b/src/bash-completion.sh
index b7259f9..8744efc 100644
--- a/src/bash-completion.sh
+++ b/src/bash-completion.sh
@@ -1,83 +1,157 @@
-# zypper completion v 0.2 aplha 2 :-)
+# zypper bash completion script
+#
# A hackweek gift from Marek Stopka
+# Major rewrite by Josef Reidinger
+# 2009/02/19 Allow empty spaces in repos names, Werner Fink
#
# some TODOs:
# - complete package names for install/remove/update
+_strip()
+{
+ local s c o
+ if test ${#COMPREPLY[@]} -gt 0 ; then
+ s="${COMP_WORDBREAKS// }"
+ s="${s// }"
+ s="${s//[\{\}()\[\]]}"
+ s="${s} (){}[]"
+ o=${#s}
+ while test $((o--)) -gt 0 ; do
+ c="${s:${o}:1}"
+ COMPREPLY=(${COMPREPLY[*]//${c}/\\${c}})
+ done
+ fi
+}
+
_zypper() {
ZYPPER_CMDLIST=()
- ZYPPER="/usr/bin/zypper -q"
+ ZYPPER="$(type -p zypper)"
+
+ local noglob=$(shopt -po noglob)
local magic_string="Command options:"
- local opts opts2 cur prev command ITER
- if test ${#ZYPPER_CMDLIST[*]} = 0; then
- for foo in $(LC_ALL=C $ZYPPER -h | sed -e "1,/Commands:/d" -e "/\t\t\t\t/d" | awk -F ' ' '{print $1} {print $2}' | sed -e 's/,//' -e 's/[[:upper:]].*//'); do
- ZYPPER_CMDLIST="${ZYPPER_CMDLIST} $foo"
- done
- ZYPPER_CMDLIST="${ZYPPER_CMDLIST} "
+ local comp cur prev command
+ local -a opts=()
+ local -i ITER=0
+ local IFS=$'\n'
+
+ # Do not expand `?' for help
+ set -o noglob
+
+ if test ${#ZYPPER_CMDLIST[@]} -eq 0; then
+ ZYPPER_CMDLIST=($(LC_ALL=POSIX $ZYPPER -q -h | \
+ sed -rn '/^[[:blank:]]*Commands:/,$ {
+ /[\t]{4}/d
+ s/^[[:blank:]]*//
+ s/^[[:upper:]].*://
+ s/[[:blank:]]+[[:upper:]].*$//
+ s/,[[:blank:]]+/\n/
+ s/\?/\\?/
+ /^$/d
+ p
+ }'))
fi
- cur=${COMP_WORDS[COMP_CWORD]}
+ if test $COMP_CWORD -lt 1 ; then
+ let COMP_CWORD=${#COMP_WORDS[@]}
+ fi
prev=${COMP_WORDS[COMP_CWORD-1]}
- eval "ITER=$COMP_CWORD-1"
- while [[ ${ITER} -ge 0 ]]; do
- opts2=${COMP_WORDS[ITER]}
- if [[ "${ZYPPER_CMDLIST}" =~ " ${opts2} " ]]; then
+ cur=${COMP_WORDS[COMP_CWORD]}
+
+ let ITER=COMP_CWORD
+ while test $((ITER--)) -ge 0 ; do
+ comp="${COMP_WORDS[ITER]}"
+ if [[ "${ZYPPER_CMDLIST[@]}" =~ "${comp}" ]]; then
command=${COMP_WORDS[ITER]}
break;
fi
- if [[ "${opts2}" =~ "zypper" ]]; then
-
+ if [[ "${comp}" =~ "zypper" ]]; then
command="zypper"
break;
fi
- eval "ITER=$ITER-1"
done
+ unset ITER comp
case "$prev" in
"--type" | "-t")
- opts="package patch pattern product"
- COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+ opts=(package patch pattern product)
+ COMPREPLY=($(compgen -W "${opts[*]}" -- ${cur}))
+ _strip
+ eval $noglob
return 0;
;;
"--repo" | "-r")
- opts="${opts} $(LC_ALL=C $ZYPPER lr | sed -e '1,2 d' -e 's/^[0-9]\+[[:space:]]\+|[[:space:]]*\([^|]\+\)|.*$/\1/' -e 's/[[:space:]]*$//')"
- COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+ opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q lr | \
+ sed -rn '/^[0-9]/{
+ s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
+ s/[[:blank:]]*$//
+ p
+ }'))
+ COMPREPLY=($(compgen -W "${opts[*]}" -- ${cur}))
+ _strip
+ eval $noglob
return 0;
;;
esac
+ unset prev
if [[ "$command" =~ "zypper" ]]; then
- opts=$ZYPPER_CMDLIST
- opts2=$(LC_ALL=C $ZYPPER help 2>&1 | sed -e "1,/Global Options:/d" -e "/Commands:/,$ d" -e 's/.*--/--/' -e 's/[,[:space:]].*//' -e '/^$/d')
- opts="${opts} ${opts2}"
- COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+ opts=(${ZYPPER_CMDLIST[*]}$(echo; LC_ALL=POSIX $ZYPPER -q help 2>&1 | \
+ sed -rn '/Global Options:/,/Commands:/{
+ /[\t]{4}/d
+ s/^[[:blank:]]*//
+ /[[:upper:]].*:/d
+ s/[[:blank:]]+[[:upper:]].*$//
+ s/[,[:blank:]].*$/\n/
+ /^$/d
+ p
+ }'))
+ COMPREPLY=($(compgen -W "${opts[*]}" -- ${cur}))
+ _strip
+ eval $noglob
return 0;
fi
- if [ -n "$command" ]; then
- opts=$(LC_ALL=C $ZYPPER help $command 2>&1 | sed -e "1,/$magic_string/d" -e 's/.*--/--/' -e 's/ .*//')
-
+ if test -n "$command" ; then
+ opts=$(LC_ALL=POSIX $ZYPPER -q help $command 2>&1 | sed -e "1,/$magic_string/d" -e 's/.*--/--/' -e 's/ .*//')
+
#handling individual commands if they need more then we can dig from help
case "$command" in
- help | ?)
- opts=$ZYPPER_CMDLIST
+ help | \?)
+ opts=(${ZYPPER_CMDLIST[@]})
;;
removerepo | rr | modifyrepo | mr | renamerepo | nr | refresh | ref)
- opts2=$(LC_ALL=C $ZYPPER lr | sed -e '1,2 d' -e 's/^[0-9]\+[[:space:]]\+|[[:space:]]*\([^|]\+\)|.*$/\1/' -e 's/[[:space:]]*$//' -e 's/ /\\ /g' -e "s/^\(.*\)$/'\1'/")
- opts="${opts} ${opts2}"
+ opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q lr | \
+ sed -rn '/^[0-9]/{
+ s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
+ s/[[:blank:]]*$//
+ /^$/d
+ p
+ }'))
;;
addservice | as | modifyservice | ms | removeservice | rs)
- opts2=$(LC_ALL=C $ZYPPER ls | sed -e '1,2 d' -e 's/^[0-9]\+[[:space:]]\+|[[:space:]]*\([^|]\+\)|.*$/\1/' -e 's/[[:space:]]*$//' -e 's/ /\\ /g' -e "s/^\(.*\)$/'\1'/")
- opts="${opts} ${opts2}"
+ opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q ls | \
+ sed -rn '/^[0-9]/{
+ s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/
+ s/[[:blank:]]*$//
+ /^$/d
+ p
+ }'))
;;
removelock | rl)
- opts2=$(LC_ALL=C $ZYPPER ll | sed -e '1,2 d' | cut -d '|' -f 2)
- opts="${opts} ${opts2}"
+ opts=(${opts[@]}$(echo; LC_ALL=POSIX $ZYPPER -q ll | \
+ sed -rn '/^[0-9]/{
+ s/^[0-9]+[[:blank:]]*\|[[:blank:]]*([^|]+).*/\1/p
+ s/[[:blank:]]*$//
+ /^$/d
+ p
+ }'))
;;
esac
-
- COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+ IFS=$'\n'
+ COMPREPLY=($(compgen -W "${opts[*]}" -- ${cur}))
+ _strip
fi
+ eval $noglob
}
complete -F _zypper -o default zypper
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org