Directory
the realization of the general version progress bar
Create a PROCESS.C C program file in the process directory to create a makefile file that implements the ability to compile PROCESS.C C code
the realization of color progress bar
Linux output color characters to modify the ordinary version of the PROCESS.C program
the realization of the general version progress bar
1.1 Create a proprietary directory for this program
① first creates the process directory through the mkdir process;
②CD process, into the process directory;
③vim PROCESS.C, create C program and enter to start programming;
④ Common version of the progress bar of the C language implementation code:
#include <stdio.h>
#include <unistd.h>
int main () {
char buf[101] = {0};//holds the progress bar
to output with BUF size_t i = 0;
Const char* label = "-/|\\";
for (; i<50;++i) {
buf[i] = ' # ';
printf ("[%lu%%][%c][%-50s]\r", i*2,label[i%4],buf);
/*[%lu%%]->i*2, because I max is 50, this is the output progress
const char* label = "-/|\\" in the form of hundred, + [%c] + label[i%4] The
meaning of these sentences is that as the loop progresses, Output a single character of the string that the label refers to, because it is output in the form of [%c],
so it takes up only one character bit and eventually forms a continuously rotating horizontal line (with the illusion of a relatively small time interval)/
fflush (stdout);
/*fflush (stdout) is the cleaning of the standard output stream, the timely printing of data to the screen, where is the uninterrupted output buf*/
usleep (100000);
}
printf ("\ n");
return 0;
}
1.2 Create a makefile file that implements the ability to compile PROCESS.C C code
① Create makefile files in the process directory, vim makefile;
② Enter the code under the makefile file, as shown in the figure:
③ in the terminal input make, this will generate an executable process process, and then enter the./process, the program will be executed.
2, the realization of the color progress bar 2.1 Linux Output color characters
The ① control code looks like this:
\033[0m Turn off all properties
\033[1m set high brightness
\033[4m underline
\033[5m flashing \033[7m \033[8m
----\033[30m 37m set foreground color
\033[40m----\33[47m Set background color
\033[na cursor up n line
\033[NB cursor down n row
\033[nc cursor right n row
\033[nd Cursor Left n line
\033[y;xh set cursor position
\033[2j Clear screen
\033[k erase content from cursor to line end
\033[s save cursor position
\033[u restore cursor position
\ 033[?25l Hidden cursor
\033[?25h display cursor
② Background color:
40: Black 41: Crimson 42: Green 43: Yellow 44: Blue 45: Purple 46: Dark green 47: White
③ Foreground color:
30: The Black 31: Red 32: Green 33: Yellow 34: Blue 35: Purple 36: Dark green 37: White
④ format:
printf ("\033[background color code; font color code M" to output the string "\033[0m"),
printf ("\e[background color code; font color code m%s\e[0m", "string to Output");
Description: The control character is to open a style, the output needs to be closed when the style can be terminal back to the original state,\033 and \e function the same .
"Example":
printf ("\e[32m%s\e[0m\n", "Hello World");
\e[32m to turn on the green foreground color style, \e[0m to turn off all styles, and if not, all output fonts are green foreground colors.
2.2 Modify the ordinary version of the PROCESS.C program
"program as shown in the following image":
"program Run Results":
The progress bar is red at the top 50% and the back turns green: