basesrc: use segment start if DTS for first buffer is unset
[platform/upstream/gstreamer.git] / tools / gstreamer-completion
1 # Bash tab-completion for GStreamer.                      -*- shell-script -*-
2 # Put this in /etc/bash_completion.d/
3
4 _gst_inspect() {
5     local _gst_version=1.0
6     local cur cword prev words
7     _gst_init_completion
8     [[ "$cur" == "=" ]] && cur=
9     [[ "$cur" =~ -.*=*$ ]] && prev="${cur%%=*}" cur="${cur#*=}"
10
11     _gst_common_options || return
12
13     COMPREPLY=( $(compgen \
14         -W "$(_gst_parse_help gst-inspect-$_gst_version) \
15             $(_gst_plugins) $(_gst_elements)" \
16         -- "$cur") )
17     [[ $COMPREPLY == *= ]] && compopt -o nospace 2>/dev/null
18 } &&
19 complete -F _gst_inspect gst-inspect-1.0
20
21 _gst_launch() {
22     local _gst_version=1.0
23     local cur cword prev words
24     _gst_init_completion
25     local curtype option element property
26     _gst_launch_parse
27     _gst_common_options || return
28
29     COMPREPLY=( $(_gst_launch_compgen) )
30     [[ $COMPREPLY == *= ]] && compopt -o nospace 2>/dev/null
31 } &&
32 complete -o default -F _gst_launch gst-launch-1.0
33
34 _gst_common_options() {
35     if [[ -n "$curtype" ]]; then  # Called from _gst_launch
36         [[ $curtype == optionval ]] || return 0
37     else  # Called from _gst_inspect
38         local option="$prev"
39     fi
40
41     case "$option" in
42         --gst-debug-level)
43                 COMPREPLY=( $(compgen -W "0 1 2 3 4 5" -- "$cur") );;
44         --gst-debug) # TODO: comma-separated list of category_name:level pairs.
45                 ;;
46         --gst-plugin-path) # TODO: support multiple (colon-separated) paths.
47                 COMPREPLY=( $(compgen -d -- "$cur") );;
48         --gst-plugin-load) # TODO: comma-separated list of plugins (files?).
49                 ;;
50         *) return 0;;
51     esac
52     return 1  # No need to attempt further completions.
53 }
54
55 _gst_launch_compgen() {
56     case $curtype in
57         option)
58             compgen \
59                 -W "$(_gst_parse_help gst-launch-$_gst_version)" \
60                 -- "$cur" ;;
61         element)
62             compgen -W "$(_gst_elements)" -- "$cur" ;;
63         option-or-element)
64             compgen \
65                 -W "$(_gst_parse_help gst-launch-$_gst_version) \
66                     $(_gst_elements)" \
67                 -- "$cur" ;;
68         optionval)
69             case "$option" in
70                 -o|--output) compgen -f -- "$cur" ;;
71                 --exclude) ;; # TODO: comma-separated list of status information types.
72             esac ;;
73         \!)
74             compgen -W '!' -- "$cur" ;;
75         property)
76             compgen -W "$(_gst_properties $element) ! " -- "$cur" ;;
77         propertyval)
78             compgen -W "$(_gst_property_values $element $property)" -- "$cur" ;;
79     esac
80 }
81
82 _gst_plugins() {
83     gst-inspect-$_gst_version 2>/dev/null |
84     grep -v 'Total count' |
85     awk -F': +' '{print $1}' |
86     uniq
87 }
88
89 _gst_elements() {
90     gst-inspect-$_gst_version 2>/dev/null |
91     grep -v 'Total count' |
92     awk -F': +' '{print $2}'
93 }
94
95 _gst_properties() {
96     local element="$1"
97     gst-inspect-$_gst_version "$element" 2>/dev/null |
98     sed -n '/^Element Properties:$/,$ p' |
99     awk '/^  [a-z]/ { print $1 "=" }'
100 }
101
102 _gst_property_values() {
103     local element=$1 property=$2
104     gst-inspect-$_gst_version $element 2>/dev/null |
105     awk "
106         /^Element Properties:\$/        { inproperties = 1; next; }
107         inproperties && /^  $property / { inproperty = 1; next; }
108         inproperty && /^ *Boolean/      { printf \"true\nfalse\n\"; exit; }
109         inproperty && /^ *Enum/         { inenum = 1; next; }
110         inenum && /^ *\([0-9]+\): /     { print \$2; next; }
111         inproperty && /^  [a-z]/        { exit; }"
112 }
113
114 # Walks over $words, sets $curtype to the string:
115 #
116 #   'option' if $cur is an option or flag like "-a" or "--abc".
117 #   'optionval' if $cur is the value of an option
118 #               (which will be set in $option).
119 #   'element' if $cur is a GStreamer element name.
120 #   '!' if $cur is '!'.
121 #   'property' if $cur is the name of a property of a GStreamer element
122 #               (which will be set in $element).
123 #   'propertyval' if $cur is the value of an element's property
124 #               (which will be set in $element and $property, respectively).
125 #
126 # ($cur is the word currently being completed.)
127 #
128 # Before calling this function make sure that $curtype, $option, $element and
129 # $property are local, and that $cur, $cword and $words have been initialised.
130 #
131 # See test cases in tests/misc/test-gstreamer-completion.sh in the
132 # gstreamer source repository.
133 #
134 _gst_launch_parse() {
135     local i next state
136     curtype= i=1 state=start
137     while [[ $i -le $cword ]]; do
138         next="${words[i]}"
139         # Note that COMP_WORDBREAKS by default includes "=" and ":".
140         case "$state,$next" in
141             start,-*=*) curtype=optionval option="${next%%=*}" state=start;;
142             start,-*) curtype=option option="$next" state=option;;
143             start,) curtype=option-or-element;;
144             start,*) curtype=element element="$next" state=element;;
145             option,=) curtype=optionval state=option=;;
146             option,*) _gst_takes_arg "$option" &&
147                             curtype=optionval state=start ||
148                         # re-evaluate without incrementing i:
149                         { curtype= state=start; continue; }
150                       ;;
151             option=,*) curtype=optionval state=start;;
152             element,\!) curtype='!' state='!';;
153             \!,*) curtype=element element="$next" state=element;;
154             element,*=)
155                 curtype=propertyval property="${next%=}" state=property=;;
156             element,*=*)
157                 curtype=propertyval property="${next%%=*}" state=element;;
158             element,*) curtype=property property="$next" state=property;;
159             property,=) curtype=propertyval state=property=;;
160             property=,*) curtype=propertyval state=element;;
161         esac
162         i=$((i + 1))
163     done
164     cur="${cur#*=}"
165 }
166
167 _gst_takes_arg() {
168     case "$1" in
169         -o|--output|--gst-debug-level|--gst-debug) true;;
170         --gst-plugin-path|--gst-plugin-load|--exclude) true;;
171         *) false;;
172     esac
173 }
174
175 _gst_parse_help() {
176     $1 --help-all 2>&1 | grep -Eo -e '--[a-z-]+'
177 }
178
179 _gst_init_completion() {
180     if type _get_comp_words_by_ref &>/dev/null; then
181         # Available since bash-completion 1.2
182         _get_comp_words_by_ref cur cword prev words
183     else
184         # bash-completion not installed or too old. Use bash's raw facilities.
185         # This won't complete properly if the cursor is in the middle of a
186         # word.
187         cur="${COMP_WORDS[COMP_CWORD]}"
188         prev="${COMP_WORDS[COMP_CWORD-1]}"
189         cword=$COMP_CWORD
190         words=("${COMP_WORDS[@]}")
191     fi
192 }