Command-line progress meter in C++
Hello,
In C++, in a program that runs from the terminal, I want to show some sort of progress meter. I am thinking of a revolving bar: \ | / -- \ | ,etc. - this would be stationary and the character would change to reflect revolving motion. Or of a percentage completion number: [10%] -> [36%] -> [100%] - again remain stationary and only the numbers would change. Or of a progress bar - [****> 23% ] -> [**********> 42% ], etc. this would remain stationary, and the numbers would change and the number of stars in the bar would increase. How do I do this type of dynamic change on a terminal output? |
Quote:
http://www.linux-mag.com/downloads/2...sequences.html The escape sequence that you need most is backspace. I think that backspace is ^H http://www.linux.com/articles/53729 In any case the thing to do is look up how to pass escape sequences in C++. ----------------------- Steve Stites P.S. This documentation says that in C++ backspace is passed as \b. http://linux.isoe.ch/Prog/Array/escape.html |
Quote:
|
Hello,
Quote:
|
All you need to know is to use a \r instead of \n (as was suggested) and remember to overwrite the entire used part of the line each time so that you don't have residual display data. You'll really need to use parts of the POSIX C API to make it work reliably because you'll 1) need to write to the terminal or skip the progress bar entirely if there is no terminal, 2) determine the width of the terminal so that you don't get bizarre output for, say, a 20-character terminal. Let us know when you get a basic working version of what you're doing and we'll help with the terminal part if you'd like.
ta0kira PS Backspace characters don't always perform the function of backspace in the terminal. That only happens if the terminal is set up that way. |
Hello,
Quote:
I'll post the code in a couple of days after it is written. Again, thanks for all the help! |
Hello,
Here is the code for the rotating bar: Code:
#include <iostream> I am assuming it has something to do with sleeping or counting a number of clock ticks per cycle, but I have no idea how to do that. |
Quote:
Code:
else Quote:
Code:
for (i = 0; i < 1000; i++){ |
Hello,
Quote:
Quote:
Quote:
|
Quote:
Code:
std::cout.flush(); |
Hello,
Quote:
Also, sleep() only takes an int for an argument. What if I only want it to sleep for say half a second or a tenth of a second? |
Quote:
Quote:
|
Hello,
Quote:
|
All times are GMT -5. The time now is 02:09 PM. |