2019-12-03 09:54:50 -06:00
|
|
|
#pragma once
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2019-12-03 11:54:06 -06:00
|
|
|
class ProgressBar {
|
2019-12-03 13:50:00 -06:00
|
|
|
std::string _name{""};
|
|
|
|
|
float _progress{0.0};
|
2019-12-03 10:46:25 -06:00
|
|
|
size_t _bar_width{80};
|
2019-12-03 13:50:00 -06:00
|
|
|
std::string _start{"["};
|
|
|
|
|
std::string _step{"■"};
|
|
|
|
|
std::string _head{"■"};
|
|
|
|
|
std::string _end{"]"};
|
2019-12-03 09:54:50 -06:00
|
|
|
std::mutex _mutex;
|
2019-12-03 13:50:00 -06:00
|
|
|
|
|
|
|
|
void hide_cursor() {
|
|
|
|
|
std::cout << "\e[?25l";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void show_cursor() {
|
|
|
|
|
std::cout << "\e[?25h";
|
|
|
|
|
}
|
2019-12-03 09:54:50 -06:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2019-12-03 11:54:06 -06:00
|
|
|
explicit ProgressBar(const std::string& name) : _name(name) {}
|
|
|
|
|
|
|
|
|
|
void increment(float value) {
|
|
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
2019-12-03 10:46:25 -06:00
|
|
|
_progress = value / 100.0;
|
2019-12-03 13:50:00 -06:00
|
|
|
hide_cursor();
|
|
|
|
|
std::cout << _start;
|
2019-12-03 11:54:06 -06:00
|
|
|
float pos = _progress * static_cast<float>(_bar_width);
|
2019-12-03 10:46:25 -06:00
|
|
|
for (size_t i = 0; i < _bar_width; ++i) {
|
2019-12-03 13:50:00 -06:00
|
|
|
if (i < pos) std::cout << _step;
|
|
|
|
|
else if (i == pos) std::cout << _head;
|
|
|
|
|
else std::cout << "-";
|
2019-12-03 09:54:50 -06:00
|
|
|
}
|
2019-12-03 13:50:00 -06:00
|
|
|
std::cout << _end << " " << static_cast<int>(value) << "%";
|
|
|
|
|
std::cout << " " << _name << "\r";
|
2019-12-03 11:54:06 -06:00
|
|
|
std::cout.flush();
|
2019-12-03 13:50:00 -06:00
|
|
|
show_cursor();
|
2019-12-03 09:54:50 -06:00
|
|
|
}
|
|
|
|
|
};
|