Forum: Ruby howto tab 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.
Michael 'entropie' Trommer (Guest)
on 2005-12-24 08:56
(Received via mailing list)
Hello,

I use [1] to process a string from cmdline-user-input to call methods
form a module namespace. It works for
now, but there are later a lot of commands to input so i want
tab-completion because iam lazy :)

module CmdsBase
  CmdsBase.cmd_foo
    DoSomething.new
  end

  ...
end

All methods with /^cmd_(\w*)$/ should be completeable from
commandline.

Any ideas to solve this clean?

[1]
  # snip:
  def self.cmd
    begin

      line = gets
      args, i =[], -1
      line.scan(/\w*\s/) do |a|
        a.strip!
        meth = nil
        unless (i+=1).zero? and

        MyBiadjCmdlineCaller.methods.include?(a)
          break if i.zero?
          args.push(a)
        else
          meth = a
        end

        unless args.size.zero? # ignoring input with arguments yet
          MyBiadjCMDLine.ok?(MyBiadjCmdlineCaller.method(meth).call)
        end

      end
    end while true
  end





So long
--
Michael 'entropie' Trommer;  http://ackro.org

ruby -e "0.upto((a='njduspAhnbjm/dpn').size-1){|x| a[x]-=1}; p
'mailto:'+a"
Daniel Calvelo (Guest)
on 2005-12-24 21:30
(Received via mailing list)
Have you tried using the Readline module?

require "readline"
$completions = ["cmd_1","cmd_42","cmd_one"]
Readline::completion_proc = Proc.new{ |prefix| $completions if prefix
=~ /cmd_/ }
0.upto(4) { line = Readline::readline( "Cmd? ", true ) }

You get the idea...

Daniel.
Michael 'entropie' Trommer (Guest)
on 2005-12-27 18:39
(Received via mailing list)
* Daniel Calvelo (dca.gis@gmail.com) wrote:
> Have you tried using the Readline module?
>
> require "readline"
> $completions = ["cmd_1","cmd_42","cmd_one"]
> Readline::completion_proc = Proc.new{ |prefix| $completions if prefix
> =~ /cmd_/ }
> 0.upto(4) { line = Readline::readline( "Cmd? ", true ) }

thanks a lot, thats what i need!

So long
--
Michael 'entropie' Trommer;  http://ackro.org

ruby -e "0.upto((a='njduspAhnbjm/dpn').size-1){|x| a[x]-=1}; p
'mailto:'+a"
This topic is locked and can not be replied to.