Files
indicators/include/progress/bar.hpp

34 lines
815 B
C++
Raw Normal View History

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 10:02:46 -06:00
std::string _name{"Running"};
2019-12-03 10:46:25 -06:00
size_t _bar_width{80};
2019-12-03 09:54:50 -06:00
std::string _start{"|"};
std::string _end{"|"};
std::mutex _mutex;
2019-12-03 11:54:06 -06:00
float _progress{0.0};
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 10:02:46 -06:00
std::cout << _name << " [";
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) {
if (i < pos) std::cout << '#';
else if (i == pos) std::cout << ">";
2019-12-03 09:54:50 -06:00
else std::cout << " ";
}
2019-12-03 11:54:06 -06:00
std::cout << "] " << static_cast<int>(value) << "%\r";
std::cout.flush();
2019-12-03 09:54:50 -06:00
}
};