我在stdout使用readline prompt.
我的代码类似于:
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()
Run Code Online (Sandbox Code Playgroud)
在实际代码中,会生成算术问题。
光标重置(使用 …