Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update data on multiple lines of console

I want to display data on two lines of the console. I just want to update the two lines everytime.

What I have done till now is -

var _logInline = function(alpha, bravo) {
    process.stdout.cursorTo(0, 0);
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(alpha.toString());
    process.stdout.write('\n');
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(bravo.toString());
    process.stdout.write('\n');

};

var delay = 1000;
var time = 0;
setInterval(function() {
    time++;
    _logInline('alpha-' + time, 'bravo-' + time * time);
}, delay);

The obvious problem with this solution is that the cursor goes to the top of the window. I don't want that, instead it should display the content where ever the cursor is at the moment. Probably I need to get the current cursor position first in my logic. Is there a way to do that?

Alternative and the most preferred solution would be to get a lib which can do the same thing

EDIT: I have seen some questions on stackoverflow which give an option of logging without new line but this is not exactly what I want. I want multiple no-new-line logging.

like image 424
tusharmath Avatar asked Jul 02 '13 11:07

tusharmath


1 Answers

ncurses is the most powerful library I've used to control the terminal, there is an excellent npm package by mscdex that binds to the c library https://npmjs.org/package/ncurses

But it may be a little overkill for your needs, here is an alternative solution but it involves using a bash script:

Based on this gist I've put together the following code adapted to your example, you can download it from gist or read it here, don't forget to give exec permissions to the bash script with:

  chmod +x cursor-position.sh 

cursor-position.js

module.exports = function(callback) {
  require('child_process').exec('./cursor-position.sh', function(error, stdout, stderr){
    callback(error, JSON.parse(stdout));
  });
}

cursor-position.sh

#!/bin/bash
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
# http://stackoverflow.com/questions/2575037/how-to-get-the-cursor-position-in-bash
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
# on my system, the following line can be replaced by the line below it
echo -en "\033[6n" > /dev/tty
# tput u7 > /dev/tty    # when TERM=xterm (and relatives)
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1))    # strip off the esc-[
col=$((${pos[1]} - 1))
echo \{\"row\":$row,\"column\":$col\}

index.js

var getCursorPosition = require('./cursor-position');

var _logInline = function(row, msg) {
  if(row >= 0) row --; //litle correction
  process.stdout.cursorTo(0, row);
  process.stdout.clearLine();
  process.stdout.cursorTo(0, row);
  process.stdout.write(msg.toString());
};

var delay = 1000;
var time = 0;

//Start by getting the current position
getCursorPosition(function(error, init) {
  setInterval(function() {
      time++;
      _logInline(init.row, 'alpha-' + time);
      _logInline(init.row + 1, 'bravo-' + time * time);
  }, delay);
});
like image 160
alfonsodev Avatar answered Oct 18 '22 05:10

alfonsodev