Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get terminal size in a piped node.js process?

I'm using Grunt to kick off a unit-test framework (Intern), which ultimately pipes another node.js process that I'm then using Charm to output results to the screen. I'm having to pass in the terminal size information from a Grunt config option, but it's a bit messy and I'd like to try and get the terminal size from within the piped process, but the standard process.stdout.cols/getWindowSize are simply unavailable as the piped process doesn't register as TTY (although Charm works fine with it all).

Any suggestions?

EDIT Just to be clear here ... the Grunt JavaScript file is running in the main node.js process, but the file I'm attempting to retrieve this info from (and where I'm therefore running people's suggested commands) is in a spawned child process.

like image 465
Martin Avatar asked Oct 19 '22 03:10

Martin


1 Answers

Try these:

  • tput cols tells you the number of columns.
  • tput lines tells you the number of rows.
  • echo -e "lines\ncols"|tput -S to get both the lines and cols

There's stty, from coreutils:

$ stty size #60 120 <= sample output

While running the below code in terminal prints the cols:

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("tput cols", puts);
like image 85
Anand Sudhanaboina Avatar answered Oct 24 '22 10:10

Anand Sudhanaboina