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