Skip to content

Instantly share code, notes, and snippets.

@cstrouse
Last active November 1, 2017 04:11
Show Gist options
  • Save cstrouse/8e13a7008ebfef49f335d05bb4212409 to your computer and use it in GitHub Desktop.
Save cstrouse/8e13a7008ebfef49f335d05bb4212409 to your computer and use it in GitHub Desktop.
bash completion for chromebrew
_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
@cstrouse
Copy link
Author

cstrouse commented Feb 3, 2017

Thanks for adding that.

@uberhacker
Copy link

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

@cstrouse
Copy link
Author

cstrouse commented Nov 1, 2017

@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