5

I am using to Ruby to write a small command line utility to search Pubmed. Right now, I prompt the user for a query and display the results, and the user has the option of appending to the query or entering an entirely new query. I would like to add the ability to edit the current query; i.e. the prompt should come pre-filled with an editable version of the previous query, like so:

Enter query: <PREVIOUS QUERY HERE>

It's easy enough to print out the previous query next to the prompt, but how do I make this output editable, as if the user had typed it herself?

@casper: Thank you for the response Casper. I tried the code that you supplied below, and it does indeed work on its own. Strangely enough, it doesn't seem to work when I try to use it in a gem. My gem is called db_hippo. I added rb-readline as a dependency in my gemspec, and I put the extension to RbReadline in lib/db_hippo/rb-readline.rb

module DbHippo
  module RbReadline
    <CASPER'S EXTENSION HERE>
  end
end

I wish to use the functionality in another submodule of DbHippo, DbHippo::Source. In DbHippo::Source I added at the top:

require 'rb-readline'
require 'db_hippo/rb-readline'

Then in one of the methods of DbHippo::Source, I have:

RbReadline.prefill_prompt(query)
query = Readline.readline("Query: ", true)

The query variable is definitely not empty, but for some reason in this context the prompt doesn't get prefilled. I also notice that if I put the extension in the same file (lib/db_hippo/rb-readline) without making it a submodule of DbHippo, I get the error: uninitialized constant DbHippo::Source::Readline (NameError) on the line:

query = Readline.readline("Query: ", true)

This all seems to have something to do with proper naming of modules, require statements, and gems. This is the first gem I've tried to build. Any idea what's going wrong here?

4
  • Yes. You can't put my extension code inside the DbHippo module. That's because my extension code is monkey patching RbReadline, which is a top-level module. If you put the patch code inside DbHippo you will create a new module DbHippo::RbReadline, and the original non-patched code will still exist in the top-level RbReadline module. In addition to that you now have two RbReadline modules, and ruby will have trouble differentiating them..thus making everything work very poorly :-/ So just keep my monkey patch at the top-level and you should be OK.
    – Casper
    Jun 18, 2012 at 5:29
  • I usually create a folder in my lib dir called extensions or something similar, and put all the monkey-patches in there. Then you just require 'db-hippo/extensions/rb-readline', and it will be pretty obvious to anyone reading the code what is happening.
    – Casper
    Jun 18, 2012 at 5:32
  • Also note that you have to require 'rb-readline' BEFORE you require the extension code. Make sure it goes in the right order otherwise it won't work. If you're unsure rename the extension file to something else, like readline-patch.rb to make sure you're including the rb-readline gem before you include the patch code (it's possible to confuse rubygems when you have a file with the same name as a global gem).
    – Casper
    Jun 18, 2012 at 5:48
  • Great explanation, I've got everything working now. Thanks for all the help! Jun 18, 2012 at 6:49

2 Answers 2

6

Maybe googlers will find this useful.

With plain Readline on Ruby 2.1 you could use:

def ask(prompt, default=nil)

  if default
    Readline.pre_input_hook = -> {
      Readline.insert_text(default)
      Readline.redisplay
      # prevent re-trigger on every `readline`
      Readline.pre_input_hook = nil
    }
  end
  data = Readline.readline("#{prompt}: ")
  return data.chomp
end

ask("MOAR...?", "COMPUTARS!") # displays: MOAR...? COMPUTARS!

At the prompt the text COMPUTARS! will be editable

2
  • 1
    After multiple hours of searching, googling and digging through multiple ways of dealing with terminal in ruby to solve that problem in question I finally found really clean solution thanks to You!! Thank YOU!
    – przemod
    Jun 8, 2016 at 22:35
  • 1
    However, it would be very prudent to clear Readline.pre_input_hook inside the hook handler. If you don't, it will continue to trigger on every readline (including irb prompt :D )
    – Amadan
    Sep 10, 2018 at 4:57
4

You can do it with RbReadline:

require 'rubygems'
require 'rb-readline'

module RbReadline
  def self.prefill_prompt(str)
    @rl_prefill = str
    @rl_startup_hook = :rl_prefill_hook
  end

  def self.rl_prefill_hook
    rl_insert_text @rl_prefill if @rl_prefill
    @rl_startup_hook = nil
  end
end

RbReadline.prefill_prompt("Previous query")
str = Readline.readline("Enter query: ", true)

puts "You entered: #{str}"

Not the answer you're looking for? Browse other questions tagged or ask your own question.