linux终端进度条

0. 这个是在oschina上看到的,很实用,可在代码中添加,权当库使用,小巧实用

1. 地址在此

url = https://git.oschina.net/lionsoul/ltpro.git

2. 细看代码,实现原理就是按比例打印对应彩色字符。可用于进度等。此方法也可用于任何彩色终端

看主要应用

3.详细代码

/** * 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 * bar:结构体 * max:bar长度 * title:bar的名字,显示在前面 * style:显示模式 */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);}

真正的爱,应该超越生命的长度心灵的宽度灵魂的深度

linux终端进度条

相关文章:

你感兴趣的文章:

标签云: