当前位置:   article > 正文

Linux 实现打印彩色进度条

Linux 实现打印彩色进度条

预备知识

一、理解回车换行

  • 在我们熟悉的C语言中,换行就可以跳转的下一行开头 ,但其实这一操作有两个步骤,\r (回车)和 \n(换行)
  • 也就是先回到开头,再进行换行

\r 回车就是回到这一行开头
\n 换行就是另起一行

二、认识行缓冲

  • 在内存中预留了一块空间,用来缓冲输入或输出的数据,这个保留的空间被称为缓冲区。
  • 下面我们通过几个代码来理解一下:

1、代码一、二(回车换行理解)

代码一:

#include<stdio.h>

int main()
{
    printf("hello world\n");
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

代码二:

  • 那我将这个\n换成了\r,再次打印会出现什么情况?
#include<stdio.h>

int main()
{
    printf("hello world\r");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

在这里插入图片描述

  • 发现 \n 可以打印出来,而 \r,不能打印出来,因为显示器模式是行刷新缓冲区是按行缓冲的,没有\n,就不能立即刷新。 \r 回到行首后,会进行覆盖写,shell 提示符会覆盖掉之前写的 “hello world”,如果我们在 “hello world” 不加 \r,则不会进行覆盖写,shell 提示符会顺着 “hello world” 往后写

如下:

在这里插入图片描述

2、代码三、四(sleep函数和ffush函数理解)

  • 行缓冲是缓冲区刷新策略的一种,在行缓冲模式下,当输入和输出中遇到 ‘\n’ 换行时,就会刷新缓冲区,下面我们认识头文件<unistd.h>的三个函数

sleep:Linux 下的休眠函数,单位是秒
usleep:和sleep 一样,单位ms(即10-6 m)
fflush :刷新缓冲区

代码 3:

#include<stdio.h>
#include<unistd.h>

int main()
{
    printf("hello world");
    sleep(3);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

  • 我们写的这个C语言程序是从上到下依次执行的,而我们看到的是先休眠后打印
  • 这是因为数据保存在缓冲区中,没有主动刷新。当程序退出后,保存在缓冲区中的数据被自动刷新出来了,如果我们想提前刷新,便可以调用fflush函数来刷新缓冲区

代码四:

#include <stdio.h>
#include <unistd.h>                                                                                                     
int main()
{
    printf("hello world");
    fflush(stdout);
    printf("\n");
    sleep(3);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

  • 这次 “hello world” 被直接打印出来,我们加 \n避免shell 提示符出现在 “hello world” 后面

三、简单倒计时

1. 倒计时代码

#include <stdio.h>
#include <unistd.h>
int main()
{
    int cnt=10;
    while(cnt>=0)
    {
        printf("%-2d\r",cnt);
        fflush(stdout);
        sleep(1);
        cnt--;                                                                                                                                            
    }
    printf("\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2、效果展示

在这里插入图片描述

具备了以上介绍的知识,接下来我们就实现进度条了

四、进度条

1、效果展示

在这里插入图片描述

2、进度条代码

makefile
processbar:ProcessBar.c main.c
        gcc -o $@ $^ 
.PHONY:clean
clean:  
        rm -rf processbar
  • 1
  • 2
  • 3
  • 4
  • 5
ProcessBar.h
#pragma once
#include <string.h>
#include <unistd.h>
#include <stdio.h>

// 进度条箭头
#define TAIL '>'

// 进度条的数组大小
#define Length 102

// 进度条加载的进度条
#define Style '='

// 重定义函数指针
typedef void (*callback_t)(double, double);

// 进度条的实现
void ProcBar(double total, double current);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
ProcessBar.c
#include "ProcessBar.h"

#define LIGHT_CYAN   "\033[1;36m" // 亮青色
#define NONE "\033[m" 	//截断

// 显示进度
const char* lable = "|/-\\";

void ProcBar(double total, double current)
{
    char bar[Length];
    // 初始化进度条
    memset(bar, '\0', sizeof(bar));
    int len = strlen(lable);

    int cnt = 0;
    double rate = (current * 100.0) / total;
    // 循环次数
    int loop_count = (int)rate;
    while (cnt < loop_count)
    {
        bar[cnt++] = Style;
        if (rate < 100)
            bar[loop_count] = TAIL;
    }
    // 打印显示
    printf(LIGHT_CYAN"[%-100s]"NONE"[%.2lf%%][%c]\r", bar, rate, lable[cnt % len]);
    // 刷新缓冲区
    fflush(stdout);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
main.c
#include "ProcessBar.h"

// 网络带宽【1mb】
double bandwidth = 1024 * 1024 * 1.0;

void download(double filesize, callback_t cb)
{
    //  累计下载的数据量
    double current = 0.0;
    printf("download begin, current: %lf\n", current);
    while (current <= filesize)
    {
        // 使用函数指针更新界面
        cb(filesize, current);
        //从网络中获取数据
	//......
	
	// 睡眠
        usleep(100000);

        // 累计下载
        current += bandwidth;
    }
    printf("\ndownload done, filesize: %lf\n", filesize);
}

int main()
{
    // 测试调用
    //download(100 * 1024 * 1024, ProcBar);
    download(2 * 1024 * 1024, ProcBar);
    //download(200*1024*1024,ProcBar);
    //download(400*1024*1024,ProcBar);
    download(50*1024*1024,ProcBar);
    download(10*1024*1024,ProcBar);

    // 测试
    //ProcBar(100.0, 56.9);
    //ProcBar(100.0, 1.0);
    //ProcBar(100.0, 99.9);
    //ProcBar(100.0, 100);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

3、实现过程分析

4.3.1 进度条实现样式

进度条样式 :

  • 进度条的主要内容是两个中括号包裹,中间进度显示以=>的方式进行推进呈现

进度条百分比:

  • 显示当前进度百分比,随着进度不断推进,百分比也在增加

进度条旋转字符:

  • 显示加载样式,可以利用一个旋转的字符,例如 [] 的样式,顺时针不断旋转,依次为 “| / - \”,注意 ** 也是转义字符,因此需要两个 *\ *

进度条颜色:

c语言颜色参考
我们可以根据自己的喜好给进度条上色,在此我们找到颜色参照表

4.3.2 进度条实现方法
  • 预留进度条大小为 100 个 = ,外加 1 个 > ,加上保存 \0 的位置,定义一个102个单位的长度的bar数组。

  • 如果将打印放在循环中的话,在打印的时候会变得卡卡的,我们可以将打印放到循环外面,等数组放上=>后,在一起打印,这样更好

  • 我们又实现了一个函数download(),把ProcBar(),作为参数传递给download(),用usleep函数模拟下载时间,然后循环起来回调processbar()函数,便实现了进度条

  • 最后考虑到第二次下载,bar数组满了,我们再每次调用download()函数时,清空bar数组,完成实现~~

  • 这就实现了我们最终的效果

在这里插入图片描述

最后本文就到这里结束了,感谢大家的收看,请多多指点~

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

闽ICP备14008679号