31 Star 62 Fork 28

狮子的魂 / ltpro

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ltpro.c 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
狮子的魂 提交于 2015-12-07 11:36 . code tab to 4 space
/**
* linux terminal progress bar (no thread safe).
* @package progress.c
*
* @author chenxin <chenxin619315@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ltpro.h"
/**
* initialize the progress bar.
* @max = 0
* @val = 0
*
* @param style
* @param tip words.
*/
extern void progress_init(
progress_t *bar, char *title, int max, int style)
{
bar->chr = '#';
bar->title = title;
bar->style = style;
bar->max = max;
bar->offset = 100 / (float)max;
bar->pro = (char *) malloc(max+1);
if ( style == PROGRESS_BGC_STYLE )
memset(bar->pro, 0x00, max+1);
else {
memset(bar->pro, 32, max);
memset(bar->pro+max, 0x00, 1);
}
}
extern void progress_show( progress_t *bar, float bit )
{
int val = (int)(bit * bar->max);
switch ( bar->style )
{
case PROGRESS_NUM_STYLE:
printf("\033[?25l\033[31m\033[1m%s%d%%\033[?25h\033[0m\r",
bar->title, (int)(bar->offset * val));
fflush(stdout);
break;
case PROGRESS_CHR_STYLE:
memset(bar->pro, '#', val);
printf("\033[?25l\033[31m\033[1m%s[%-s] %d%%\033[?25h\033[0m\r",
bar->title, bar->pro, (int)(bar->offset * val));
fflush(stdout);
break;
case PROGRESS_BGC_STYLE:
memset(bar->pro, 32, val);
printf("\033[?25l\033[31m\033[1m%s\033[41m %d%% %s\033[?25h\033[0m\r",
bar->title, (int)(bar->offset * val), bar->pro);
fflush(stdout);
break;
}
}
//destroy the the progress bar.
extern void progress_destroy(progress_t *bar)
{
free(bar->pro);
}
C
1
https://gitee.com/lionsoul/ltpro.git
git@gitee.com:lionsoul/ltpro.git
lionsoul
ltpro
ltpro
master

搜索帮助