Last active
February 16, 2021 19:35
-
-
Save mickaelperrin/6162752a75803787fb717750ffd1a97c to your computer and use it in GitHub Desktop.
Symfony ZSH completion with caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef _sfbinary symfony | |
function _symfony_caching_policy() { | |
local -a oldp | |
oldp=( "$1"(Nmh+24) ) | |
(( $#oldp )) | |
} | |
function _sfbinary_subcommand() { | |
if ( [[ ${+list} -eq 0 ]] || _cache_invalid "symfony_subcommand_console" ) && ! _retrieve_cache "symfony_subcommand_console"; then | |
list=$(symfony $1 --no-ansi 2>/dev/null | sed "1,/Available commands/d" | awk '/^ ?[^ ]+ / { print $1 }') | |
_store_cache "symfony_subcommand_console" list | |
fi | |
_arguments "1:subcommand:($list)" | |
} | |
function _sfbinary() { | |
local cache_policy | |
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy | |
zstyle ":completion:${curcontext}:" cache-policy ${cache_policy:-_symfony_caching_policy} | |
local line | |
if ( [[ ${+list} -eq 0 ]] || _cache_invalid SYMFONY_COMMANDS ) && ! _retrieve_cache SYMFONY_COMMANDS; then | |
list=$(symfony list --no-ansi 2>/dev/null | sed "1,/Available commands/d" | sed "/Available wrappers/,/^$/d" | sed "s/ [A-Z].*//" | tr , '\n' | sed 's/ //g') | |
_store_cache SYMFONY_COMMANDS list | |
fi | |
_arguments -C "1:commands:($list)" "*::arg:->args" | |
if [[ $line[1] != "" ]]; then | |
case $line[1] in | |
console) | |
_sfbinary_subcommand 'console' | |
;; | |
composer) | |
_sfbinary_subcommand 'composer' | |
;; | |
*) | |
_files | |
;; | |
esac | |
fi | |
} | |
compdef _sfbinary symfony |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment