代码之家  ›  专栏  ›  技术社区  ›  Anton Medvedev

如何读取STDIN并启动终端应用程序?

  •  1
  • Anton Medvedev  · 技术社区  · 7 年前

    我正在构建一个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 . 应用程序正在立即关闭。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Anton Medvedev    7 年前

    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,
      })
    
    推荐文章