【问题标题】:Clear all lines in NodeJS stream清除 NodeJS 流中的所有行
【发布时间】:2014-05-21 05:23:02
【问题描述】:

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

输出是:

1
3
4

我想从stdout 中删除所有行而不是最新行,所以输出将是:

3
4

【问题讨论】:

    标签: node.js stream


    【解决方案1】:

    试试这个:-

    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)
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这太棒了!谢谢!
      【解决方案3】:

      下面的print 函数可以接受带有任意数量换行符的字符串:

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

      你可以试试:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我知道,但我想替换行而不是添加新行。
        • 好的,抱歉!我会在这里留下我的答案,因为我相信它适合'我如何从标准输出中删除所有行',关于你的问题,所以我希望它可以帮助其他人! (:
        猜你喜欢
        • 2016-09-02
        • 2015-04-17
        • 2011-11-10
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 2016-03-14
        • 1970-01-01
        相关资源
        最近更新 更多