bash completion: only generate option list when needed

Right now we are generating the fully option list before doing
anything else. That makes filename completion significantly slower
than it was before, for no gain. It's easy to only generate the
option list when it's actually needed.

I also know I could additionally cache the option list across
invocations, but I'm not doing that yet to make testing easier.
This commit is contained in:
Philip Langdale 2020-01-13 17:10:51 -08:00
parent 4d51660195
commit 3259494d9e
1 changed files with 5 additions and 5 deletions

View File

@ -74,11 +74,6 @@ _mpv()
{
compopt +o nospace mpv
# This regex detects special options where we don't want an '=' appended
local special_regex='Flag.*\[not in config files\]|Print'
local options=($(mpv --list-options | grep -v -E "$special_regex" |awk '{print "\\"$1;}' | grep '\--'))
local specials=($(mpv --list-options | grep -E "$special_regex" |awk '{print "\\"$1;}' | grep '\--'))
# _filedir requires the current candidate be in $cur
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[((COMP_CWORD - 1))]}
@ -95,6 +90,11 @@ _mpv()
else
case $cur in
-*)
# This regex detects special options where we don't want an '=' appended
local special_regex='Flag.*\[not in config files\]|Print'
local options=($(mpv --list-options | grep -v -E "$special_regex" |awk '{print "\\"$1;}' | grep '\--'))
local specials=($(mpv --list-options | grep -E "$special_regex" |awk '{print "\\"$1;}' | grep '\--'))
COMPREPLY=($(compgen -W "${options[*]}" -S '=' -- "${cur}"))
local normal_count=${#COMPREPLY[@]}
COMPREPLY+=($(compgen -W "${specials[*]}" -- "${cur}"))