实现控制台上的进度条
来源:互联网 发布:理财软件哪个收益最高 编辑:程序博客网 时间:2024/04/18 20:45
通常, 控制台程序在执行一个漫长的任务时,需要实时显示当前进度信息, 本文演示了类似GUI进度条控件的实现.
由于需要实时更新进度条信息,并且是要在同一行显示,所以需要用到回车转义字符'\r'.
首先是进度条结构体的定义:
#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <time.h>#include <errno.h>#include <sys/select.h>#include <sys/time.h>#include <sys/types.h>#define bool unsigned char#define true 1#define false 0struct progress_bar_info { const char *name; bool interactive; long init_length; long step; //步长 long total_length; int width; char *buffer;};
接着是几个帮助函数, 主要是获取控制台屏幕大小:
static intget_screen_width(void){ int screen_width = getenv("COLUMNS"); return screen_width;}static intget_screen_height(void){ int screen_height = getenv("LINES"); return screen_height;}
接下来是创建和显示进度条图形的函数:
static voidcreate_image(struct progress_bar_info *pbi, bool done){ char *p = pbi->buffer; pbi->init_length += pbi->step; long current_size = pbi->init_length; int percentage = 0; int i = 0; if (current_size >= pbi->total_length) { percentage = 100; strcpy(p, "100%"); } else { percentage = 100.0 * current_size / pbi->total_length; sprintf (p, "%2d%% ", percentage); } p += 4; int progress_size = 10; //-> *p++ = '['; int current_progress_size = progress_size * percentage/100; for (i = 0; i < progress_size; i++) { if (i < current_progress_size) *p++ = '>'; else *p++ = ' '; } *p++ = ']'; if (done) strcpy(p, " Done!\n"); p = strchr(p, '\0'); //move to the end;}static voiddisplay_image(char *buf){ fprintf(stderr, "\r");//回车, 将光标设置到行的开头 fprintf(stderr, "%s", buf);}
下面是创建进度条函数: 传入参数: 进度条初始长度和总长度
void* progress_bar_create(int initial, int total){ struct progress_bar_info *pbi = (struct progress_bar_info*)malloc(sizeof(struct progress_bar_info)); if (pbi == NULL) return NULL; if (initial > total) total = initial; pbi->init_length = initial; pbi->total_length = total; pbi->step = 2; pbi->width = get_screen_width() - 1; //dont't use the last screen column pbi->buffer = (char*)malloc(pbi->width + 100); create_image(pbi, false); display_image(pbi->buffer); return pbi;}
进度条更新:
void progress_bar_update(void *progress_bar){ struct progress_bar_info *pbi = (struct progress_bar_info*)progress_bar; pbi->init_length += pbi->step; create_image (pbi, false); display_image (pbi->buffer);}
进度条完成:
void progress_bar_finish(void *progress_bar){ struct progress_bar_info *pbi = (struct progress_bar_info*)progress_bar; create_image (pbi, true); display_image (pbi->buffer); free(pbi->buffer);}
下面是进度条测试代码:
int mySleep(unsigned int sleepSecond){ struct timeval t_timeval; t_timeval.tv_sec = sleepSecond; t_timeval.tv_usec = 0; select( 0, NULL, NULL, NULL, &t_timeval ); return 0;}void thread_func(void *data){ struct progress_bar_info *pbi = (struct progress_bar_info*)data; while (pbi->init_length < pbi->total_length) { progress_bar_update(pbi); mySleep(1); } progress_bar_finish(pbi);#if 0 int i = 0; for (i = 0; i < 10; i++) { fprintf(stderr, "%d\r", i); // printf(stderr, "\r"); mySleep(1); } fprintf(stderr, "\n");#endif}int main(void){ struct progress_bar_info *pbi = progress_bar_create(0, 100); pthread_t a_thread; pthread_create(&a_thread, NULL, thread_func, pbi); pthread_join(a_thread, NULL); return 0;}
- 实现控制台上的进度条
- C#: 控制台上输出显示进度条/百分比
- 控制台上的黑白棋
- C++在控制台上实现2048游戏
- Windows控制台上的UTF-8支持
- 控制台上ctrl+z引起的疑惑
- 使用canto+w3m实现在控制台上完美阅读RSS
- c/c++ 入门之控制台上实现贪吃蛇
- C在控制台上实现鼠标画图功能
- ffmpeg控制台上不能输出信息的解决办法
- 设置eclipse控制台上的信息输入到某个文件
- 控制台上的内容不输入到nohup.out
- 一个控制台上永远都是你赢的五子棋游戏,囧
- 在控制台上输入菱形的边长,打印出菱形
- C#控制台基础 按下任意键,显示在控制台上或者不显示在控制台上的console.readkey()
- 进度条的控制
- 控制台上输出倒三角
- 编写了一个服务器端的程序实现在客户端输入字符然后在控制台上显示,直到输入"END"为止,让你写出客户端的程序;
- AOP编程的理解
- 烧绳计时问题
- objective-c之@synthesize和@property含义
- cocos2d与cocos2d x卸载
- 黑马视频绝对给力,各位亲想要下载火速前往,
- 实现控制台上的进度条
- SQL数据库 本地 127.0.01 连接 配置
- C,C++表达式求值顺序
- Android培训班(114)start_kernel函数11
- windows编程学习笔记一之——windows程序基本框架
- HDFS中的Append/Hflush/Read规范文档(HDFS-265:Revisit append / Append&Hflush&Read Specification)
- C++题目(五)
- [Chromium阅读点滴]arraysize宏定义与数组元素个数统计
- 条款2:尽量以const,enum,inline替换#define