linux下进度条的实现

目录

一、代码一版

1.processbar.h

2.processbar.c

3.main.c

二、代码二版

1.processbar.h

2.processbar.c

3.main.c

三、改变文字颜色


一、代码一版

使用模块化编程

1.processbar.h

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

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

2.processbar.c

复制代码
#include"processbar.h"
#include<string.h>
#include<unistd.h>
char* load ="|/-\\"; 
void processbar()
{
  char bar[capacity];
  memset(bar,'\0',sizeof(bar));
  int size = strlen(load);

  int cent = 0;
  while(cent <=99)
  {
   bar[cent++] = style;
   if(cent <= 99 )
   bar[cent] = '>';
   printf("[%-100s][%d%%][%c]\r",bar,cent,load[cent%size]);
   fflush(stdout);//没有\n就没法立即刷新,因为显示器模式是行刷新,因此手动刷新
   usleep(100000);//休眠xx微秒
  }
  printf("\n");
}

3.main.c

复制代码
int main()
{
  processbar();
  return 0;
}

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

复制代码
char* load ="|/-\\";

进行实现

二、代码二版

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

1.processbar.h

复制代码
#include<stdio.h>
#define capacity 101
#define style '='
extern void processbar();

2.processbar.c

复制代码
#include"processbar.h"
#include<string.h>
#include<unistd.h>
char* load ="|/-\\"; 
char bar[capacity];
void processbar(int cent)
{
  if(cent > 100 || cent < 0)return;
  int size = strlen(load);

   bar[cent++] = style;
   if(cent <= 99 )
   bar[cent] = '>';
   printf("[%-100s][%d%%][%c]\r",bar,cent,load[cent%size]);
   fflush(stdout);
   if(cent > 99)memset(bar,'\0',sizeof(bar));

}

3.main.c

复制代码
#include"processbar.h"
typedef void(*callback_t)(int);//回调函数

void Download(callback_t cb)
{
  int total = 100;//100MB
  int curr = 0;
  while(curr < total)
  {
    //假设有一个下载
    usleep(50000);//模拟下载的时间
    int rate = curr * 100 / total;//更新进度

    cb(rate);//通过回调函数显示进度
    
    curr += 1;
  }
    printf("\n");
}
int main()
{
  Download(processbar);
  printf("\n");
  Download(processbar);
  return 0;
}

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

printf("helloworld")

printf("hello""world")

三、改变文字颜色

复制代码
//颜色宏定义
#define NONE         "\033[m"
#define RED          "\033[0;32;31m"
#define LIGHT_RED    "\033[1;31m"
#define GREEN        "\033[0;32;32m"
#define LIGHT_GREEN  "\033[1;32m"
#define BLUE         "\033[0;32;34m"
#define LIGHT_BLUE   "\033[1;34m"
#define DARY_GRAY    "\033[1;30m"
#define CYAN         "\033[0;36m"
#define LIGHT_CYAN   "\033[1;36m"
#define PURPLE       "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN        "\033[0;33m"
#define YELLOW       "\033[1;33m"
#define LIGHT_GRAY   "\033[0;37m"
#define WHITE        "\033[1;37m"

使用方法

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

复制代码
printf(YELLOW"this print msg is yellow!\n"NONE);

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

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

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

相关推荐
IT摆渡者6 分钟前
11Panel 部署雷池WAF
linux
学习路上_write6 分钟前
Linux黑马命令学习
linux·运维·服务器
那年窗外下的雪.11 分钟前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计
服务器·网络·学习·算法·哈希算法
yunwei3726 分钟前
eBPF 教程:追踪 CUDA GPU 操作
linux·后端·性能优化
Android系统攻城狮30 分钟前
Linux Gstreamer深度解析之gst_audio_channel_reorder_map调用流程与实战(十八)
linux·运维·服务器·音视频进阶·gstreamer音视频进阶
新时代牛马1 小时前
docker run 起不来?从dockerd、containerd 到runc 一条线讲透
运维·docker·容器
TomEval1 小时前
【App 自动化】14 - App 自动化基础与环境
运维·自动化
cc_yy_zh1 小时前
学习使用kali抓包
服务器·学习·php
盛世宏博智慧档案1 小时前
户外配电柜为什么要装POE供电以太网温湿度传感器?
服务器·网络·人工智能·监控·温湿度·配电柜
新时代牛马1 小时前
Linux 防火墙:nftables 与iptables 兼容层
linux·运维·服务器