如何阅读STDIN并启动祝福终端应用程序



我正在构建一个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,
})

最新更新