How do command line tools change their output after outputting it?

后端 未结 4 964
北恋
北恋 2021-02-13 18:24

I\'ve noticed that a lot of command line tools, wget for example, will show progress as a number or progress bar that advances as a process is completed. While the question isn\

4条回答
  •  梦毁少年i
    2021-02-13 18:50

    I can only speak about node.js, but the built-in readline module has some very basic screen handling functionality built-in. For example:

    var readline = require('readline');
    var c = 0;
    var intvl = setInterval(function() {
      // Clear entirety of current line
      readline.clearLine(process.stdout, 0);
      readline.cursorTo(process.stdout, 0);
      process.stdout.write('Progress: ' + (++c) + '%');
      if (c === 100)
        clearInterval(intvl);
    }, 500);
    

    There are also third party modules if you want to get fancier, such as multimeter/meterbox and blessed/blessed-contrib.

    Generally speaking though, some programs use ncurses, while others simply just manually output the ANSI escape codes to clear and redraw the current line.

提交回复
热议问题