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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With