Debian Codegraph @

etc/mpv.bash-completion

raw
1 #
2 # This file is part of mpv.
3 #
4 # mpv is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # mpv is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 _mpv_options()
19 {
20 if [ -z ${_mpv_options_cache+x} ]; then
21 _mpv_options_cache=$(mpv --no-config --list-options)
22 fi
23 echo "$_mpv_options_cache"
24 }
25
26 _mpv_get_args()
27 {
28 local doc=$(_mpv_options | grep -E "^\\s*$1\\s")
29 local partial="$2"
30 local type=$(echo "$doc" | awk '{print $2;}')
31
32 # We special-case profiles to ensure we read the config
33 if [ "$1" = "--show-profile" ]; then
34 type="ShowProfile"
35 elif [ "$1" = "--profile" ]; then
36 type="Profile"
37 fi
38
39 declare -a candidates
40 case $type in
41 String)
42 if echo "$doc" | grep -q '\[file\]' ; then
43 if [ "$cur" = '=' ]; then
44 # Without this, _filedir will try and complete files starting with '='
45 cur=""
46 fi
47 _filedir 2>/dev/null || COMPREPLY=($(compgen -f))
48 return 0
49 else
50 candidates=($(mpv --no-config $1=help | grep -v ':' | awk '{print $1;}'))
51 candidates+=("help")
52 fi
53 ;;
54 Flag)
55 candidates=("yes" "no" "help")
56 ;;
57 Choices:|Object)
58 candidates=($(mpv --no-config $1=help | grep -v ':' | awk '{print $1;}'))
59 candidates+=("help")
60 ;;
61 Image)
62 candidates=($(mpv --no-config $1=help))
63 candidates=("${candidates[@]:2}")
64 candidates+=("help")
65 ;;
66 Profile)
67 candidates=($(mpv $1=help | grep -v ':' | awk '{print $1;}'))
68 candidates+=("help")
69 ;;
70 ShowProfile)
71 candidates=($(mpv $1= | grep -v ':' | awk '{print $1;}'))
72 ;;
73 *)
74 # There are other categories; some of which we could do something smarter
75 # about, with enough work.
76 ;;
77 esac
78 COMPREPLY=($(compgen -W "${candidates[*]}" -- "${partial}"))
79 if [ ${#COMPREPLY[@]} -gt 1 ]; then
80 compopt -o nospace mpv
81 fi
82 }
83
84 # This regex detects special options where we don't want an '=' appended
85 _mpv_special_regex='\s(Flag.*\[not in config files\]|Print)'
86 _mpv_skip_regex='\sremoved \[deprecated\]'
87
88 _mpv_regular_options()
89 {
90 if [ -z ${_mpv_regular_options_cache+x} ]; then
91 _mpv_regular_options_cache=($(_mpv_options | grep -vE "$_mpv_skip_regex" | \
92 grep -vE "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--'))
93 _mpv_regular_options_cache="${_mpv_regular_options_cache[*]}"
94 fi
95 echo "$_mpv_regular_options_cache"
96 }
97
98 _mpv_special_options()
99 {
100 if [ -z ${_mpv_special_options_cache+x} ]; then
101 _mpv_special_options_cache=($(_mpv_options | grep -vE "$_mpv_skip_regex" | \
102 grep -E "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--'))
103 _mpv_special_options_cache="${_mpv_special_options_cache[*]}"
104 fi
105 echo "$_mpv_special_options_cache"
106 }
107
108 _mpv()
109 {
110 compopt +o nospace mpv
111
112 # _filedir requires the current candidate be in $cur
113 local cur=${COMP_WORDS[COMP_CWORD]}
114 local prev=${COMP_WORDS[((COMP_CWORD - 1))]}
115
116 if [ "$cur" = '=' ]; then
117 # If the current word is '=' then we are looking for an argument for the
118 # option specified by the previous word.
119 _mpv_get_args "$prev"
120 elif [ "$prev" = '=' ]; then
121 # If the previous word is '=' then we are completing an argument for the
122 # option specified by the word before the '='.
123 local prevprev=${COMP_WORDS[((COMP_CWORD - 2))]}
124 _mpv_get_args "$prevprev" "$cur"
125 else
126 case $cur in
127 -*)
128 COMPREPLY=($(compgen -W "$(_mpv_regular_options)" -S '=' -- "${cur}"))
129 local normal_count=${#COMPREPLY[@]}
130 COMPREPLY+=($(compgen -W "$(_mpv_special_options)" -- "${cur}"))
131 if [ $normal_count -gt 0 -o ${#COMPREPLY[@]} -gt 1 ]; then
132 compopt -o nospace mpv
133 fi
134 ;;
135 *)
136 _filedir 2>/dev/null || COMPREPLY=($(compgen -f))
137 ;;
138 esac
139 fi
140 }
141
142 complete -F _mpv mpv