Merge pull request #21 from dawidpilarski/dev

API change - progress bar can be constructed with proper settings.
This commit is contained in:
Pranav
2020-02-11 13:38:14 +05:30
committed by GitHub
14 changed files with 841 additions and 553 deletions

View File

@@ -37,56 +37,74 @@ SOFTWARE.
#include <iostream>
#include <mutex>
#include <string>
#include <tuple>
#include <thread>
#include "setting.hpp"
namespace indicators {
class BlockProgressBar {
using Settings = std::tuple<
option::ForegroundColor,
option::BarWidth,
option::Start,
option::End,
option::PrefixText,
option::PostfixText,
option::ShowPercentage,
option::ShowElapsedTime,
option::ShowRemainingTime,
option::Completed,
option::SavedStartTime,
option::MaxPostfixTextLen>;
public:
void set_foreground_color(Color color) {
std::lock_guard<std::mutex> lock{_mutex};
_foreground_color = color;
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
explicit BlockProgressBar(Args&&... args) : settings_(
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::start>(option::Start{"["}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::end>(option::End{"]"}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{""}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{""}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...)
) {}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id>&& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = std::move(setting).value;
}
void set_bar_width(size_t bar_width) {
std::lock_guard<std::mutex> lock{_mutex};
_bar_width = bar_width;
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id>& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = setting.value;
}
void start_bar_with(const std::string &start) {
std::lock_guard<std::mutex> lock{_mutex};
_start = start;
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
}
void end_bar_with(const std::string &end) {
std::lock_guard<std::mutex> lock{_mutex};
_end = end;
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_prefix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_prefix_text = text;
}
void set_postfix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
}
void show_percentage() { _show_percentage = true; }
void hide_percentage() { _show_percentage = false; }
void show_elapsed_time() { _show_elapsed_time = true; }
void hide_elapsed_time() { _show_elapsed_time = false; }
void show_remaining_time() { _show_remaining_time = true; }
void hide_remaining_time() { _show_remaining_time = false; }
void set_progress(float value) {
{
std::lock_guard<std::mutex> lock{_mutex};
@@ -110,44 +128,47 @@ public:
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
_print_progress();
}
private:
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
Settings settings_;
float _progress{0.0};
size_t _bar_width{100};
std::string _prefix_text{""};
std::string _start{"["};
std::string _end{"]"};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::mutex _mutex;
Color _foreground_color{indicators::Color::WHITE};
template <typename Indicator, size_t count> friend class MultiProgress;
std::atomic<bool> _multi_progress_mode{false};
void _save_start_time() {
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
saved_start_time = true;
}
}
void _print_progress(bool from_multi_progress = false) {
if (_multi_progress_mode && !from_multi_progress) {
if (_progress > 100.0) {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
}
return;
}
@@ -156,25 +177,25 @@ private:
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
std::cout << termcolor::bold;
details::set_stream_color(std::cout, _foreground_color);
std::cout << _prefix_text;
std::cout << _start;
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
std::cout << get_value<details::ProgressBarOption::start>();
details::BlockProgressScaleWriter writer{std::cout, _bar_width};
details::BlockProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>()};
writer.write(_progress);
std::cout << _end;
if (_show_percentage) {
std::cout << get_value<details::ProgressBarOption::end>();
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
if (_show_elapsed_time) {
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, elapsed);
}
if (_show_remaining_time) {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << " [";
@@ -184,18 +205,18 @@ private:
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
std::cout.flush();
if (_progress > 100.0) {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
}
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
if (get_value<details::ProgressBarOption::completed>() && !from_multi_progress) // Don't std::endl if calling from MultiProgress
std::cout << termcolor::reset << std::endl;
}
};

View File

@@ -34,80 +34,95 @@ SOFTWARE.
#include <chrono>
#include <cmath>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <tuple>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
namespace indicators {
class ProgressBar {
using Settings = std::tuple<
option::BarWidth,
option::PrefixText,
option::PostfixText,
option::Start,
option::End,
option::Fill,
option::Lead,
option::Remainder,
option::MaxPostfixTextLen,
option::Completed,
option::ShowPercentage,
option::ShowElapsedTime,
option::ShowRemainingTime,
option::SavedStartTime,
option::ForegroundColor
>;
public:
void set_foreground_color(Color color) {
std::lock_guard<std::mutex> lock{_mutex};
_foreground_color = color;
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
explicit ProgressBar(Args&&... args) :
settings_(
details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::start>(option::Start{"["}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::end>(option::End{"]"}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::fill>(option::Fill{"="}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::lead>(option::Lead{">"}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::remainder>(option::Remainder{" "}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{false} ,std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...)
)
{}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id>&& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = std::move(setting).value;
}
void set_bar_width(size_t bar_width) {
std::lock_guard<std::mutex> lock{_mutex};
_bar_width = bar_width;
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id>& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = setting.value;
}
void start_bar_with(const std::string &start) {
std::lock_guard<std::mutex> lock{_mutex};
_start = start;
}
void fill_bar_progress_with(const std::string &fill) {
std::lock_guard<std::mutex> lock{_mutex};
_fill = fill;
}
void lead_bar_progress_with(const std::string &lead) {
std::lock_guard<std::mutex> lock{_mutex};
_lead = lead;
}
void fill_bar_remainder_with(const std::string &remainder) {
std::lock_guard<std::mutex> lock{_mutex};
_remainder = remainder;
}
void end_bar_with(const std::string &end) {
std::lock_guard<std::mutex> lock{_mutex};
_end = end;
}
void set_prefix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_prefix_text = text;
}
void set_postfix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
}
void show_percentage() { _show_percentage = true; }
void hide_percentage() { _show_percentage = false; }
void show_elapsed_time() { _show_elapsed_time = true; }
void hide_elapsed_time() { _show_elapsed_time = false; }
void show_remaining_time() { _show_remaining_time = true; }
void hide_remaining_time() { _show_remaining_time = false; }
void set_progress(float value) {
{
std::lock_guard<std::mutex> lock{_mutex};
_progress = value;
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
}
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_progress(float newProgress){
{
std::lock_guard<std::mutex> lck(_mutex);
_progress = newProgress;
}
_save_start_time();
_print_progress();
}
@@ -126,78 +141,81 @@ public:
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
_print_progress();
}
private:
float _progress{0.0};
size_t _bar_width{100};
std::string _prefix_text{""};
std::string _start{"["};
std::string _fill{"="};
std::string _lead{">"};
std::string _remainder{" "};
std::string _end{"]"};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
float _progress{0};
Settings settings_;
std::chrono::nanoseconds _elapsed;
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::mutex _mutex;
Color _foreground_color{indicators::Color::WHITE};
template <typename Indicator, size_t count> friend class MultiProgress;
std::atomic<bool> _multi_progress_mode{false};
void _save_start_time() {
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
saved_start_time = true;
}
}
void _print_progress(bool from_multi_progress = false) {
if (_multi_progress_mode && !from_multi_progress) {
if (_progress > 100.0) {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
}
return;
}
std::lock_guard<std::mutex> lock{_mutex};
auto now = std::chrono::high_resolution_clock::now();
if (!_completed)
if (!get_value<details::ProgressBarOption::completed>())
_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
std::cout << termcolor::bold;
details::set_stream_color(std::cout, _foreground_color);
std::cout << _prefix_text;
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
std::cout << _start;
std::cout << get_value<details::ProgressBarOption::start>();
details::ProgressScaleWriter writer{std::cout, _bar_width, _fill, _lead, _remainder};
details::ProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>(),
get_value<details::ProgressBarOption::fill>(),
get_value<details::ProgressBarOption::lead>(),
get_value<details::ProgressBarOption::remainder>()};
writer.write(_progress);
std::cout << _end;
std::cout << get_value<details::ProgressBarOption::end>();
if (_show_percentage) {
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
if (_show_elapsed_time) {
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, _elapsed);
}
if (_show_remaining_time) {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << " [";
@@ -207,18 +225,18 @@ private:
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
std::cout.flush();
if (_progress > 100.0) {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
}
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
if (get_value<details::ProgressBarOption::completed>() && !from_multi_progress) // Don't std::endl if calling from MultiProgress
std::cout << termcolor::reset << std::endl;
}
};

View File

@@ -34,49 +34,77 @@ SOFTWARE.
#include <chrono>
#include <cmath>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <tuple>
#include <thread>
#include <vector>
namespace indicators {
class ProgressSpinner {
using Settings = std::tuple<
option::ForegroundColor,
option::PrefixText,
option::PostfixText,
option::ShowPercentage,
option::ShowElapsedTime,
option::ShowRemainingTime,
option::ShowSpinner,
option::SavedStartTime,
option::Completed,
option::MaxPostfixTextLen,
option::SpinnerStates
>;
public:
void set_foreground_color(Color color) {
std::lock_guard<std::mutex> lock{_mutex};
_foreground_color = color;
template <typename... Args, typename std::enable_if<details::are_settings_from_tuple<Settings, typename std::decay<Args>::type...>::value, void*>::type = nullptr>
explicit ProgressSpinner(Args&&... args) : settings_(
details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::spinner_show>(option::ShowSpinner{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::spinner_states>(option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "", "", "", "", ""}}, std::forward<Args>(args)...)
){}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id>&& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = std::move(setting).value;
}
void set_prefix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_prefix_text = text;
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id>& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(std::declval<Settings>()))>::type>::value, "Setting has wrong type!");
std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = setting.value;
}
void set_postfix_text(const std::string &text) {
std::lock_guard<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
}
void show_percentage() { _show_percentage = true; }
void hide_percentage() { _show_percentage = false; }
void show_elapsed_time() { _show_elapsed_time = true; }
void hide_elapsed_time() { _show_elapsed_time = false; }
void show_remaining_time() { _show_remaining_time = true; }
void hide_remaining_time() { _show_remaining_time = false; }
void show_spinner() { _show_spinner = true; }
void hide_spinner() { _show_spinner = false; }
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
std::lock_guard<std::mutex> lock(_mutex);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_progress(float value) {
{
@@ -101,39 +129,37 @@ public:
return std::min(static_cast<size_t>(_progress), size_t(100));
}
bool is_completed() const { return _completed; }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
_print_progress();
}
void set_spinner_states(const std::vector<std::string> &states) {
std::lock_guard<std::mutex> lock{_mutex};
_states = states;
private:
Settings settings_;
float _progress{0.0};
size_t _index{0};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::mutex _mutex;
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
private:
float _progress{0.0};
std::string _prefix_text{""};
size_t _index{0};
std::vector<std::string> _states{"", "", "", "", "", "", "", "", "", ""};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_elapsed_time{false};
std::atomic<bool> _show_remaining_time{false};
std::atomic<bool> _saved_start_time{false};
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
std::atomic<bool> _show_spinner{true};
std::mutex _mutex;
Color _foreground_color;
template <details::ProgressBarOption id>
auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
return details::get_value<id>(settings_).value;
}
void _save_start_time() {
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now();
_saved_start_time = true;
saved_start_time = true;
}
}
@@ -143,21 +169,21 @@ private:
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
std::cout << termcolor::bold;
details::set_stream_color(std::cout, _foreground_color);
std::cout << _prefix_text;
if (_show_spinner)
std::cout << _states[_index % _states.size()];
if (_show_percentage) {
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
if (get_value<details::ProgressBarOption::spinner_show>())
std::cout << get_value<details::ProgressBarOption::spinner_states>()[_index % get_value<details::ProgressBarOption::spinner_states>().size()];
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
if (_show_elapsed_time) {
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, elapsed);
}
if (_show_remaining_time) {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << " [";
@@ -167,19 +193,19 @@ private:
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (_show_elapsed_time)
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
std::cout.flush();
_index += 1;
if (_progress > 100.0) {
_completed = true;
get_value<details::ProgressBarOption::completed>() = true;
}
if (_completed)
if (get_value<details::ProgressBarOption::completed>())
std::cout << termcolor::reset << std::endl;
}
};

View File

@@ -0,0 +1,222 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Dawid Pilarski <dawid.pilarski@panicsoftware.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstddef>
#include <indicators/color.hpp>
#include <string>
#include <type_traits>
#include <utility>
namespace indicators{
namespace details{
template <bool condition>
struct if_else;
template<>
struct if_else<true>{
using type = std::true_type;
};
template<>
struct if_else<false>{
using type = std::false_type ;
};
template <bool condition, typename True, typename False>
struct if_else_type;
template <typename True, typename False>
struct if_else_type<true, True, False>{
using type = True;
};
template <typename True, typename False>
struct if_else_type<false, True, False>{
using type = False;
};
template <typename... Ops>
struct conjuction;
template <>
struct conjuction<> : std::true_type {};
template <typename Op, typename... TailOps>
struct conjuction<Op, TailOps...> : if_else_type<!Op::value, std::false_type, conjuction<TailOps...>>::type {};
template <typename... Ops>
struct disjunction;
template <>
struct disjunction<> : std::false_type {};
template <typename Op, typename... TailOps>
struct disjunction<Op, TailOps...> : if_else_type<Op::value, std::true_type, disjunction<TailOps...>>::type {};
enum class ProgressBarOption{
bar_width=0,
prefix_text,
postfix_text,
start,
end,
fill,
lead,
remainder,
max_postfix_text_len,
completed,
show_percentage,
show_elapsed_time,
show_remaining_time,
saved_start_time,
foreground_color,
spinner_show,
spinner_states
};
template <typename T, ProgressBarOption Id>
struct Setting{
template <typename... Args, typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type>
explicit Setting(Args&&... args) : value(std::forward<Args>(args)...){}
Setting(const Setting&) = default;
Setting(Setting&&) = default;
static constexpr auto id = Id;
using type = T;
T value{};
};
template <typename T>
struct is_setting : std::false_type{};
template <ProgressBarOption Id, typename T>
struct is_setting<Setting<T, Id>> : std::true_type{};
template <typename... Args>
struct are_settings : if_else<conjuction<is_setting<Args>...>::value>::type {};
template <>
struct are_settings<> : std::true_type{};
template<typename Setting, typename Tuple>
struct is_setting_from_tuple;
template<typename Setting>
struct is_setting_from_tuple<Setting, std::tuple<>> : std::true_type {};
template <typename Setting, typename... TupleTypes>
struct is_setting_from_tuple<Setting, std::tuple<TupleTypes...>> :
if_else<disjunction<std::is_same<Setting, TupleTypes>...>::value>::type {};
template <typename Tuple, typename... Settings>
struct are_settings_from_tuple : if_else<conjuction<is_setting_from_tuple<Settings, Tuple>...>::value>::type {};
template <ProgressBarOption Id>
struct always_true{
static constexpr auto value = true;
};
template<ProgressBarOption Id, typename Default>
Default&& get_impl(Default&& def){
return std::forward<Default>(def);
}
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
(std::decay<T>::type::id == Id),
decltype(std::forward<T>(first))>
::type{
return std::forward<T>(first);
}
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
(std::decay<T>::type::id != Id),
decltype(get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...))>::type{
return get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...);
}
template <ProgressBarOption Id, typename Default, typename... Args, typename = typename std::enable_if<are_settings<Args...>::value, void>::type>
auto get(Default&& def, Args&&... args) -> decltype(details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...)){
return details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...);
}
template <ProgressBarOption Id>
using StringSetting = Setting<std::string, Id>;
template <ProgressBarOption Id>
using IntegerSetting = Setting<std::size_t, Id>;
template <ProgressBarOption Id>
using BooleanSetting = Setting<bool, Id>;
template <ProgressBarOption Id, typename Tuple, std::size_t counter =0>
struct option_idx;
template <ProgressBarOption Id, typename T, typename... Settings, std::size_t counter>
struct option_idx<Id, std::tuple<T, Settings...>, counter> : if_else_type<(Id == T::id),
std::integral_constant<std::size_t, counter>,
option_idx<Id, std::tuple<Settings...>, counter+1>>::type{};
template <ProgressBarOption Id, std::size_t counter>
struct option_idx<Id, std::tuple<>, counter>{
static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found");
};
template <ProgressBarOption Id, typename Settings>
auto get_value(Settings&& settings) -> decltype((std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::declval<Settings&&>()))){
return std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::forward<Settings>(settings));
}
}
namespace option{
using BarWidth = details::IntegerSetting<details::ProgressBarOption::bar_width>;
using PrefixText = details::StringSetting<details::ProgressBarOption::prefix_text>;
using PostfixText = details::StringSetting<details::ProgressBarOption::postfix_text>;
using Start = details::StringSetting<details::ProgressBarOption::start>;
using End = details::StringSetting<details::ProgressBarOption::end>;
using Fill = details::StringSetting<details::ProgressBarOption::fill>;
using Lead = details::StringSetting<details::ProgressBarOption::lead>;
using Remainder = details::StringSetting<details::ProgressBarOption::remainder>;
using MaxPostfixTextLen = details::IntegerSetting<details::ProgressBarOption::max_postfix_text_len>;
using Completed = details::BooleanSetting<details::ProgressBarOption::completed>;
using ShowPercentage = details::BooleanSetting<details::ProgressBarOption::show_percentage>;
using ShowElapsedTime = details::BooleanSetting<details::ProgressBarOption::show_elapsed_time>;
using ShowRemainingTime = details::BooleanSetting<details::ProgressBarOption::show_remaining_time>;
using SavedStartTime = details::BooleanSetting<details::ProgressBarOption::saved_start_time>;
using ForegroundColor = details::Setting<Color, details::ProgressBarOption::foreground_color>;
using ShowSpinner = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
using SpinnerStates = details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
}
}