Simplified progress bar

This commit is contained in:
Pranav Srinivas Kumar
2019-12-03 11:54:06 -06:00
parent 1e6df03be6
commit 9e954f67fa

View File

@@ -5,31 +5,29 @@
#include <mutex>
#include <thread>
template <typename T>
class progress_bar {
class ProgressBar {
std::string _name{"Running"};
size_t _bar_width{80};
std::string _start{"|"};
std::string _end{"|"};
std::mutex _mutex;
T _progress{T()};
float _progress{0.0};
public:
~progress_bar() {
std::cout << std::endl;
}
void increment(T value) {
std::unique_lock<std::mutex> lock{_mutex};
explicit ProgressBar(const std::string& name) : _name(name) {}
void increment(float value) {
std::unique_lock<std::mutex> lock{_mutex};
_progress = value / 100.0;
std::cout << _name << " [";
T pos = _progress * static_cast<T>(_bar_width);
float pos = _progress * static_cast<float>(_bar_width);
for (size_t i = 0; i < _bar_width; ++i) {
if (i < pos) std::cout << '#';
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << static_cast<int>(value) << " %\r" << std::flush;
std::cout << "] " << static_cast<int>(value) << "%\r";
std::cout.flush();
}
};