[sbopkg-users] bash completion

slakmagik slakmagik at gmail.com
Thu Mar 10 19:33:32 UTC 2011


Thanks for this - and sorry for taking so long to get back to you.

On 2011-02-03 (Thu) 18:30:54 [+0300], Sergey V. wrote:
> 
> TODO: completion not work for syntax like this
>       sbopkg -i|-b "pkg1 pkg2"
> (now I do not see any other issues)
> 

Yeah, completion on quoted args would good.

I haven't looked into or experimented with programmable completion much,
so here are just some vague notes and questions:

> Please review or/and test it.

> # bash completion for sbopkg tool
> 
> _sbopkg()
> {
>     COMPREPLY=()
> 
>     local cur="${COMP_WORDS[COMP_CWORD]}"
>     local prev="${COMP_WORDS[COMP_CWORD-1]}"
(Not important, but maybe:)
+      local opts=$(echo "-{b,c,d,e,f,g,h,i,k,l,o,P,p,q,R,r,s,u,V,v}")
> 
>     if [[ "$cur" == -* ]]; then
-         COMPREPLY=( $( compgen -W '-b -c -d -e -f -g -h -i -k -l \
-                       -o -P -p -q -R -r -s -u -V -v' -- "$cur" ) )
+          COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
>         return 0
>     fi
> 
>     case $prev in
>         -e)
>             COMPREPLY=( $( compgen -W 'ask continue stop' -- "$cur" ) )
>             return 0
>             ;;
>         -f)
>             COMPREPLY=( $( compgen -f -- "$cur" ) )
>             return 0
>             ;;
>         -d)
>             COMPREPLY=( $( compgen -d -- "$cur" ) )
>             return 0
>             ;;
>         -V)
>             COMPREPLY=( $( compgen -W "? \
>                 $(sbopkg -V ? 2>&1 | cut -s -f1)" -- "$cur" ) )
>             return 0
>             ;;
-         -i|-b)
+         -b|-g|-i|-s)
(The -g is pretty useless but consistent and doesn't seem to hurt
anything. The -s won't help if the search string is the end of the
desired string, but will if it's the beginning.)
>             ;;
>         *)
>             return 0
>             ;;
>     esac
> 
>     local config="/etc/sbopkg/sbopkg.conf"
> 
>     for (( i=${#COMP_WORDS[@]}-1; i>0; i-- )); do
>         if [[ "${COMP_WORDS[i]}" == -f ]]; then
>             config="${COMP_WORDS[i+1]}"
>             break
>         fi
>     done
> 
>     if [ ! -r "$config" ]; then
>         return 0
>     fi
> 
>     . $config
> 
>     for (( i=1; i<${#COMP_WORDS[@]}; i++ )); do
>         case "${COMP_WORDS[i]}" in
>             -V)
>                 REPO_NAME="${COMP_WORDS[i+1]%%/*}"
>                 REPO_BRANCH="${COMP_WORDS[i+1]#*/}"
>                 ;;
>             -d)
>                 REPO_ROOT="${COMP_WORDS[i+1]}"
>                 ;;
>         esac
>     done
> 
>     if [ ! -r "$REPO_ROOT/$REPO_NAME/$REPO_BRANCH/SLACKBUILDS.TXT" ]; then
>         return 0
>     fi
>     COMPREPLY=( $( sed -ne "/^SLACKBUILD NAME: $cur/{s/^SLACKBUILD NAME: //;p}"\
>         $REPO_ROOT/$REPO_NAME/$REPO_BRANCH/SLACKBUILDS.TXT )
>         $( cd $QUEUEDIR; compgen -f -X "!*.sqf"  -- "$cur" ) )
> } && complete -o filenames -F _sbopkg sbopkg

The only real issue I see is that the output of tab completion on -V
isn't entirely correct. It currently produces

?       master  11.0    12.0    12.1    12.2    13.0    13.1    local

and if you accidentally tab complete on 'sbopkg -V ?' it converts it to

sbopkg -V \?

(which doesn't hurt, but doesn't look great.)

It would be better to produce

?            SBo/11.0     SBo/12.1     SBo/13.0     local/local
SB64/master  SBo/12.0     SBo/12.2     SBo/13.1

Changing the last line's 'filenames' to 'default' does this, but this
results in unwanted side effects such as trying to complete things that
have no completions with what's in the current directory.

Anyway, I can certainly add this as is, but changing the -V output would
be good - it's not an issue now but, if multiple repos had a 'master',
then 'master master master 11.0' wouldn't be ideal output. :)

Thanks again.


More information about the sbopkg-users mailing list