From: Joel VanderWerf Date: 2003-02-28T07:40:09+09:00 Subject: Re: Looking for shell emulation with command completion Jim Freeze wrote: > On Friday, 28 February 2003 at 7:15:50 +0900, Joel VanderWerf wrote: > >>Jim Freeze wrote: >> >>>Hi: >>> >>>Has anyone written a ruby console app that supports >>>command completion? I am very interested in >>>seeing an example of how this can be done. >>> >> >>How about just using irb? It's not hard to extract enough code from the >>irb source so you can embed it within your own app. (I'll post the code >>if anyone's interested.) > > > Never thought about that. Doesn't irb use readline for > its command completion? Yes. > Please, post the code. I am interested. ===================================== #!/usr/bin/env ruby require 'irb' require 'irb/completion' module IRB def IRB.start_session(object) unless $irb IRB.initialize nil ## maybe set some opts here, as in parse_opts in irb/init.rb? IRB.load_modules end workspace = WorkSpace.new(object) if @CONF[:SCRIPT] ## normally, set by parse_opts $irb = Irb.new(workspace, @CONF[:SCRIPT]) else $irb = Irb.new(workspace) end @CONF[:IRB_RC].call($irb.context) if @CONF[:IRB_RC] @CONF[:MAIN_CONTEXT] = $irb.context trap 'INT' do $irb.signal_handle end catch :IRB_EXIT do $irb.eval_input end ## might want to reset your app's interrupt handler here end end if __FILE__ == $0 class Object include IRB::ExtendCommandBundle # so that Marshal.dump works end x = Object.new puts "\nStarted irb shell" IRB.start_session(x) puts "\nExited irb shell" end ======================================= If you want to change the way the x object is displayed in the prompt, just define a to_s method for it. Irb lets you customize the prompt, as well, but I've never played with that much.