I. concept of progress bar
The progress bar is the real-time display of the processing speed, completion, the size of the remaining unfinished tasks and the time that may need to be processed in the form of pictures when the computer is processing the tasks, generally in the form of rectangular bars.
Several elements of progress bar
The necessary elements for displaying the progress bar are the percentage of completed tasks, the display of task status and the representation of completed tasks. This paper uses numbers to describe the percentage of tasks completed, symbols' - ',' / ',' | ',' \ 'to indicate the status of tasks, and continuous' #' to indicate the amount of tasks completed. At the same time, in order to display the process, the time should be delayed.
III. some versions of progress bar
Based on the above analysis, the first version can be written:
#include<stdio.h> 2 #include<unistd.h> 3 int main() 4 { 5 char string[4]={'-','/','|','\\'}; 6 char buf[100]={0}; 7 int i=0; 8 for(;i<=100;i++) 9 { 10 printf("[%d]",i); 11 printf("[%c]",string[i%4]); 12 buf[i]='#'; 13 printf("%s\n",buf); 14 usleep(100*1000); 15 } 16 return 0; 17 } ~
Partial results:
It took a long time for this version to produce results, and the result was 100 lines of output.
The reason for the long-term result: the data is still in the buffer. The printf statement stores all the data to be output in the buffer. Only when the data in the buffer is full can one output be made.
Improvement: use fflush(stdout) function to output standard output function to display screen.
Reason for outputting 100 lines: used \ n \ NIS line feed. Loop 100 times, output 100 lines.
Improvement: change \ n to \ R \ R is the carriage return character, so that the cursor returns to the starting position of the line and does not start a new line. If the line breaks, a new line will be created.
The improved version 2 is:
#include<stdio.h> 2 #include<unistd.h> 3 int main() 4 { 5 char string[4]={'-','/','|','\\'}; 6 char buf[100]={0}; 7 int i=0; 8 for(;i<=100;i++) 9 { 10 printf("[%d]",i); 11 printf("[%c]",string[i%4]); 12 buf[i]='#'; 13 printf("%s\r",buf); 14 fflush(stdout); 15 usleep(100*1000); 16 } 17 return 0; 18 }
The results show the progress dynamically in one row. But the color is all black. In order to make it more beautiful, set the color for the progress and implement version 3.
#include<stdio.h> 2 #include<unistd.h> 3 #define BLACK "\e[0;30m" 4 #define YELLOW "\e[1;33m" 5 #define GREEN "\e[0;32m" 6 #define BLUE "\e[0;34m" 7 #define RED "\e[0;31m" 8 int main() 9 { 10 char string[4]={'-','/','|','\\'}; 11 char buf[100]={0}; 12 int i=0; 13 for(;i<=100;i++) 14 { 15 buf[i]='#'; 16 printf(BLACK"[%d]",i); 17 if(i<=25) 18 { 19 printf(YELLOW"[%c]",string[i%4]); 20 printf(YELLOW"[%s]\r",buf); 21 } 22 else if(i<=50) 23 { 24 printf(GREEN"[%c]",string[i%4]); 25 printf(GREEN"[%s]\r",buf); 26 } 27 else if(i<=75) 28 { 29 printf(BLUE"[%c]",string[i%4]); 30 printf(BLUE"[%s]\r",buf); 31 } 32 else{ 33 printf(RED"[%c]",string[i%4]); 34 printf(RED"[%s]\r",buf); 35 } 36 fflush(stdout); 37 usleep(100*1000); 38 } 39 return 0; 40 }
Finally realize the color progress bar.
IV. several points involved in the preparation of progress bar
1. Difference between \ R and \ n
\n: new line, starting again.
\r: carriage return, do not wrap, return the cursor to the beginning of the line.
Sleep and usleep
Both of them are control system sleep, sleep in seconds, usleep in microseconds. 0.1 seconds is set in this article.
3.fflush function
The fflush function is often used to process disk files. fflush() forces the data in the buffer to be written back to the file specified by the stream parameter. In this paper, fflush writes the buffer data to the standard output stream stdout.
4.unistd header file
unistd header file is the most important header file in Linux system programming, which contains the API (application system programming interface) provided by the operating system to the programmer.