Inside my Bash Completion file, i'm looking up completion-strings by an external script, which takes some time (1-2 seconds). Since these strings mostly stay the same for the rest of the time the current shell runs, i want to cache them and when the Bash completion is triggered the next time, it should use the cached string instead of the expensive lookup, so that it completes immediately when its run the second time.
To get a feeling about by completion file, here is the important part of the completion file:
getdeployablefiles()
{
# How can i cache the result of 'pbt getdeployablefiles'
# for the time the current shell runs?
echo `pbt getdeployablefiles`
}
have pbt &&
_pbt_complete()
{
local cur goals
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
goals=$(getdeployablefiles)
COMPREPLY=( $(compgen -W "${goals}" -- $cur) )
return 0
} &&
complete -F _pbt_complete pbt
How can i cache the output of getdeployablefiles for the rest of the shell session? I need some kind of global variable here, or some other trick.
Solution:
Just had to make goals
non-local and ask if it's set. The final script:
getdeployablefiles()
{
echo `pbt getdeployablefiles`
}
have pbt &&
_pbt_complete()
{
local cur
if [ -z "$_pbt_complete_goals" ]; then
_pbt_complete_goals=$(getdeployablefiles)
fi
_pbt_complete_goals=$(getdeployablefiles)
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${_pbt_complete_goals}" -- $cur) )
return 0
} &&
complete -F _pbt_complete pbt