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

后端 未结 4 959
北恋
北恋 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:51

    They probably use the fancy ncurses library but on my Linux for my personal command-line tools I simply send '\r' to move the cursor back to the start of the line to overwrite it with new progress information.

    #include 
    #include 
    #include 
    
    int main()
    {
        for(auto i = 0; i < 100; ++i)
        {
            std::cout << "\rprogress: " << i << "%        " << std::flush;
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    
        std::cout << "\rprogress: DONE             " << std::flush;
    }
    

提交回复
热议问题