小编arv*_*ve0的帖子

在 Node 中使用 readline prompt/setPrompt 时无法清除行

我在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)

在实际代码中,会生成算术问题。

这是输出的屏幕截图。

发生什么了

光标重置(使用 …

console prompt node.js

5
推荐指数
1
解决办法
1863
查看次数

标签 统计

console ×1

node.js ×1

prompt ×1