1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
int progressBar(unsigned int current, unsigned int total, double speed) { double prcnt; int num_of_dots; char buffer[80] = {0}; int width; /* get term width */ FILE *fp; prcnt=1.0*current/total; fp = popen("stty size | cut -d" " -f2","r"); fgets(buffer, sizeof(buffer),fp); pclose(fp); width = atoi(buffer); if ( width < 32) { printf("e[1A%0.2f%s %3d%% completed.n",speed, "Mb/s",(int)(prcnt*100)); } else { num_of_dots = width - 20; char *pline_to_print = (char *)malloc( sizeof(char)*width ); int dots = (int)(num_of_dots*prcnt); memset(pline_to_print,0,width); memset(pline_to_print,'>',dots); memset(pline_to_print+dots, ' ', num_of_dots - dots); printf("e[1A %0.2fMb/s [%s] %3d%% n",speed,pline_to_print,(int)(prcnt*100)); free(pline_to_print); } return 0; } |
使用方法:
1 2 3 4 5 6 7 8 9 |
main() { int i; for(i=0;i<100;i++) { progressBar(i,99,8.55); usleep(800); } } |
转载请注明:爱开源 » 有用的代码小片段3-linux终端进度条