Search code examples
node.jsblessed

How to read STDIN and start blessed terminal app?


I am building a Node.js App. I'm trying to read STDIN buffer, parse it and then start my blessed program, but if I trying to read STDIN my blessed program closed instantly. Also, input doesn't work.

Here is an example:

// 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
})

Create some app:

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

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

Run program echo something | node index.js. App is closing instantly.


Solution

  • Found solution myself:

    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,
      })