#include<stdio.h>
#include<unistd.h>
void draw_progress(int pos)
{
pos = (pos < 0) ? 0 : pos;
pos = (pos > 100) ? 100 : pos;
char bar[102]={'\0'};
const char *state = "-\\|/";
printf(" \033[0;31;43m%-100s\033[0m[%d%%]\r", bar, pos);
printf("[%c]\033[0;46m%*s\033[0m\r", state[pos % 4], pos, bar);
fflush(stdout);//清除读写缓冲区
}
int main()
{
int i = 0;
for(; i < 100; i++){
printf("%d%% \r", i);
usleep(20000);
fflush(stdout);
}
printf("\n");
i = 0;
while(i <= 100)
{
draw_progress(i);
usleep(200000);
i++;
}
printf("\n");
return 0;
}
效果如下: