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.
D.Shawley Sep 15 '09 at 3:37 2009-09-15 03:37
source share