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 <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(float value) {
|
||||||
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
void increment(T value) {
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user