C++ Console Progress Indicator

后端 未结 4 663
一生所求
一生所求 2021-02-08 23:13

What would be an easy way of implementing a console-based progress indicator for a task that\'s being executed, but I can\'t anticipate how much time it would take?

I us

4条回答
  •  长情又很酷
    2021-02-08 23:53

    You could try something like:

    void
    spinner(int spin_seconds) {
        static char const spin_chars[] = "/-\\|";
        unsigned long i, num_iterations = (spin_seconds * 10);
        for (i=0; i

    Of course, this is non-standard because of the sub-second usleep() and I'm not sure if there is any guarantee that \b erases a character or not, but it works on most platforms. You can also try \r instead if \b doesn't do the trick. Otherwise, try to find a version of curses.

    Edit (curses sample)

    This should get you started:

    #include 
    #include 
    
    void spinner(int spin_seconds) {
        static char const spin_chars[] = "/-\\|";
        unsigned long i, num_iterations = (spin_seconds * 10);
        for (i=0; i

    Make sure to link with either -lcurses or -lncurses. That should work on any UNIX alike out there.

提交回复
热议问题