什么会给我像 ruby​​ readline 这样的默认值? [英] What will give me something like ruby readline with a default value?

查看:9
本文介绍了什么会给我像 ruby​​ readline 这样的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在终端上显示一个已经输入默认值的提示,我该怎么做?

If I want to have a prompt on the terminal with a default value already typed in, how can I do that?

Ruby 的标准 Readline.readline() 允许我设置 history 但不填写默认值(至少据我所知)

Ruby's standard Readline.readline() lets me set the history but not fill in a default value (as far as I can tell, at least)

我想要这样的东西:

代码:

input = Readline.readline_with_default('>', 'default_text')

控制台:

> default_text|

推荐答案

Readline 可以满足您的要求.有一个回调,您可以在显示提示后获得控制并将一些文本插入读取缓冲区.

What you are asking is possible with Readline. There's a callback where you can get control after the prompt is displayed and insert some text into the read buffer.

这对我有用:

Readline.pre_input_hook = -> do
  Readline.insert_text "hello.txt"
  Readline.redisplay

  # Remove the hook right away.
  Readline.pre_input_hook = nil
end

input = Readline.readline("Filename: ", false)
puts "-- input:#{input.inspect}"

顺便说一句,我相当尝试使用 HighLine,但它对我来说似乎是一个无可替代的选择.令人失望的原因之一是 HighLine#ask 将光标移动键读取为常规输入.在那种发现之后,我不再朝那个方向看.

BTW, I fairly tried to use HighLine, but it appeared to be a no-alternative to me. One of the disappointing reasons was the fact that HighLine#ask reads cursor movement keys as regular input. I stopped looking in that direction after that sort of discovery.

这篇关于什么会给我像 ruby​​ readline 这样的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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