Linux applet - my color progress bar
Start to do some small programs under linux. The first one is the progress bar. In fact, it is a pseudo progress bar. It's based on time.
Let's look at the code first:
#include <stdio.h> #include <unistd.h> #include <string.h> #define NONE "\e[0m" #define BLACK "\e[0;30m" #define L_BLACK "\e[1;30m" #define RED "\e[0;31m" #define L_RED "\e[1;31m" #define GREEN "\e[0;32m" #define L_GREEN "\e[1;32m" #define BROWN "\e[0;33m" #define YELLOW "\e[1;33m" #define BLUE "\e[0;34m" #define L_BLUE "\e[1;34m" #define PURPLE "\e[0;35m" #define L_PURPLE "\e[1;35m" #define CYAN "\e[0;36m" #define L_CYAN "\e[1;36m" #define GRAY "\e[0;37m" #define WHITE "\e[1;37m" #define BOLD "\e[1m" #define UNDERLINE "\e[4m" #define BLINK "\e[5m" #define REVERSE "\e[7m" #define HIDE "\e[8m" #define CLEAR "\e[2J" #define CLRLINE "\r\e[K" //or "\e[1K\r""]]" int main(void) { char bar[102] = {'\0'}; const char *label = "|/-\\"; int i = 0; char space[100]; int j = 0; for (j = 0; j < 100; j++) space[j] = ' '; while (i <= 100) { if (i < 25) printf("\e[0;31m[\e[0m" "\e[0;31;41m%-s\e[0m" "%s" "\e[0;31m][%d%%][%c]\e[0m\r", bar, space, i, label[i % 4]); else if (i < 100) printf("\e[0;33m[\e[0m" "\e[0;33;43m%-s\e[0m" "%s" "\e[0;33m][%d%%][%c]\e[0m\r", bar, space, i, label[i % 4]); else printf("\e[0;32m[\e[0m" "\e[0;32;42m%-s\e[0m" "%s" "\e[0;32m][%d%%][%c]\e[0m\r", bar, space, i, label[i % 4]); fflush(stdout); space[99 - i] = '\0'; bar[i++] = ' '; usleep(100000); } printf("\n"); return 0; }
bar:
- This is an array of stored characters, that is, the display content of our progress bar. Because the progress bar is 100% in general, it's enough to give him 102 spaces.
- First, save all '\ 0' in this array, so you can print as many as you want. Just type all the contents before '\ 0'. A space is saved for each loop. In fact, it's OK to save anything here, because if you want to give the background color, everything will disappear. Unless you change the color of the characters and the background color to a different color, it depends.
space:
- What is this array for? Think about it. If we print the contents of the bar array directly, plus the color, the whole line will change color, because we want [the distance between the two brackets is always 100]. If you don't add space array to control the colorless part in the middle, you have to add printf("[%-100s]..."... ") to the print bar array to make 100 colors for you. It covers what we want to show.
- So the space array is that if you type one more colored space in bar, I will type one less colorless space. To maintain a part of the 100 spaces colored, part colorless.
\r: This is to return the cursor to the beginning of the line without wrapping.
fflush(stdout): because the buffer is flushed by row, or the buffer is full and then flushed. So we need to manually refresh the contents of the buffer.
How can I make the progress bar colorful after saying so much? In fact, it's just to add a color format to printf.
For details, please refer to the blog: Different colors of printf function in linux