Clear terminal window in Node.js readline shell

后端 未结 7 880
长情又很酷
长情又很酷 2021-01-31 05:41

I have a simple readline shell written in Coffeescript:

rl = require \'readline\'
cli = rl.createInterface process.std         


        
7条回答
  •  情话喂你
    2021-01-31 06:17

    This is the only answer that will clear the screen AND scroll history.

    function clear() {
      // 1. Print empty lines until the screen is blank.
      process.stdout.write('\033[2J');
    
      // 2. Clear the scrollback.
      process.stdout.write('\u001b[H\u001b[2J\u001b[3J');
    }
    
    // Try this example to see it in action!
    (function loop() {
      let i = -40; // Print 40 lines extra.
      (function printLine() {
        console.log('line ' + (i + 41));
        if (++i < process.stdout.columns) {
          setTimeout(printLine, 40);
        }
        else {
          clear();
          setTimeout(loop, 3000);
        }
      })()
    })()
    
    • The first line ensures the visible lines are always cleared.

    • The second line ensures the scroll history is cleared.

提交回复
热议问题