Linux C实现文件拷贝可变色进度条显示

printf的格式化输出中,可以设置前景色和背景色,因此进度条的颜色可以通过printf的格式化输出来实现;此外,进度条的实现主要依靠"\r"和"fflush(stdout)“来支持。”\r" 用来回到行首而不换行,fflush(stdout)是刷新屏幕输出缓冲区。只要根据进度,重新填写缓冲区内容,然后fflush()刷新缓冲区,重新在屏幕上打印缓冲区内容则可以动态实现进度条。

测试代码(在Redhat6.4中gcc编译):

/*  
* Time:2018年9月30日13:24:35
* Author:Kangruojin
* Version:V2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define MAX_STR_LEN 	101	
#define SIZE_BIT_KB		10		/* 单位为KB */

#define _PROGRESS_BAR_RED		"\033[31m[%-100s][%d%%][%c][%dKB - %dKB]\r\033[0m"

#define _PROGRESS_BAR_BLUE		"\033[34m[%-100s][%d%%][%c][%dKB - %dKB]\r\033[0m"

#define _PROGRESS_BAR_GREEN		"\033[32m[%-100s][%d%%][%c][%dKB - %dKB]\r\033[0m"

#define PRINT_RED(format,...)       		printf(_PROGRESS_BAR_RED, ##__VA_ARGS__)
#define PRINT_GREEN(format,...)   		printf(_PROGRESS_BAR_BLUE, ##__VA_ARGS__)  
#define PRINT_YELLOW(format,...)   		printf(_PROGRESS_BAR_GREEN, ##__VA_ARGS__) 

void progressBar(int pre, int cur, int cs, int fs);

int main(int argc, char **argv)
{
	if(argc != 4)
	{
		printf("parameter error:./progressBar srcFileName dstFileName buffSize\n");
		printf("buffSize choose min value is 8192,It's fast!\n");
		exit(EXIT_FAILURE);
	}

	int ret_r = 0, ret_w = 0, curSize = 0, fileSize = 0, cur = 0, pre = 0, shr = SIZE_BIT_KB;

	int bufSize = atoi(argv[3]);
	char * pBuff = (char *)malloc(bufSize);
	if(NULL == pBuff){
		perror("malloc");
	}
	
	memset(pBuff, 0, bufSize);
	
	int fdr = open(argv[1], O_RDONLY);
	int fdw = open(argv[2], O_RDWR | O_CREAT | O_TRUNC);
	if(fdw < 0 || fdr < 0){
		perror("open File");
	}

	fileSize = lseek(fdr, 0, SEEK_END);
	lseek(fdr, 0, SEEK_SET);
	
	while((ret_r = read(fdr, pBuff, bufSize)) > 0){
		if((ret_w = write(fdw, pBuff, ret_r)) != ret_r){
			perror("write");/* 写的字节数与实际写入的数目不同 */
		}
		/* 根据已复制文件大小和总文件大小计算进度(x%) */
		curSize += ret_w;
		pre = cur;
		cur = (int)(((double)curSize / fileSize) * 100);
		if(cur > pre)
		{
			progressBar(pre, cur, curSize >> shr, fileSize >> shr);
		}
		memset(pBuff, 0, ret_r);
	}
	printf("\n");
	free(pBuff);
	close(fdr);
	close(fdw);

	return 0;
}
/******************************************************************************
 * Time:2018年9月30日13:23:31
 * printf()的颜色格式:\033[{attr1};{attr2};...{attrn}m
 * 以\033即Esc的ASCII开始,跟随n个属性,以m结尾
 * eg:\033[32;41m
 * attr属性取值:
 * 0:重置所有属性;1:高亮/加粗;2:暗淡;4:下滑线;5:闪烁;6:反转;8:隐藏
 * 30~37为前景色,40~47为背景色,依次是:
 * 黑	红	绿	黄	蓝	紫	青	白
 * 30	31 	32 	33	34 	35 	36  37
 * 40	41	42	43	44	45	46	47
 * ****************************************************************************/
void progressBar(int pre, int cur, int cs, int fs)
{
	if(pre < 0 || pre > 100 || cur < 0 || cur > 100)
	{
		perror("error progress!!\n");
	}

	static char str[MAX_STR_LEN] = {0};
	static char table[] = "-\\|/";
	int i = 0;
	for(i = pre; i < cur; i++){
		str[i] = '#';
	}

	if(cur < 33){
		PRINT_RED(str, cur, table[cur%4], cs, fs);
	}
	else if(cur < 66){
		PRINT_BLUE(str, cur, table[cur%4], cs, fs);
	}
	else{
		PRINT_GREEN(str, cur, table[cur%4], cs, fs);
	}

	fflush(stdout);
}

测试结果:

在这里插入图片描述

关于printf的颜色以及其他格式化输出的内容可参考:

Linux下printf格式化输出颜色及定位
shell之echo与printf和颜

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值