Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear all lines in NodeJS stream

Tags:

stream

node.js

process.stdout.clearLine() deletes latest line. How can I delete all lines from stdout?

var out = process.stdout;
out.write("1\n"); // prints `1` and new line
out.write("2");   // prints `2`

setTimeout(function () {
    process.stdout.clearLine(); // clears the latest line (`2`)
    out.cursorTo(0);            // moves the cursor at the beginning of line
    out.write("3");             // prints `3`
    out.write("\n4");           // prints new line and `4`
    console.log();
}, 1000);

The output is:

1
3
4

I want to delete all lines from stdout instead of latest line, so the output will be:

3
4
like image 675
Ionică Bizău Avatar asked Dec 04 '22 06:12

Ionică Bizău


2 Answers

dont know if this helps you try this code:

var out = process.stdout;
var numOfLinesToClear = 0;
out.write("1\n");   // prints `1` and new line
++numOfLinesToClear;
out.write("2\n");
++numOfLinesToClear;
process.stdout.moveCursor(0,-numOfLinesToClear); //move the cursor to first line
setTimeout(function () {   
    process.stdout.clearLine();
    out.cursorTo(0);            // moves the cursor at the beginning of line
    out.write("3");             // prints `3`
    out.write("\n4");           // prints new line and `4`
    console.log();
}, 1000);
like image 189
azero0 Avatar answered Dec 31 '22 12:12

azero0


try this:-

var x = 1, y = 2;
process.stdout.write(++x + "\n" + (++y))
function status() {
  process.stdout.moveCursor(0, -2)      // moving two lines up
  process.stdout.cursorTo(0)            // then getting cursor at the begining of the line
  process.stdout.clearScreenDown()      // clearing whatever is next or down to cursor
  process.stdout.write(++x + "\n" + (++y))
}

setInterval(status, 1000)
like image 36
Vikas Gautam Avatar answered Dec 31 '22 10:12

Vikas Gautam