Node.js 清除NodeJS流中的所有行

Node.js 清除NodeJS流中的所有行,node.js,stream,Node.js,Stream,process.stdout.clearLine删除最新的行。如何从标准输出中删除所有行 输出为: 1 3 4 我想从标准输出中删除所有行,而不是最新的行,因此输出将为: 3 4 不知道这是否有助于您尝试以下代码: var out = process.stdout; var numOfLinesToClear = 0; out.write("1\n"); // prints `1` and new line ++numOfLinesToClear; out.write("2\n"); +

process.stdout.clearLine删除最新的行。如何从标准输出中删除所有行

输出为:

1
3
4
我想从标准输出中删除所有行,而不是最新的行,因此输出将为:

3
4

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

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);

你也可以试试这个;process.stdout.write'\u001B[2J\u001B[0;0f';这与在命令行上发出clear命令具有相同的效果!可能会有帮助!

尝试以下方法:-

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)
以下打印函数可以获取具有任意换行数的字符串:

函数打印输入{ 常量numberOfLines=input.match/\n/g | |[]长度; process.stdout.clearScreenDown; process.stdout.writeinput; process.stdout.cursorTo0; process.stdout.moveCursor0,-numberOfLines; } 您可以像这样尝试:

//以下内容将打印出不同的行长度 setInterval=>{ 如果Math.random>0.7{ 打印“嘿${Date.now}” 我希望你玩得开心!${Date.now} 再见${Date.now}`; }否则,如果Math.random>0.3{ 打印“Hello${Date.now}” 再见${Date.now}`; }否则{ 打印“仅一行${Date.now}”; } }, 1000;
这太棒了!谢谢!我知道这一点,但我想替换行而不是添加新行。好的,对不起!我将在这里留下我的答案,因为我相信它适用于“如何从stdout中删除所有行”,关于你的问题,所以我希望它能帮助其他人
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)