Thursday, 1 June 2017

Getting the visible number of rows (or height) of the terminal in Node.js

Looking at the TTY node module, I can access process.stdout.rows which returns "A number specifying the number of rows the TTY currently has." But obviously the available ones and not the visible ones as the following code:

let lines = process.stdout.rows

// Clear console
process.stdout.write('\x1Bc')

for (var i = 0; i < lines; i++) {
  if (i === 0) {
    console.log(1)
  } else if (i === Math.round(lines / 2)) {
    console.log('halfwayish')
  } else if (i === lines - 1) {
    console.log('end')
  } else {
    console.log('\r\n')
  }
}

outputs:

enter image description here

How can I make it so that 1, halfwayish and end are outputted, so that they are seen without having to scroll in the terminal?



via Tom

No comments:

Post a Comment