Linux下實現彩色進度條程式
阿新 • • 發佈:2018-10-31
程式碼:
#include <stdio.h> #include <unistd.h> #include <string.h> int main() { int i = 0; char bar[101]; const char *lable = "|/-\\"; for (i = 0; i <= 100; i++) { bar[i] = '\0'; if (i < 40) printf("\033[1;31;44m%s\033[0m [%d%%][%c]\r", bar, i, lable[i % 4]); else if (i < 80) printf("\033[1;31;43m%s\033[0m [%d%%][%c]\r", bar, i, lable[i % 4]); else printf("\033[1;31;41m%s\033[0m [%d%%][%c]\r", bar, i, lable[i % 4]); fflush(stdout); bar[i] = ' '; usleep(100000); } printf("\n"); return 0; }
Makefile:
test:test.c
gcc test.c -o test
.PHONY:clean
clean:
rm -f test.i test.s test.o test
執行過程: