Nodejs - how to maintain status on top of stdout

I am trying to output something like this:

counter is: 10            <= fixed line and auto updating
console.logs, etc...      <= other console.logs, errors, defaul outputs
console.logs, etc...
console.logs, etc...
console.logs, etc...

Is it possible?

I tried with process.stdout.write (), but it does not work.

var counter = 0;
setInterval(function(){
    counter++;
    process.stdout.write("counter is " + counter + " \r");
}, 500);

setInterval(function(){
    console.log('some output');
}, 1500);
+4
source share
3 answers

Here is an example using blessed :

var blessed = require('blessed');

var screen = blessed.screen(),
    body = blessed.box({
      top: 1,
      left: 0,
      width: '100%',
      height: '99%'
    }),
    statusbar = blessed.box({
      top: 0,
      left: 0,
      width: '100%',
      height: 1,
      style: {
        fg: 'white',
        bg: 'blue'
      }
    });

screen.append(statusbar);
screen.append(body);

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
  return process.exit(0);
});

function status(text) { statusbar.setContent(text); screen.render(); }
function log(text) { body.insertLine(0, text); screen.render(); }

var c = 1;
setInterval(function() {
  status((new Date()).toISOString());
  log('This is line #' + (c++));
}, 100);

Here is a simpler example that has almost the same effect (the status bar does not fill the extra space with the background color):

var screen = blessed.screen(),
    body = blessed.box({
      top: 0,
      left: 0,
      width: '100%',
      height: '100%',
      tags: true
    });

screen.append(body);

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
  return process.exit(0);
});

function status(text) {
  body.setLine(0, '{blue-bg}' + text + '{/blue-bg}');
  screen.render();
}
function log(text) {
  body.insertLine(1, text);
  screen.render();
}

var c = 1;
setInterval(function() {
  status((new Date()).toISOString());
  log('This is line #' + (c++));
}, 100);
+6
source

In addition, there are a lot of node modules, which can help you, ( blessed, ncurses, ansi, termhelper), for educational purposes you can also easily do this with the vanilla is node process.stdout.moveCursor:

var logs = [];
function log(text) {
  logs.push(text);
  console.log(text);
}
function changeCounter(n) {
  process.stdout.moveCursor(0, -logs.length - 1);
  printCounter(n);
  logs.forEach(function (log) { console.log(log) });
}
function printCounter(n) {
  console.log('Counter is:', n);
}

// Now lets test
printCounter(0);
var i = 1;
setInterval(function () {
  log('meoww');
  changeCounter(i++);
});

Although you need to write additional code to prevent terminal overflow.

+3

The traditional library for this kind of thing (drawing text at the bottom of the screen) is a β€œscourge” ... there are bindings for Node.js, but there is also a β€œblessed” (ha ha) that looks more convenient: https: // github .com / chjj / blessed

+2
source

Source: https://habr.com/ru/post/1541884/


All Articles