diff options
author | Gabriele Mazzotta <gabriele.mzt@gmail.com> | 2024-01-21 11:37:31 +0100 |
---|---|---|
committer | Philip Langdale <github.philipl@overt.org> | 2024-01-21 10:44:42 -0800 |
commit | 27359c3ff10f1221b599db6f54eb719d0d2386c9 (patch) | |
tree | 01f3fcc52c7db192a459f1ccda51e65a5f85594c /etc/mpv.bash-completion | |
parent | 45f822593f14d78fa22e74fa4e725a3ffd6f713c (diff) | |
download | mpv-27359c3ff10f1221b599db6f54eb719d0d2386c9.tar.bz2 mpv-27359c3ff10f1221b599db6f54eb719d0d2386c9.tar.xz |
bash-completion: parse the mpv options lazily
Parse and cache the options lazily not to impact the shell
startup time.
Diffstat (limited to 'etc/mpv.bash-completion')
-rw-r--r-- | etc/mpv.bash-completion | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/etc/mpv.bash-completion b/etc/mpv.bash-completion index d5d504acf9..4892d0e52b 100644 --- a/etc/mpv.bash-completion +++ b/etc/mpv.bash-completion @@ -17,12 +17,17 @@ # License along with mpv. If not, see <http://www.gnu.org/licenses/>. # -# Cache all the mpv options -_mpv_options=$(mpv --no-config --list-options) +_mpv_options() +{ + if [ -z ${_mpv_options_cache+x} ]; then + _mpv_options_cache=$(mpv --no-config --list-options) + fi + echo "$_mpv_options_cache" +} _mpv_get_args() { - local doc=$(echo "$_mpv_options" | grep -E "^\\s*$1\\s") + local doc=$(_mpv_options | grep -E "^\\s*$1\\s") local partial="$2" local type=$(echo "$doc" | awk '{print $2;}') @@ -81,10 +86,26 @@ _mpv_get_args() # This regex detects special options where we don't want an '=' appended _mpv_special_regex='\s(Flag.*\[not in config files\]|Print)' _mpv_skip_regex='\sremoved \[deprecated\]' -_mpv_regular_options=($(echo "$_mpv_options" | grep -vE "$_mpv_skip_regex" | \ - grep -vE "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--')) -_mpv_special_options=($(echo "$_mpv_options" | grep -vE "$_mpv_skip_regex" | \ - grep -E "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--')) + +_mpv_regular_options() +{ + if [ -z ${_mpv_regular_options_cache+x} ]; then + _mpv_regular_options_cache=($(_mpv_options | grep -vE "$_mpv_skip_regex" | \ + grep -vE "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--')) + _mpv_regular_options_cache="${_mpv_regular_options_cache[*]}" + fi + echo "$_mpv_regular_options_cache" +} + +_mpv_special_options() +{ + if [ -z ${_mpv_special_options_cache+x} ]; then + _mpv_special_options_cache=($(_mpv_options | grep -vE "$_mpv_skip_regex" | \ + grep -E "$_mpv_special_regex" | awk '{print "\\"$1;}' | grep '\--')) + _mpv_special_options_cache="${_mpv_special_options_cache[*]}" + fi + echo "$_mpv_special_options_cache" +} _mpv() { @@ -106,9 +127,9 @@ _mpv() else case $cur in -*) - COMPREPLY=($(compgen -W "${_mpv_regular_options[*]}" -S '=' -- "${cur}")) + COMPREPLY=($(compgen -W "$(_mpv_regular_options)" -S '=' -- "${cur}")) local normal_count=${#COMPREPLY[@]} - COMPREPLY+=($(compgen -W "${_mpv_special_options[*]}" -- "${cur}")) + COMPREPLY+=($(compgen -W "$(_mpv_special_options)" -- "${cur}")) if [ $normal_count -gt 0 -o ${#COMPREPLY[@]} -gt 1 ]; then compopt -o nospace mpv fi |