【问题标题】:Clear multiple lines from stdout从标准输出中清除多行
【发布时间】:2020-07-13 06:27:05
【问题描述】:

我正在尝试在 Node.JS 中向控制台写入多行,然后清除我写的所有行。

process.stdout.write(['a', 'b', 'c'].join('\n'))

setInterval(() => {
    process.stdout.clearLine(0);
    process.stdout.write(['d', 'e', 'f'].join('\n'));
}, 1000)

我见过一些可以清除整个控制台的解决方案,我不想要这个。我只是想清除我在屏幕上写的内容。

【问题讨论】:

    标签: node.js console stdout


    【解决方案1】:

    我不知道有更好的方法来做到这一点,但我所做的是将光标向上移动 1 行,然后清除该行。

    const clearLines = (n) => {
        for (let i = 0; i < n; i++) {
          //first clear the current line, then clear the previous line
          const y = i === 0 ? null : -1
          process.stdout.moveCursor(0, y)
          process.stdout.clearLine(1)
        }
        process.stdout.cursorTo(0)
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多