Ruby中的预填提示 [英] Pre-Filled Prompt in Ruby

查看:89
本文介绍了Ruby中的预填提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby编写一个小的命令行实用程序来搜索Pubmed。现在,我提示用户进行查询并显示结果,并且用户可以选择追加到查询或输入全新的查询。我想添加编辑当前查询的功能;即提示应预先填充上一个查询的可编辑版本,例如:

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:
感谢您对Casper的答复。我尝试了下面提供的代码,它确实可以单独工作。奇怪的是,当我尝试在宝石中使用它时,它似乎不起作用。我的宝石叫做db_hippo。我在gemspec中添加了rb-readline作为依赖项,并将扩展名放在RbReadline的lib / db_hippo / rb-readline.rb

@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

我希望在DbHippo的另一个子模块中使用该功能,DbHippo :: Source。在DbHippo :: Source中,我在顶部添加:

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'

然后使用DbHippo :: Source方法之一,我有:

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

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

查询变量绝对不是空的,但是由于某种原因,提示没有被预先填充。我还注意到,如果将扩展名放在同一文件(lib / db_hippo / rb-readline)中而不使其成为DbHippo的子模块,则会收到错误:未初始化的常量DbHippo :: Source :: Readline(NameError) 在行上:

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)

这似乎都与正确命名模块,require语句和gem有关。这是我尝试制造的第一个宝石。知道这里出了什么问题吗?

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?

推荐答案

您可以使用 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}"

这篇关于Ruby中的预填提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆