C ++ console progress indicator

What would be a simple way to implement a console-based progress indicator for the task being performed, but I cannot predict how long it will take?

I used to do this when I was encoded in Clipper, and that was just a matter of repeating the letters '/', '-', '\', '|' and positioning them in the same place.

Anyway / links / libs for this (or something similar) in C ++?

The purpose of this is * nix environment.

Editing:

  • changed the title to be more consistent and general;
  • target environment added.
+7
c ++ linux console
Sep 15 '09 at 3:18
source share
4 answers

A very simple way to do this is to print a line followed by the character '\r' . This is a carriage return on its own and on most consoles, it returns the cursor to the beginning of the line without moving down. This allows you to overwrite the current line.

If you write to stdout or cout or clog, remember that fflush or std :: flush stream so that it immediately displays a string. If you write stderr or cerr, then the stream is unbuffered and all output is immediate (and inefficient).

A more complicated way to do this is to use a screen-drawing library such as curses. Windows consoles have several other ways to set them up for direct recording to the screen, but I donโ€™t know what they are.

+24
Sep 15 '09 at 3:27
source share

You can 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<num_iterations; ++i) { putchar(spin_chars[i % sizeof(spin_chars)]); fflush(stdout); usleep(100000); putchar('\b'); } } 

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

Edit (curse pattern)

This should help you:

 #include <curses.h> #include <unistd.h> void spinner(int spin_seconds) { static char const spin_chars[] = "/-\\|"; unsigned long i, num_iterations = (spin_seconds * 10); for (i=0; i<num_iterations; ++i) { mvaddch(0, 0, spin_chars[i & 3]); refresh(); usleep(100000); } } int main() { initscr(); /* initializes curses */ spinner(10); /* spin for 10 seconds */ endwin(); /* cleanup curses */ return 0; } 

Be sure to contact -lcurses or -lncurses . This should work on any UNIX as well.

+10
Sep 15 '09 at 3:37
source share

Boost has a progress library that can help some of these things.

+2
01 Oct '09 at 15:44
source share

Wow, clipper , maybe you say that @row, col are things built into the language? (Only a rhetorical question ...)

You can do simple progress indicators with printf: you can leave a trailing new line. You can obviously start or end a line with \ b to overlay characters. It is easy to make a traditional - \ | / look this way.

I remember that the recommendations of Eclipse UI recommended indicators of progress, regardless of how much you were able to talk about real progress. I think the theory is that everything is better than nothing, and just doing everything possible.

The only trick you are likely to need can potentially lead to line buffering. Be sure to fflush(stdout) after each output operation. (Or ostream :: flush ())

+1
Sep 15 '09 at 3:38
source share



All Articles