Linux下实现彩色进度条程序

代码:

#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

运行过程:

猜你喜欢

转载自blog.csdn.net/xinwenhuayu/article/details/82851709