mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Simplified progress bar
This commit is contained in:
@@ -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;
|
||||
}
|
||||
explicit ProgressBar(const std::string& name) : _name(name) {}
|
||||
|
||||
void increment(T value) {
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user