当前位置:   article > 正文

linux下进度条的实现

linux下进度条的实现

目录

一、代码一版

1.processbar.h

2.processbar.c

3.main.c

二、代码二版 

1.processbar.h

2.processbar.c

3.main.c

三、改变文字颜色


一、代码一版

使用模块化编程

1.processbar.h

  1. #include<stdio.h>
  2. #define capacity 101 //常量使用宏定义
  3. #define style '=' //符号方便后续修改
  4. extern void processbar();

 修饰变量的时候一定要带extern,修饰函数的时候可以省略,因为没有函数体就表明它一定是声明

2.processbar.c

  1. #include"processbar.h"
  2. #include<string.h>
  3. #include<unistd.h>
  4. char* load ="|/-\\";
  5. void processbar()
  6. {
  7. char bar[capacity];
  8. memset(bar,'\0',sizeof(bar));
  9. int size = strlen(load);
  10. int cent = 0;
  11. while(cent <=99)
  12. {
  13. bar[cent++] = style;
  14. if(cent <= 99 )
  15. bar[cent] = '>';
  16. printf("[%-100s][%d%%][%c]\r",bar,cent,load[cent%size]);
  17. fflush(stdout);//没有\n就没法立即刷新,因为显示器模式是行刷新,因此手动刷新
  18. usleep(100000);//休眠xx微秒
  19. }
  20. printf("\n");
  21. }

3.main.c

  1. int main()
  2. {
  3. processbar();
  4. return 0;
  5. }

效果图,当进度未达到100时,在进度条最右端会有>,同时会有加载效果 由数组

char* load ="|/-\\";

进行实现 

二、代码二版 

processbar函数只负责打印,能更好地模拟下载过程

1.processbar.h

  1. #include<stdio.h>
  2. #define capacity 101
  3. #define style '='
  4. extern void processbar();

2.processbar.c

  1. #include"processbar.h"
  2. #include<string.h>
  3. #include<unistd.h>
  4. char* load ="|/-\\";
  5. char bar[capacity];
  6. void processbar(int cent)
  7. {
  8. if(cent > 100 || cent < 0)return;
  9. int size = strlen(load);
  10. bar[cent++] = style;
  11. if(cent <= 99 )
  12. bar[cent] = '>';
  13. printf("[%-100s][%d%%][%c]\r",bar,cent,load[cent%size]);
  14. fflush(stdout);
  15. if(cent > 99)memset(bar,'\0',sizeof(bar));
  16. }

3.main.c

  1. #include"processbar.h"
  2. typedef void(*callback_t)(int);//回调函数
  3. void Download(callback_t cb)
  4. {
  5. int total = 100;//100MB
  6. int curr = 0;
  7. while(curr < total)
  8. {
  9. //假设有一个下载
  10. usleep(50000);//模拟下载的时间
  11. int rate = curr * 100 / total;//更新进度
  12. cb(rate);//通过回调函数显示进度
  13. curr += 1;
  14. }
  15. printf("\n");
  16. }
  17. int main()
  18. {
  19. Download(processbar);
  20. printf("\n");
  21. Download(processbar);
  22. return 0;
  23. }

临近字符串具有自动链接功能例如

printf("helloworld")

printf("hello""world")

三、改变文字颜色

  1. //颜色宏定义
  2. #define NONE "\033[m"
  3. #define RED "\033[0;32;31m"
  4. #define LIGHT_RED "\033[1;31m"
  5. #define GREEN "\033[0;32;32m"
  6. #define LIGHT_GREEN "\033[1;32m"
  7. #define BLUE "\033[0;32;34m"
  8. #define LIGHT_BLUE "\033[1;34m"
  9. #define DARY_GRAY "\033[1;30m"
  10. #define CYAN "\033[0;36m"
  11. #define LIGHT_CYAN "\033[1;36m"
  12. #define PURPLE "\033[0;35m"
  13. #define LIGHT_PURPLE "\033[1;35m"
  14. #define BROWN "\033[0;33m"
  15. #define YELLOW "\033[1;33m"
  16. #define LIGHT_GRAY "\033[0;37m"
  17. #define WHITE "\033[1;37m"

使用方法

1.如果你想要让某一段文字变成某个颜色

printf(YELLOW"this print msg is yellow!\n"NONE);

这两个指令相当于 开关,可以放置在任意位置。

如果开启开关,后面的所有字的颜色都会变成这个颜色

开启后关掉的话只会让某一段文字变色 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/758131
推荐阅读
相关标签
  

闽ICP备14008679号