5

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.

Martin
  • 1,289
  • 3
  • 13
  • 22

2 Answers2

3

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);
  • How are you suggesting running these from within a node program? Do you have some example code? – Martin Sep 15 '15 at 13:06
  • 1
    Unfortunately, this hasn't worked. In my grunt JS file I'm getting the correct values by using process.stdout.rows and process.stdout.columns, which come out to 40 and 178 respectively. When I get the information from the childProcess call to tput, I'm getting 24 and 80 instead. I'm actually getting some information, which I wasn't before, but it's not enough to remove the external dependency on passed-in variables. – Martin Sep 18 '15 at 08:27
0

The pty.js module can make a child act like a regular terminal.

var pty = require('pty.js');

var term = pty.spawn('bash', [], {
  name: 'xterm-color',
  cwd: process.env.HOME,
  env: process.env
});

term.on('data', function(data) {
  console.log(data);
});

term.write('ls\r');
term.resize(100, 40);
term.write('ls /\r');

console.log(term.process);
Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
  • Unfortunately, all this gives me is ... "The terminal size is 0x0" – Martin Sep 18 '15 at 08:36
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Sep 18 '15 at 09:26
  • Ok that's a differrent problem. See new solution. – Jason Livesay Sep 18 '15 at 10:26
  • It looks like that code needs you to specify cols and rows, however what I want to do it to discover the current cols and rows of the piped terminal I'm inside. Am I missing something here? – Martin Sep 18 '15 at 10:56
  • If you spawn it this way then normal term commands like size will work. Actually think you can leave rows/cols off. One idea is maybe instead of spawning another script process you could just `require()` it? – Jason Livesay Sep 18 '15 at 11:32
  • I'm not spawning the child process. The grunt commands launch intern testing framework, whose output is piped back to the current terminal. It's from within that piped terminal that I'm attempting to discover the size. – Martin Sep 18 '15 at 12:31
  • OK. You could include more details. Maybe fork `grunt-shell` or `grunt-exec` to use `pty.js`. Or `gulp` might work better. Or `bash` or `npm test` with a `package.json` script that calls intern. – Jason Livesay Sep 18 '15 at 12:47
  • None of those really address my question though ... it was pretty explicit :) – Martin Sep 18 '15 at 13:00
  • The question wasn't explicit enough. The issue is the specific grunt plugins which you didn't specify. All of those DO address your question. – Jason Livesay Sep 18 '15 at 22:06
  • 2
    The question was "How can I get terminal size in a piped node.js process?". Inside the further information, I state that "I'd like to try and get the terminal size from within the piped process". If you don't know how, that's not a problem, but please do try to answer the question. – Martin Sep 19 '15 at 19:26