displaying % completed while executing in C
I have a C program that is taking like 5 minutes to execute. So I was thinking if I can like show the percentage completed or any type of interaction for the user while executing, since a blinking cursor is a bit dull. I was 开发者_运维知识库thinking of displaying percentage but can I like erase somehow, eg if task 1 ended I put 25% then after task 2 ends the 25% becomes 50%?
Give me your input on some good interactions cheers!=)
This is essentially two questions rolled in to one:
- How to calculate percentage based on task flow
- How to present the calculation
The first depends on how your program is constructed, and is probably easiest answered by thinking about the problem within the context of your program flow. The second can be done in a myriad of ways, of which the simplest have been explained by others already, namely:
- Using
\r
to jump to the beginning of the line - Using
\b
x number of times to move the cursor back
I've used a third way which hasn't yet been mentioned, and that is to save and restore the cursor position. This allows you to arbitrarily move the cursor around.
\e[s
stores the current cursor position\e[u
restores the cursor to that position
Here's an example:
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[]) {
int i;
int tasks = 25;
printf("Progress:\e[s");
for(i = 0; i < tasks; i++) {
int pct = ((float) i / tasks) * 100;
printf(" %2d (%3d%%)\e[u", i, pct);
fflush(stdout);
sleep(1);
}
return(0);
}
Note that we don't care where the beginning of the line is: we only reprint the actual percentage. You'll notice that the cursor position is now visible right before the percentage, but you can arbitrarily move it anywhere you like.
This solution assumes that your terminal is capable of understanding these ANSI commands, which can differ from terminal to terminal. Although I think the aforementioned approach is relatively "safe", have a look at terminfo / ncurses for more on that.
Changelog:
- Rewrote the last paragraph
- Replaced my initial bash example with a C example
When printing in a console, you can use the special character "\r" which, as its name suggests (Carriage Return), puts the cursor on the first character on the current line.
You can then overwrite the previous percentage value, as long as you don't print a "\n" character, and you make sure that the new string you print is at least as long as the previous one.
What I mean is, for example, you print "25.5 %\r", then "26 %\r", your display will show something like
26 % %
To make sure this doesn't happen, then print a few whitespaces between % and \r.
You can use \r
to reset the cursor to the beginning of the line and then reprint the statement over the current text.
Use an int which keeps the number of tasks completed and call printf somehow like this every time you complete a task:
printf("Completed: %d %%\r", tasks_completed * 25);
Simply divide the tasks you have completed by the total tasks you need to do and you have a percent (If you want to remove the . in the number * it by 100)
This allows printing on one string with returning to the beginning:
printf("\b\b\b%02d%%", percent);
I.e. step back 3 chars (if any) and then print exactly 3 characters.
Do not forget to flush. You can print only a dot.
printf(".");fflush(stdout);
But you can also do this (\b is backspace).
printf("%02d%%\b\b\b",score);fflush(stdout);
It may be difficult sometimes to divide the code into say 100 parts,to display % of completion. In such cases you can include 'system time' into account.
write time taken in previous execution into a file.
set that time period to 100%,and print percentage of completion according to time elapsed by accessing system clock. Synchronize percentage completion in some interval like 25% or 50%,with actual program execution.
at last,show printing 100% till actually program ends.
anyway,this is just a different view.
精彩评论