~richardson183/gbsplay2/gbsplay-import

« back to all changes in this revision

Viewing changes to contrib/gbsplay.bashcompletion

  • Committer: Christian Garbs
  • Date: 2008-08-16 10:34:35 UTC
  • Revision ID: git-v1:a66511c41a9ddef0e2bd55525832d4d1b93215ff
provide better bash completion

only filenames with spaces are still problematic

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
    local cur=${COMP_WORDS[COMP_CWORD]}
11
11
 
12
12
    if [ "${cur:0:1}" = '-' ]; then
13
 
        # looks like an option, return list of all options
14
 
        COMPREPLY=( $( compgen -W "-E -f -g -h -l -o -q -r -R -t -T -v -V -z -Z -1 -2 -3 -4" -- $cur ) )
 
13
        # ==> looks like an option, return list of all options
 
14
        COMPREPLY=( $( compgen -W "-E -f -g -h -l -o -q -r -R -t -T -v -V -z -Z -1 -2 -3 -4" -- $cur) )
 
15
        # add trailing spaces
 
16
        local i
 
17
        for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
 
18
            COMPREPLY[$i]="${COMPREPLY[$i]} "
 
19
        done
15
20
    elif [[ "${COMP_WORDS[$(( ${COMP_CWORD} - 1))]}" =~ '^-.*o$' ]]; then
16
 
        # previous word contained -o, return list of plugouts
 
21
        # ==> previous word contained -o, return list of output plugins
17
22
        COMPREPLY=( $( compgen -W "$(gbsplay -o list 2>/dev/null | ( read; cut -d -  -f 1 )) list" -- $cur ) )
 
23
        # add trailing spaces
 
24
        local i
 
25
        for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
 
26
            COMPREPLY[$i]="${COMPREPLY[$i]} "
 
27
        done
18
28
    else
19
29
        # calculate position of filename
20
30
        local filepos=1
21
31
        while [ "${COMP_WORDS[filepos]:0:1}" = '-' ]; do
22
 
            if [[ "${COMP_WORDS[$(( filepos ))]}" =~ '^-.*o$' ]]; then
 
32
            if [[ "${COMP_WORDS[$filepos]}" =~ '^-.*o$' ]]; then
23
33
                # jump over parameter to -o
24
34
                let filepos++
25
35
            fi
26
36
            let filepos++
27
37
        done
28
38
        if [ $(( $COMP_CWORD - $filepos )) -eq 0 ] ; then
29
 
            # this is the filename
30
 
            COMPREPLY=( $( compgen -f -X '!*.gbs' -- $cur) $(compgen -d -o dirnames -- $cur ))
 
39
            # ==> this is the filename
 
40
            COMPREPLY=()
 
41
            local i
 
42
            # add trailing space to filenames
 
43
            local files
 
44
            files=( $( compgen -f -X '!*.gbs' -- $cur ) ) # does not work: | sed -e 's/\([[:space:]]\)/\\\1/g'
 
45
            for (( i=0; i < ${#files[@]}; i++ )); do
 
46
                COMPREPLY[${#COMPREPLY[@]}]="${files[$i]} "
 
47
            done
 
48
            # add trailing slash to directories
 
49
            local dirs
 
50
            dirs=( $( compgen -d -- $cur ) )
 
51
            for (( i=0; i < ${#dirs[@]}; i++ )); do
 
52
                COMPREPLY[${#COMPREPLY[@]}]="${dirs[$i]}/"
 
53
            done
31
54
        elif [ $(( $COMP_CWORD - $filepos )) -eq 1 ] ; then
32
 
            # this is the subsong start
 
55
            # ==> this is the subsong start
33
56
            COMPREPLY=( $( compgen -W "$(seq $(gbsinfo ${COMP_WORDS[filepos]} 2>/dev/null | grep ^Subsongs | cut -d: -f 2) 2>/dev/null)" -- $cur ) )
 
57
            # add trailing spaces
 
58
            local i
 
59
            for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
 
60
                COMPREPLY[$i]="${COMPREPLY[$i]} "
 
61
            done
34
62
        elif [ $(( $COMP_CWORD - $filepos )) -eq 2 ] ; then
35
 
            # this is the subsong stop...
 
63
            # ==> this is the subsong stop...
36
64
            if [[ ${COMP_WORDS[COMP_CWORD - 1]} =~ '^[0-9]+$' ]] ; then
37
65
                # ...but only if subsong start was given before
38
66
                COMPREPLY=( $( compgen -W "$(seq $(gbsinfo ${COMP_WORDS[filepos]} 2>/dev/null | grep ^Subsongs | cut -d: -f 2) 2>/dev/null)" -- $cur ) )
 
67
                # add trailing spaces
 
68
                local i
 
69
                for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
 
70
                    COMPREPLY[$i]="${COMPREPLY[$i]} "
 
71
                done
39
72
            fi
40
73
        fi
41
74
    fi
47
80
    local cur=${COMP_WORDS[COMP_CWORD]}
48
81
 
49
82
    if [ "${cur:0:1}" = '-' ]; then
50
 
        # looks like an option, return list of all options
51
 
        COMPREPLY=( $( compgen -W "-h -V" -- $cur ) )
 
83
        # ==> looks like an option, return list of all options
 
84
        COMPREPLY=( $( compgen -W "-h -V" -- $cur) )
 
85
        # add trailing spaces
 
86
        local i
 
87
        for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
 
88
            COMPREPLY[$i]="${COMPREPLY[$i]} "
 
89
        done
52
90
    else
53
91
        # calculate position of filename
54
92
        local filepos=1
56
94
            let filepos++
57
95
        done
58
96
        if [ $(( $COMP_CWORD - $filepos )) -eq 0 ] ; then
59
 
            # this is the filename
60
 
            COMPREPLY=( $( compgen -f -X '!*.gbs' -- $cur) $(compgen -d -o dirnames -- $cur ))
 
97
            # ==> this is the filename
 
98
            COMPREPLY=()
 
99
            local i
 
100
            # add trailing space to filenames
 
101
            local files
 
102
            files=( $( compgen -f -X '!*.gbs' -- $cur ) ) # does not work: | sed -e 's/\([[:space:]]\)/\\\1/g'
 
103
            for (( i=0; i < ${#files[@]}; i++ )); do
 
104
                COMPREPLY[${#COMPREPLY[@]}]="${files[$i]} "
 
105
            done
 
106
            # add trailing slash to directories
 
107
            local dirs
 
108
            dirs=( $( compgen -d -- $cur ) )
 
109
            for (( i=0; i < ${#dirs[@]}; i++ )); do
 
110
                COMPREPLY[${#COMPREPLY[@]}]="${dirs[$i]}/"
 
111
            done
61
112
        fi
62
113
    fi
63
114
}
64
115
 
65
 
complete -F _gbsplay -o plusdirs gbsplay
66
 
complete -F _gbsinfo -o plusdirs gbsinfo
 
116
complete -F _gbsplay -o default -o nospace gbsplay
 
117
complete -F _gbsinfo -o default -o nospace gbsinfo