File: lilv.bash_completion

package info (click to toggle)
lilv 0.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,336 kB
  • sloc: ansic: 11,764; python: 1,698; cpp: 349; makefile: 160; sh: 70
file content (55 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright 2012-2022 David Robillard <d@drobilla.net>
# Copyright 2009 Lars Luthman <lars.luthman@gmail.com>
# SPDX-License-Identifier: 0BSD OR ISC

# Bash auto-completion script written for lv2info.  Could be adapted to any
# other program that takes an LV2 plugin URI as parameter.

# For some reason Bash splits the command line not only at whitespace
# but also at ':' signs before putting the parts into COMP_WORDS.
# Since ':' is used in all URIs, which are what we want to complete,
# we have to put the URI back together before we can complete it
# and then cut off the parts we prepended from the completions.
# It probably breaks in some special cases but for most common uses
# it should work fine.

function _lv2info() {
    local uri cur opts w wn raw_reply len type
    opts=`lv2ls | xargs -n1 echo -n " "`
    
    # This is the last "word", as split by Bash.
    cur="${COMP_WORDS[COMP_CWORD]}"
    w="$cur"
    
    # Add the previous word while it or this one is a word break character
    for i in `seq $(( $COMP_CWORD - 1 )) -1 1`; do
	wn="${COMP_WORDS[i]}"
	if expr "$COMP_WORDBREAKS" : ".*$wn" > /dev/null; then
	    if expr "$COMP_WORDBREAKS" : ".*$w" > /dev/null; then
		break
	    fi
	fi
	w="$wn"
	uri="$w$uri"
    done
    
    # Check the length of the words we prepend
    len=${#uri}
    uri="$uri$cur"
    raw_reply="$(compgen -W "${opts}" -- ${uri})"
    
    # If we are listing alternatives, just print the full URIs.
    type=`echo $COMP_TYPE | awk '{ printf "%c", $1 }'`
    if expr "?!@%" : ".*$type" > /dev/null; then
	COMPREPLY=( $raw_reply )
	return 0
    fi
    
    # Otherwise, strip the prepended words from all completion suggestions.
    COMPREPLY=()
    for i in $raw_reply; do
	COMPREPLY=( ${COMPREPLY[@]} ${i:len} )
    done
}

complete -F _lv2info lv2info