我正在构建一个Node.js应用程序。我正在尝试读取STDIN缓冲区,对其进行解析,然后启动我的有福程序,但是,如果我尝试读取STDIN,我的有福程序会立即关闭。
另外,输入无效。

这是一个例子:

// Read stdin into buff
const stdin = process.stdin
stdin.setEncoding('utf8')

let buff = ''
function read() {
  let chunk

  while ((chunk = stdin.read())) {
    buff += chunk
  }
}

stdin.on('readable', read)

stdin.on('end', () => {
  input = buff
})


创建一些应用程序:

const program = blessed.program()
const screen = blessed.screen({
  program: program,
  smartCSR: true,
})

// Add box with mouse and keys events, etc.


运行程序echo something | node index.js。应用即将关闭。

最佳答案

自己找到解决方案:

const ttyFd = fs.openSync('/dev/tty', 'r+')

  const program = blessed.program({
    input: tty.ReadStream(ttyFd),
    output: tty.WriteStream(ttyFd),
  })

  const screen = blessed.screen({
    program: program,
    smartCSR: true,
  })

07-24 09:38
查看更多