- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
使用 readline prompt
时,我无法清除 stdout
中的一行.
我的代码类似这样:
import readline from 'readline'
const rl = readline.createInterface(process.stdin, process.stdout)
let input = ''
readline.emitKeypressEvents(process.stdin)
process.stdin.on('keypress', registerInput)
function registerInput (str, key) {
if (key.name === 'backspace') {
input = input.slice(0, input.length - 1)
} else {
input += str
}
if (input === '42') {
input = ''
console.log(' ✓')
nextPrompt()
}
}
function nextPrompt () {
readline.clearLine(process.stdout, 0)
rl.setPrompt('What is the meaning of life? ')
rl.prompt()
}
nextPrompt()
在实际代码中,会生成算术题。
Here is a screen capture of the output .
光标重置(使用 .prompt(false)
将光标保持在原位),但输入仍然存在于新提示中。
输入 42
不应出现在下一行的新提示中。
为什么我使用 .on('keypress'
而不是 rl.on('line'
?我希望游戏中的响应迅速,避免按下输入。
我尝试过的:
readline.clearLine(process.stdout, -1)
readline.clearLine(process.stdout, 1)
readline.clearLine(process.stdin, 0)
process.stdout.clearLine()
我正在使用 Node v6.0.0 和 babel-cli v6.7.7(这里的 babel 只是用于导入语句,但在实际代码中我使用 object-rest-spread 因此需要 babel)。 Here is a gist of the generated code如果你没有安装 babel。
rl.prompt
是否与 readline.clearLine
不兼容?如何解决?
最佳答案
我是这样解决的:
import readline from 'readline'
let input = ''
let question = 'What is the meaning of life? '
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
readline.emitKeypressEvents(process.stdin)
process.stdin.on('keypress', registerInput)
function registerInput (str, key) {
if (key.name === 'backspace') {
input = input.slice(0, input.length - 1)
} else {
const numbers = '1 2 3 4 5 6 7 8 9 0'.split(' ')
if (numbers.indexOf(str) !== -1) {
input += str
}
}
if (input === '42') {
console.log(' ✓')
input = ''
}
render(question, input)
}
function render (question, input) {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
process.stdout.write(question + input)
}
render(question, input)
但问题仍然存在,为什么 rl.prompt
与 readline.clearLine
不兼容?
我猜测 prompt
在幕后做了一些类似于我的方法的事情。 resetPromptInput
会很好。
关于node.js - 在 Node 中使用 readline prompt/setPrompt 时无法清除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014419/
我正在学习使用 PowerShell 编写脚本,我发现这段代码可以帮助我完成一个项目 该示例来自 Is there a one-liner for using default values with
出于好奇,window.prompt 和 JavaScript 中的 prompt 之间有什么区别。 老师用的我考试题的答案之一 var yourName = window.prompt("Pleas
我正在使用 Phonegap 构建一个应用程序我正在使用 javascript 提示输入类别名称,效果很好 var newcat=prompt("ADD CATEGORY",""); 我想将其
我正在使用 Anaconda 学习 Python。早些时候我只有 Anaconda Prompt。但是最近通过 更新了 Anaconda 之后 conda update conda 我是来看Anaco
我有一个对话框,其中有多个提示(Prompts.text, Prompts.number, Prompts.Choice,提示.确认)。虽然 Prompts.choice 和 Prompts.conf
我有一个引用参数“Year_Parameter”的值提示,以及一个包含一列(数据项表达式)的列表,该列表以这种方式引用与值提示相同的参数: #prompt('Year_Parameter')# 值提示
我正在尝试从绘制 ZSH shell 提示的函数中将当前光标位置(当前行号和列)读入一个变量中。我的目标是在提示下方显示内容,仅当有足够多的空行不会导致额外滚动时。 在交互式 shell 中,我可以使
有什么(支持的)方法可以做到这一点吗? IIRC 当使用“比 X 更暗”的色调颜色时,它通常会在某个时刻自行切换为白色,但我们的方案似乎处于边缘。 最佳答案 在 iOS5 中可以实现: [[UINav
如果生成的列表对于打开的命令提示符窗口的高度来说太长(就像这个长分支列表),它就会卡在这里。它首先显示适合窗口的内容,然后显示 :,如果按 ↓,您将到达列表的末尾,并显示 (END) 如何摆脱这种情况
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
例如,我想删除 AppData/Roaming/MyFolder/myfile.txt 中的文件。 我使用命令提示符并输入:del %userprofile%/AppData/Roaming/MyFo
例如,我想删除 AppData/Roaming/MyFolder/myfile.txt 中的文件。 我使用命令提示符并输入:del %userprofile%/AppData/Roaming/MyFo
这个问题已经有答案了: Is there a destructor for Java? (24 个回答) 已关闭 8 年前。 我正在将一些 C++ 代码移植到 Java,这些代码使用非常常见的 C++
在android中我定义了一个布局如下: ... 但由于某些原因,当我启动相应的 Activity 时,微调器的 android:promp 文本未显示。为了完整起见
代码是这样的: for(var node=iterator.iterateNext(),origin_background=node.style.background;node;node=iterat
仅当用户在提示中单击取消 时,下面的代码才会运行alert('Please enter your name!');。如果用户未在提示框中输入任何内容并单击 Enter,我试图让代码也运行所述警报。但是
我想知道是否可以根据当前路径或pwd值更改PS1? 例如: cd /home/user/directory1 PS1=[\e[1;32m\u\e[m@\e[1;34m\h\e[m \e[1;33m\W
这是一个奇怪的问题,我无法通过 Google 找到好的答案(当然,我总是有可能会失败)。在我的 bash 提示符下,如果我给提示符的一部分涂上颜色,当我按“向上”键转到上一个命令时,在超过一定数量的字
有没有办法验证javascript中调用window.prompt()显示的提示框的输入textBox的文本? 我的意思是,如果输入文本框中写入的字符串包含数字或我定义的其他非法字符等,则在单击“确定
我正在努力使用 HTML 代码来验证外部 js 文件中 Javascript Prompt() 的输入。我了解调用 Javascript 函数进行验证,并且知道如何编写该函数,但是如何“监听”HTML
我是一名优秀的程序员,十分优秀!