这篇文章主要讲解了C++如何实现一个命令行进度条,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

缘起

最近做遥感影像融合的GWPCA方法,在带宽比较大的时候速度太慢了,需要有个进度条指示一下,然后我去找进度条的库,发现github上面的C/C++的相应的库似乎没有能在VS下跑的,自己花了点时间写了一个。

效果

实现

大概需要考虑这样几个要素

已完成的百分比执行速度已执行的时间剩余时间

另外进度条的引入不能破坏已有的执行结构,最好和Python的tqdm库类似,通过 start , update 等函数来完成整个进度条,因此对于C语言来说,需要一个定时器,定期将进度条进行重绘(不可能更新一次就重绘一次),因此整个进度条就包含了两个类,一个是进度条类,一个是定时器类。另外需要考虑线程安全的问题。

// Progress.hpp#pragma once#include <ctime>#include <chrono>#include <iostream>#include <iomanip>#include "Timer.hpp"using namespace std::chrono;class ProgressBar{protected: // 进度条的长度(不包含前后缀)unsigned int ncols; // 已完成的数量std::atomic<unsigned int> finishedNum; // 上次的已完成数量unsigned int lastNum; // 总数unsigned int totalNum; // 进度条长度与百分比之间的系数double colsRatio; // 开始时间steady_clock::time_point beginTime; // 上次重绘的时间steady_clock::time_point lastTime; // 重绘周期milliseconds interval;Timer timer;public:ProgressBar(unsigned int totalNum, milliseconds interval) : totalNum(totalNum), interval(interval), finishedNum(0), lastNum(0), ncols(80), colsRatio(0.8) {} // 开始void start(); // 完成void finish(); // 更新void update() { return this->update(1); } // 一次更新多个数量void update(unsigned int num) { this->finishedNum += num; } // 获取进度条长度unsigned int getCols() { return this->ncols; } // 设置进度条长度void setCols(unsigned int ncols) { this->ncols = ncols; this->colsRatio = ncols / 100; } // 重绘void show();};void ProgressBar::start() { // 记录开始时间,并初始化定时器this->beginTime = steady_clock::now();this->lastTime = this->beginTime;// 定时器用于定时调用重绘函数this->timer.start(this->interval.count(), std::bind(&ProgressBar::show, this));}// 重绘函数void ProgressBar::show() { // 清除上次的绘制内容std::cout << "\r"; // 记录重绘的时间点steady_clock::time_point now = steady_clock::now();// 获取已完成的数量unsigned int tmpFinished = this->finishedNum.load();// 获取与开始时间和上次重绘时间的时间间隔auto timeFromStart = now - this->beginTime;auto timeFromLast = now - this->lastTime;// 这次完成的数量unsigned int gap = tmpFinished - this->lastNum;// 计算速度double rate = gap / duration<double>(timeFromLast).count();// 应显示的百分数double present = (100.0 * tmpFinished) / this->totalNum;// 打印百分数std::cout << std::setprecision(1) << std::fixed << present << "%|";// 计算应该绘制多少=符号int barWidth = present * this->colsRatio;// 打印已完成和未完成进度条std::cout << std::setw(barWidth) << std::setfill('=') << "=";std::cout << std::setw(this->ncols - barWidth) << std::setfill(' ') << "|";// 打印速度std::cout << std::setprecision(1) << std::fixed << rate << "op/s|";// 之后的两部分内容分别为打印已过的时间和剩余时间int timeFromStartCount = duration<double>(timeFromStart).count();std::time_t tfs = timeFromStartCount;tm tmfs;gmtime_s(&tmfs, &tfs);std::cout << std::put_time(&tmfs, "%X") << "|";int timeLast;if (rate != 0) { // 剩余时间的估计是用这次的速度和未完成的数量进行估计timeLast = (this->totalNum - tmpFinished) / rate;}else {timeLast = INT_MAX;}if ((this->totalNum - tmpFinished) == 0) {timeLast = 0;}std::time_t tl = timeLast;tm tml;gmtime_s(&tml, &tl);std::cout << std::put_time(&tml, "%X");this->lastNum = tmpFinished;this->lastTime = now;}void ProgressBar::finish() { // 停止定时器this->timer.stop();std::cout << std::endl;}

// Timer.hpp#pragma once#include <functional>#include <chrono>#include <thread>#include <atomic>#include <memory>#include <mutex>#include <condition_variable>using namespace std::chrono;class Timer{public:Timer() : _expired(true), _try_to_expire(false){}Timer(const Timer& timer){_expired = timer._expired.load();_try_to_expire = timer._try_to_expire.load();}~Timer(){stop();}void start(int interval, std::function<void()> task){// is started, do not start againif (_expired == false)return;// start async timer, launch thread and wait in that thread_expired = false;std::thread([this, interval, task]() {while (!_try_to_expire){// sleep every interval and do the task again and again until times upstd::this_thread::sleep_for(std::chrono::milliseconds(interval));task();}{// timer be stopped, update the condition variable expired and wake main threadstd::lock_guard<std::mutex> locker(_mutex);_expired = true;_expired_cond.notify_one();}}).detach();}void startOnce(int delay, std::function<void()> task){std::thread([delay, task]() {std::this_thread::sleep_for(std::chrono::milliseconds(delay));task();}).detach();}void stop(){// do not stop againif (_expired)return;if (_try_to_expire)return;// wait until timer _try_to_expire = true; // change this bool value to make timer while loop stop{std::unique_lock<std::mutex> locker(_mutex);_expired_cond.wait(locker, [this] {return _expired == true; });// reset the timerif (_expired == true)_try_to_expire = false;}}private:std::atomic<bool> _expired; // timer stopped statusstd::atomic<bool> _try_to_expire; // timer is in stop processstd::mutex _mutex;std::condition_variable _expired_cond;};

看完上述内容,是不是对C++如何实现一个命令行进度条有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。