Mapping% completed at runtime in C

I have a C program that runs like 5 minutes to execute. So I thought, can I show that the percentage is complete or some type of interaction for the user at runtime, since the blinking cursor is a little dull. I thought about showing the percentage, but I like to somehow erase, for example, if task 1 is over, I put 25%, and then, after task 2 ends, 25% become 50%?

Give me your contribution to some good cheers! =)

+4
source share
8 answers

These are, in fact, two questions recounted to one:

  • How to calculate percentage based on task flow.
  • How to submit a calculation

First of all, it depends on how your program is built, and it is probably easiest to answer the thought of a problem in the context of your program flow. The second can be done in a variety of ways, of which the simplest have already been explained by others, namely:

  • Using \r to go to the beginning of a line
  • Using \b x the number of times to move the cursor back

I used the third method, which has not yet been mentioned, and this is to save and restore the cursor position. This allows you to arbitrarily move the cursor.

  • \e[s saves the current cursor position
  • \e[u restores the cursor to this position

Here is 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 line starts: we retype the actual percentage . You will notice that the cursor position is now visible directly in front of the percentage, but you can arbitrarily move it anywhere.

This solution assumes that your terminal is capable of understanding these ANSI commands, which may vary from terminal to terminal. Although I think the above approach is relatively "safe", check out terminfo / ncurses to find out more about this.

Changelog:

  • Rewrote the last paragraph
  • Replaced my original bash example C example
+7
source

When printing in the console, you can use the special character "\ r", which, as its name implies ("Carriage Return"), places the cursor on the first character in the current line.

Then you can overwrite the previous percentage value if you do not print the character "\ n", and make sure that the new line you are printing is not less than the previous one.

I mean, for example, you type "25.5% \ r", then "26% \ r", something like

26 % %

To make sure this does not happen, type a few spaces between% and \ r.

+5
source

You can use the \r to reset cursor at the beginning of a line, and then retype the instruction for the current text.

+1
source

Use an int that stores the number of tasks completed and invokes printf anyway, as soon as you complete the task:

 printf("Completed: %d %%\r", tasks_completed * 25); 
+1
source

Just divide the tasks that you have completed into general tasks that you need to complete, and you have a percentage (if you want to delete. In number * it is 100)

+1
source

This allows you to print on the same line and return to the beginning:

 printf("\b\b\b%02d%%", percent); 

those. cancel 3 characters (if any), and then print exactly 3 characters.

+1
source

Remember to make a flash. You can print only a dot.

 printf(".");fflush(stdout); 

But you can also do this (\ b is the backspace).

 printf("%02d%%\b\b\b",score);fflush(stdout); 
0
source

Sometimes it can be difficult to split the code into 100 parts to display% completion. In such cases, you can enable "system time".

recording time recorded in the previous version to a file. set this time period to 100% and completion completion percentage according to the time elapsed to access the system clock. Synchronize percent completion over an interval, such as 25% or 50%, with the actual execution of the program.
Finally, show print 100% before the actual program. in any case, this is just a different opinion.

0
source

Source: https://habr.com/ru/post/1339176/


All Articles