Forum: Ruby ruby command -- bash readline completion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2005-12-27 12:50
(Received via mailing list)
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
This topic is locked and can not be replied to.