Skip to content

Instantly share code, notes, and snippets.

@nono
Forked from tomafro/_gem
Created August 23, 2009 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nono/173414 to your computer and use it in GitHub Desktop.
Save nono/173414 to your computer and use it in GitHub Desktop.
#compdef gem
# My first zsh completion function, for the gem command. It lets you type 'gem <tab>' to complete gem commands
# (including installed ones) and for some commands (currently just open and update) allows you to complete gem
# names as well. The implementation isn't ideal, so I'd appreciate advice on how I can improve it, particularly
# the 'caching' of the gem_commands and installed_gems.
local curcontext="$curcontext" state line ret=1
_arguments -C \
'(- 1 *)'{-h,--help}'[display help information]' \
'(- 1)--version[display version information]' \
'1: :->cmds' \
'*: :->args' && ret=0
case $state in
cmds)
if (( ! $+_gem_cmds )) ; then
typeset -gH _gem_cmds
_gem_cmds=( $(gem help commands | grep '^ [a-z]' | cut -d " " -f 5) )
fi
compadd $_gem_cmds && ret=0
;;
args)
case $line[1] in
(dependency|open|read|uninstall|unpack|update)
if (( ! $+_gem_installed )) ; then
typeset -gH _gem_installed
_gem_installed=( $(gem list | grep '^[A-Za-z]' | cut -d " " -f 1) )
fi
compadd $_gem_installed && ret=0
;;
esac
;;
esac
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment