ruby command -- bash readline completion

D

Daniel Harple

I have put together some basic bash readline completion for ruby.

* Typing `ruby -[TAB]` will give you a list of available command line
switches.
* Typing `ruby -r[TAB]' will give you a list of available libraries
to load. For this I have disabled searching "." because that could
take a very long time.

See [http://www.rubygarden.org/ruby?ProgrammableCompletition] for ri
completion

Create a file named ruby in bash_completion.d/ with --

#+++++++
have ruby &&
{
# Get a list of all available ruby libraries
_ruby_libraries()
{
COMPREPLY=( $( compgen -P "$prefix" -W "$( ruby -rfind -e '
libraries = []
$:.each do |path|
# searching "." may take a very long time.
unless path == "."
Find.find(path) do |found|
Find.prune if File.dirname(found) =~ /test/
libraries << found.gsub(/^#{path}/,"")[1..-4] if found[/.rb
$/]
end
end
end
print libraries.join(" ")
'
) " -- $cur ) )
}

_ruby()
{
local cur prev prefix temp

COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
prefix=""

# completing an option (may or may not be separated by a space)
if [[ "$cur" == -?* ]]; then
temp=$cur
prev=${temp:0:2}
cur=${temp:2}
prefix=$prev
fi

case "$prev" in
-r)
_ruby_libraries
return 0
;;
esac

# handle case where first parameter is not a dash option
if [ $COMP_CWORD -eq 1 ] && [[ "$cur" != -* ]]; then
_filedir
return 0
fi

# complete using basic options
COMPREPLY=( $( compgen -W '-a -c -d -e -F -l -n -p -s -S -T -v
-w -W -0 -C -i -I -K -r' -- $cur ) )

return 0
}
complete -F _ruby $default ruby
}
#+++++++

-- Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top