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

后端 未结 4 962
北恋
北恋 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条回答
  •  攒了一身酷
    2021-02-13 18:55

    Looking at GNU wget repo on GitHub -- progress.c

    It seems they do it the same way i.e. print a \r and then overwrite.

    /* Print the contents of the buffer as a one-line ASCII "image" so
       that it can be overwritten next time.  */
    
    static void
    display_image (char *buf)
    {
      bool old = log_set_save_context (false);
      logputs (LOG_VERBOSE, "\r");
      logputs (LOG_VERBOSE, buf);
      log_set_save_context (old);
    }
    

提交回复
热议问题