清除NodeJS流中的所有行

Ion*_*zău 1 stream node.js

process.stdout.clearLine()删除最新行.如何删除所有行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);
Run Code Online (Sandbox Code Playgroud)

输出是:

1
3
4
Run Code Online (Sandbox Code Playgroud)

我想删除所有行stdout而不是最新行,因此输出将是:

3
4
Run Code Online (Sandbox Code Playgroud)

aze*_*ro0 7

不知道这是否有助于您尝试此代码:

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);
Run Code Online (Sandbox Code Playgroud)