Files
indicators/include/progress/bar.hpp

149 lines
3.7 KiB
C++
Raw Normal View History

2019-12-03 09:54:50 -06:00
#pragma once
#include <atomic>
2019-12-03 21:25:22 -06:00
#include <iostream>
2019-12-03 09:54:50 -06:00
#include <mutex>
2019-12-03 21:25:22 -06:00
#include <string>
#include <progress/termcolor.hpp>
2019-12-03 09:54:50 -06:00
#include <thread>
2019-12-03 11:54:06 -06:00
class ProgressBar {
2019-12-03 21:25:22 -06:00
public:
enum class Color { GREY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
2019-12-03 21:25:22 -06:00
void color(Color color) {
std::unique_lock<std::mutex> lock{_mutex};
_color = color;
}
2019-12-03 19:59:43 -06:00
void bar_width(size_t bar_width) {
std::unique_lock<std::mutex> lock{_mutex};
_bar_width = bar_width;
}
2019-12-03 21:25:22 -06:00
void start_with(const std::string &start) {
2019-12-03 19:59:43 -06:00
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 16:53:17 -06:00
_start = start;
}
2019-12-03 21:25:22 -06:00
void fill_progress_with(const std::string &fill) {
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 16:53:17 -06:00
_fill = fill;
}
2019-12-03 21:25:22 -06:00
void lead_progress_with(const std::string &lead) {
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 16:53:17 -06:00
_lead = lead;
}
2019-12-03 21:25:22 -06:00
void fill_remainder_with(const std::string &remainder) {
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 16:56:39 -06:00
_remainder = remainder;
2019-12-03 21:25:22 -06:00
}
2019-12-03 16:53:17 -06:00
2019-12-03 21:25:22 -06:00
void end_with(const std::string &end) {
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 16:53:17 -06:00
_end = end;
2019-12-03 20:40:34 -06:00
}
2019-12-03 21:55:38 -06:00
void append_text(const std::string& text) {
std::unique_lock<std::mutex> lock{_mutex};
_text_after = text;
if (_text_after.length() > _max_text_after_length)
_max_text_after_length = _text_after.length();
}
2019-12-03 21:25:22 -06:00
void show_percentage(bool flag) { _show_percentage = flag; }
2019-12-03 11:54:06 -06:00
2019-12-03 19:59:43 -06:00
void set_progress(float value) {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress = value;
2019-12-03 09:54:50 -06:00
}
2019-12-03 19:59:43 -06:00
_print_progress();
2019-12-03 09:54:50 -06:00
}
2019-12-03 19:59:43 -06:00
void tick() {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress += 1;
}
_print_progress();
}
2019-12-03 22:53:38 -06:00
size_t current() {
std::unique_lock<std::mutex> lock{_mutex};
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool completed() const { return _completed; }
2019-12-03 21:25:22 -06:00
private:
float _progress{0.0};
size_t _bar_width{100};
2019-12-03 21:55:38 -06:00
std::string _text_before{""};
2019-12-03 21:25:22 -06:00
std::string _start{"["};
std::string _fill{"="};
std::string _lead{">"};
std::string _remainder{" "};
std::string _end{"]"};
2019-12-03 21:55:38 -06:00
std::string _text_after{""};
std::atomic<size_t> _max_text_after_length{0};
2019-12-03 21:25:22 -06:00
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::mutex _mutex;
Color _color;
void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex};
2019-12-03 22:53:38 -06:00
std::cout << termcolor::bold;
2019-12-03 21:25:22 -06:00
switch(_color) {
case Color::GREY:
std::cout << termcolor::grey;
break;
case Color::RED:
std::cout << termcolor::red;
break;
case Color::GREEN:
std::cout << termcolor::green;
break;
case Color::YELLOW:
std::cout << termcolor::yellow;
break;
case Color::BLUE:
std::cout << termcolor::blue;
break;
case Color::MAGENTA:
std::cout << termcolor::magenta;
break;
case Color::CYAN:
std::cout << termcolor::cyan;
break;
case Color::WHITE:
std::cout << termcolor::white;
break;
}
std::cout << _start;
float pos = _progress * static_cast<float>(_bar_width) / 100.0;
for (size_t i = 0; i < _bar_width; ++i) {
if (i < pos)
std::cout << _fill;
else if (i == pos)
std::cout << _lead;
else
std::cout << _remainder;
}
std::cout << _end;
2019-12-03 22:53:38 -06:00
if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
2019-12-03 23:07:44 -06:00
if (_max_text_after_length == 0) _max_text_after_length = 20;
2019-12-03 21:55:38 -06:00
std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r";
2019-12-03 21:25:22 -06:00
std::cout.flush();
2019-12-03 23:07:44 -06:00
if (_progress > 100.0) {
_completed = true;
}
2019-12-03 21:25:22 -06:00
if (_completed)
std::cout << termcolor::reset << std::endl;
}
2019-12-03 09:54:50 -06:00
};