25 lines
808 B
Text
25 lines
808 B
Text
|
|
# shellcheck shell=bash
|
||
|
|
# Cache completion scripts to avoid subprocess overhead on every shell start.
|
||
|
|
#
|
||
|
|
# Usage: cached_completion <tool> <generation-command...>
|
||
|
|
# Example: cached_completion gh gh completion -s bash
|
||
|
|
#
|
||
|
|
# The cache file is regenerated when the tool binary is newer than the cache.
|
||
|
|
# Cache location: ${XDG_CACHE_HOME:-$HOME/.cache}/bash.d/
|
||
|
|
|
||
|
|
_COMP_CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/bash.d"
|
||
|
|
|
||
|
|
cached_completion() {
|
||
|
|
local tool="$1"; shift
|
||
|
|
local cache_file="${_COMP_CACHE}/${tool}.bash"
|
||
|
|
local tool_path
|
||
|
|
tool_path="$(command -v "$tool" 2>/dev/null)" || return
|
||
|
|
|
||
|
|
if [[ ! -f "$cache_file" || "$tool_path" -nt "$cache_file" ]]; then
|
||
|
|
mkdir -p "$_COMP_CACHE"
|
||
|
|
"$@" > "$cache_file" 2>/dev/null || return
|
||
|
|
fi
|
||
|
|
|
||
|
|
# shellcheck disable=SC1090
|
||
|
|
. "$cache_file"
|
||
|
|
}
|