Last active
November 1, 2017 04:11
-
-
Save cstrouse/8e13a7008ebfef49f335d05bb4212409 to your computer and use it in GitHub Desktop.
bash completion for chromebrew
This file contains 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
_crew() { | |
COMPREPLY=() | |
local word="${COMP_WORDS[COMP_CWORD]}" | |
if [ "$COMP_CWORD" -eq 1 ]; then | |
COMPREPLY=( $(compgen -W "search download install remove whatprovides update upgrade" -- "$word") ) | |
else | |
local command="${COMP_WORDS[1]}" | |
local availpkgs=$(ls /usr/local/lib/crew/packages/ | sed -e 's,.rb,,g' | sort) | |
local installed=$(pcregrep -o1 '[\s]+\"name\": \"([\w_-]+)\"' /usr/local/etc/crew/device.json | sort) | |
case "$command" in | |
download) | |
COMPREPLY=( $(compgen -W "$availpkgs" -- "$word") ) | |
;; | |
install) | |
# only suggest packages for installation that are not installed | |
local notinstalled=$(comm -23 <(echo "$availpkgs") <(echo "$installed")) | |
COMPREPLY=( $(compgen -W "$notinstalled" -- "$word") ) | |
;; | |
remove|upgrade) | |
# only suggest packages for removal that are installed | |
COMPREPLY=( $(compgen -W "$installed" -- "$word") ) | |
;; | |
*) | |
;; | |
esac | |
fi | |
} | |
complete -F _crew crew |
The 'complete' command may be missing. You could use the following to implement it:
complete () {
emulate -L $(echo $SHELL | tr '/' '\n' | tail -1)
local args void cmd print remove
args=("$@")
zparseopts -D -a void o: A: G: W: C: F: P: S: X: a b c d e f g j k u v p=print r=remove
if [[ -n $print ]]
then
printf 'complete %2$s %1$s\n' "${(@kv)_comps[(R)_bash*]#* }"
elif [[ -n $remove ]]
then
for cmd
do
unset "_comps[$cmd]"
done
else
compdef _bash_complete\ ${(j. .)${(q)args[1,-1-$#]}} "$@"
fi
}
Thanks for adding that.
Hey @cstrouse: I really like this idea, however, I was unable to get the script working. I have executable compgen, pcregrep and complete commands available. If I type crew in<tab>
, it won't complete with crew install
. Is there something I might be missing?
Thanks
@uberhacker I think it only completes package names in the current state.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried generating the options using crew itself but it was way too slow hence the alternate methods for determining completion options.
Place the completion file at
/usr/local/lib/crew/completion.bash.inc
and add this to your~/.bashrc
. I like doing it this way (as seen in the Google Cloud Platform SDK) better.