Clear all lines in NodeJS stream
Asked Answered
A

4

5

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
Aletaaletha answered 21/5, 2014 at 5:23 Comment(0)
J
11

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);
Jaysonjaywalk answered 21/5, 2014 at 8:14 Comment(0)
W
4

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)
Waddle answered 31/12, 2015 at 11:22 Comment(0)
C
1

You can try this one also; process.stdout.write('\u001B[2J\u001B[0;0f'); this has the same effect of issuing the clear command on the command line! Maybe that helps!

Cribbage answered 21/5, 2014 at 22:11 Comment(2)
I know that, but I want to replace lines instead of adding new lines.Maremma
Ok, sorry! I will leave my answer here as i believe it fits for 'How can I delete all lines from stdout', on your question, so I hope it helps anyone else! (:Cribbage
B
1

The following print function can take a string with any number of line breaks:

function print(input) {
  const numberOfLines = (input.match(/\n/g) || []).length;
  process.stdout.clearScreenDown();
  process.stdout.write(input);
  process.stdout.cursorTo(0);
  process.stdout.moveCursor(0, -numberOfLines);
}

You can try it out like:

// the following will print out different line lengths
setInterval(() => {
  if (Math.random() > 0.7) {
    print(`Hey ${Date.now()}

    I hope you are having a good time! ${Date.now()}

    Bye ${Date.now()}`);
  } else if (Math.random() > 0.3) {
    print(`Hello ${Date.now()}

    Good bye ${Date.now()}`);
  } else {
    print(`just one line ${Date.now()}`);
  }
}, 1000);
Bombazine answered 3/4, 2020 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.