Progress spinner changed, default values approach changed, templates refactored

This commit is contained in:
Dawid Pilarski
2020-02-04 23:29:14 +01:00
committed by pilarski
parent 46bccdcc1c
commit 46c9983fab
5 changed files with 344 additions and 403 deletions

View File

@@ -188,19 +188,21 @@ int main() {
// //
// PROGRESS BAR 5 // PROGRESS BAR 5
// //
indicators::ProgressSpinner p; indicators::ProgressSpinner p{
p.set_prefix_text(""); option::PrefixText{""},
p.set_postfix_text("Checking credentials"); option::PostfixText{"Checking credentials"},
p.set_foreground_color(indicators::Color::YELLOW); option::ForegroundColor{indicators::Color::YELLOW},
p.set_spinner_states({"", "", "", "", "", "", "", ""}); option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "", "", ""}}
};
auto job = [&p]() { auto job = [&p]() {
while (true) { while (true) {
if (p.is_completed()) { if (p.is_completed()) {
p.set_foreground_color(indicators::Color::GREEN); p.set_option(option::ForegroundColor{indicators::Color::GREEN});
p.set_prefix_text(""); p.set_option(option::PrefixText{""});
p.hide_spinner(); p.set_option(option::SpinnerShow{false});
p.hide_percentage(); p.set_option(option::ShowPercentage{false});
p.set_postfix_text("Authenticated!"); p.set_option(option::PostfixText{"Authenticated!"});
p.mark_as_completed(); p.mark_as_completed();
break; break;
} else } else
@@ -217,42 +219,43 @@ int main() {
// //
// PROGRESS BAR 6 // PROGRESS BAR 6
// //
indicators::ProgressSpinner p; indicators::ProgressSpinner p{
p.set_prefix_text(" - "); option::PrefixText{" - "},
p.set_postfix_text("Searching for the Moon"); option::PostfixText{"Searching for the Moon"},
p.set_foreground_color(indicators::Color::WHITE); option::ForegroundColor{indicators::Color::WHITE},
p.set_spinner_states({"", "", "", ""}); option::ShowPercentage{false},
p.hide_percentage(); option::SpinnerStates{std::vector<std::string>{"", "", "", ""}}
};
auto job = [&p]() { auto job = [&p]() {
while (true) { while (true) {
auto current = p.current(); auto current = p.current();
if (current == 24) { if (current == 24) {
p.set_prefix_text(" - ✔"); p.set_option(option::PrefixText{" - ✔"});
p.hide_spinner(); p.set_option(option::SpinnerShow{false});
} else if (current == 25) { } else if (current == 25) {
std::cout << std::endl; std::cout << std::endl;
p.show_spinner(); p.set_option(option::SpinnerShow{true});
p.set_prefix_text(" - "); p.set_option(option::PrefixText{" - "});
p.set_postfix_text("Contacting Kerbal headquarters"); p.set_option(option::PostfixText{"Contacting Kerbal headquarters"});
} else if (current == 49) { } else if (current == 49) {
p.set_prefix_text(" - ✔"); p.set_option(option::PrefixText{" - ✔"});
p.hide_spinner(); p.set_option(option::SpinnerShow{false});
} else if (current == 50) { } else if (current == 50) {
std::cout << std::endl; std::cout << std::endl;
p.show_spinner(); p.set_option(option::SpinnerShow{true});
p.set_prefix_text(" - "); p.set_option(option::PrefixText{" - "});
p.set_postfix_text("Designing spaceship"); p.set_option(option::PostfixText{"Designing spaceship"});
} else if (current == 74) { } else if (current == 74) {
p.set_prefix_text(" - ✔"); p.set_option(option::PrefixText{" - ✔"});
p.hide_spinner(); p.set_option(option::SpinnerShow{false});
} else if (current == 75) { } else if (current == 75) {
std::cout << std::endl; std::cout << std::endl;
p.show_spinner(); p.set_option(option::SpinnerShow{true});
p.set_prefix_text(" - "); p.set_option(option::PrefixText{" - "});
p.set_postfix_text("Launching rocket"); p.set_option(option::PostfixText{"Launching rocket"});
} else if (current == 95) { } else if (current == 95) {
p.set_prefix_text(" - ✔"); p.set_option(option::PrefixText{" - ✔"});
p.hide_spinner(); p.set_option(option::SpinnerShow{false});
} else if (current == 99) { } else if (current == 99) {
std::cout << std::endl; std::cout << std::endl;
// //
@@ -291,7 +294,7 @@ int main() {
}; };
std::thread thread2(job2); std::thread thread2(job2);
thread2.join(); thread2.join();
p.set_postfix_text("Mission successful!"); p.set_option(indicators::option::PostfixText{"Mission successful!"});
p.mark_as_completed(); p.mark_as_completed();
break; break;
} }

View File

@@ -41,60 +41,79 @@ SOFTWARE.
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <tuple>
#include <type_traits>
namespace indicators { namespace indicators {
class ProgressBar { 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: public:
template <typename... Args> 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) : explicit ProgressBar(Args&&... args) :
settings_( settings_(
indicators::get<ProgressBarOption::BAR_WIDTH>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::PREFIX_TEXT>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::prefix_text>(option::PrefixText{}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::POSTFIX_TEXT>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::postfix_text>(option::PostfixText{}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::START>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::start>(option::Start{"["}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::END>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::end>(option::End{"]"}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::FILL>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::fill>(option::Fill{"="}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::LEAD>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::lead>(option::Lead{">"}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::REMAINDER>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::remainder>(option::Remainder{" "}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::max_postfix_text_len>(option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::COMPLETED>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::completed>(option::Completed{false}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::SHOW_PERCENTAGE>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::show_percentage>(option::ShowPercentage{false} ,std::forward<Args>(args)...),
indicators::get<ProgressBarOption::SHOW_ELAPSED_TIME>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::show_elapsed_time>(option::ShowElapsedTime{false}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::SHOW_REMAINING_TIME>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::show_remaining_time>(option::ShowRemainingTime{false}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::SAVED_START_TIME>(std::forward<Args>(args)...), details::get<details::ProgressBarOption::saved_start_time>(option::SavedStartTime{false}, std::forward<Args>(args)...),
indicators::get<ProgressBarOption::FOREGROUND_COLOR>(std::forward<Args>(args)...) details::get<details::ProgressBarOption::foreground_color>(option::ForegroundColor{Color::WHITE}, std::forward<Args>(args)...)
) )
{} {}
template <typename T, ProgressBarOption id> template <typename T, details::ProgressBarOption id>
void set_option(Setting<T, id>&& setting){ void set_option(details::Setting<T, id>&& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(detail::get<id>())>::type>::value, "Setting has wrong type!"); 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); std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = std::move(setting).value; get_value<id>() = std::move(setting).value;
} }
template <typename T, ProgressBarOption id> template <typename T, details::ProgressBarOption id>
void set_option(const Setting<T, id>& setting){ void set_option(const details::Setting<T, id>& setting){
static_assert(!std::is_same<T, typename std::decay<decltype(detail::get<id>())>::type>::value, "Setting has wrong type!"); 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); std::lock_guard<std::mutex> lock(_mutex);
get_value<id>() = setting.value; get_value<id>() = setting.value;
} }
void set_option(const Setting<std::string, ProgressBarOption::POSTFIX_TEXT>& setting){ void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
get_value<ProgressBarOption::POSTFIX_TEXT>() = setting.value; get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if(setting.value.length() > get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>()){ if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() = setting.value.length(); get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
} }
} }
void set_option(Setting<std::string, ProgressBarOption::POSTFIX_TEXT>&& setting){ void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
get_value<ProgressBarOption::POSTFIX_TEXT>() = std::move(setting).value; get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto& new_value = get_value<ProgressBarOption::POSTFIX_TEXT>(); auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
if(new_value.length() > get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>()){ if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() = new_value.length(); get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
} }
} }
@@ -122,40 +141,23 @@ public:
return std::min(static_cast<size_t>(_progress), size_t(100)); return std::min(static_cast<size_t>(_progress), size_t(100));
} }
bool is_completed() const { return get_value<ProgressBarOption::COMPLETED>(); } bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() { void mark_as_completed() {
get_value<ProgressBarOption::COMPLETED>() = true; get_value<details::ProgressBarOption::completed>() = true;
_print_progress(); _print_progress();
} }
private: private:
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
>;
template <ProgressBarOption id> template <details::ProgressBarOption id>
auto get_value() -> decltype((std::get<static_cast<int>(id)>(std::declval<Settings&>()).value)) { auto get_value() -> decltype((details::get_value<id>(std::declval<Settings&>()).value)) {
return std::get<static_cast<int>(id)>(settings_).value; return details::get_value<id>(settings_).value;
} }
template <ProgressBarOption id> template <details::ProgressBarOption id>
auto get_value() const -> decltype((std::get<static_cast<int>(id)>(std::declval<const Settings&>()).value)) { auto get_value() const -> decltype((details::get_value<id>(std::declval<const Settings&>()).value)) {
return std::get<static_cast<int>(id)>(settings_).value; return details::get_value<id>(settings_).value;
} }
float _progress{0}; float _progress{0};
@@ -168,9 +170,9 @@ private:
std::atomic<bool> _multi_progress_mode{false}; std::atomic<bool> _multi_progress_mode{false};
void _save_start_time() { void _save_start_time() {
auto& show_elapsed_time = get_value<ProgressBarOption::SHOW_ELAPSED_TIME>(); auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto& saved_start_time = get_value<ProgressBarOption::SAVED_START_TIME>(); auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
auto& show_remaining_time = get_value<ProgressBarOption::SHOW_REMAINING_TIME>(); auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) { if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
_start_time_point = std::chrono::high_resolution_clock::now(); _start_time_point = std::chrono::high_resolution_clock::now();
saved_start_time = true; saved_start_time = true;
@@ -180,40 +182,40 @@ private:
void _print_progress(bool from_multi_progress = false) { void _print_progress(bool from_multi_progress = false) {
if (_multi_progress_mode && !from_multi_progress) { if (_multi_progress_mode && !from_multi_progress) {
if (_progress > 100.0) { if (_progress > 100.0) {
get_value<ProgressBarOption::COMPLETED>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
return; return;
} }
std::lock_guard<std::mutex> lock{_mutex}; std::lock_guard<std::mutex> lock{_mutex};
auto now = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now();
if (!get_value<ProgressBarOption::COMPLETED>()) if (!get_value<details::ProgressBarOption::completed>())
_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point); _elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
std::cout << termcolor::bold; std::cout << termcolor::bold;
details::set_stream_color(std::cout, get_value<ProgressBarOption::FOREGROUND_COLOR>()); details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<ProgressBarOption::PREFIX_TEXT>(); std::cout << get_value<details::ProgressBarOption::prefix_text>();
std::cout << get_value<ProgressBarOption::START>(); std::cout << get_value<details::ProgressBarOption::start>();
details::ProgressScaleWriter writer{std::cout, get_value<ProgressBarOption::BAR_WIDTH>(), details::ProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>(),
get_value<ProgressBarOption::FILL>(), get_value<details::ProgressBarOption::fill>(),
get_value<ProgressBarOption::LEAD>(), get_value<details::ProgressBarOption::lead>(),
get_value<ProgressBarOption::REMAINDER>()}; get_value<details::ProgressBarOption::remainder>()};
writer.write(_progress); writer.write(_progress);
std::cout << get_value<ProgressBarOption::END>(); std::cout << get_value<details::ProgressBarOption::end>();
if (get_value<ProgressBarOption::SHOW_PERCENTAGE>()) { if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%"; std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
} }
if (get_value<ProgressBarOption::SHOW_ELAPSED_TIME>()) { if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " ["; std::cout << " [";
details::write_duration(std::cout, _elapsed); details::write_duration(std::cout, _elapsed);
} }
if (get_value<ProgressBarOption::SHOW_REMAINING_TIME>()) { if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<ProgressBarOption::SHOW_ELAPSED_TIME>()) if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<"; std::cout << "<";
else else
std::cout << " ["; std::cout << " [";
@@ -223,18 +225,18 @@ private:
details::write_duration(std::cout, remaining); details::write_duration(std::cout, remaining);
std::cout << "]"; std::cout << "]";
} else { } else {
if (get_value<ProgressBarOption::SHOW_ELAPSED_TIME>()) if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]"; std::cout << "]";
} }
if (get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() == 0) if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() = 10; get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<ProgressBarOption::POSTFIX_TEXT>() << std::string(get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(), ' ') << "\r"; std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
std::cout.flush(); std::cout.flush();
if (_progress > 100.0) { if (_progress > 100.0) {
get_value<ProgressBarOption::COMPLETED>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
if (get_value<ProgressBarOption::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; std::cout << termcolor::reset << std::endl;
} }
}; };

View File

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

View File

@@ -35,22 +35,70 @@ SOFTWARE.
namespace indicators{ 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{ enum class ProgressBarOption{
BAR_WIDTH=0, bar_width=0,
PREFIX_TEXT, prefix_text,
POSTFIX_TEXT, postfix_text,
START, start,
END, end,
FILL, fill,
LEAD, lead,
REMAINDER, remainder,
MAX_POSTFIX_TEXT_LEN, max_postfix_text_len,
COMPLETED, completed,
SHOW_PERCENTAGE, show_percentage,
SHOW_ELAPSED_TIME, show_elapsed_time,
SHOW_REMAINING_TIME, show_remaining_time,
SAVED_START_TIME, saved_start_time,
FOREGROUND_COLOR, foreground_color,
spinner_show,
spinner_states
}; };
template <typename T, ProgressBarOption Id> template <typename T, ProgressBarOption Id>
@@ -63,7 +111,7 @@ struct Setting{
static constexpr auto id = Id; static constexpr auto id = Id;
using type = T; using type = T;
T value; T value{};
}; };
template <typename T> template <typename T>
@@ -72,61 +120,54 @@ struct is_setting : std::false_type{};
template <ProgressBarOption Id, typename T> template <ProgressBarOption Id, typename T>
struct is_setting<Setting<T, Id>> : std::true_type{}; struct is_setting<Setting<T, Id>> : std::true_type{};
template <typename T, typename... Args>
struct are_settings_impl{
static constexpr bool value = is_setting<T>::value && are_settings_impl<Args...>::value;
};
template <typename T>
struct are_settings_impl<T> : is_setting<T>{};
template <typename... Args> template <typename... Args>
struct are_settings{ struct are_settings : if_else<conjuction<is_setting<Args>...>::value>::type {};
static constexpr bool value = are_settings_impl<Args...>::value;
};
template <> template <>
struct are_settings<>{ struct are_settings<> : std::true_type{};
static constexpr bool value = true;
}; 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 {};
namespace detail{
template <ProgressBarOption Id> template <ProgressBarOption Id>
struct always_true{ struct always_true{
static constexpr auto value = true; static constexpr auto value = true;
}; };
template <ProgressBarOption Id> template<ProgressBarOption Id, typename Default>
struct get_ret_type; Default&& get_impl(Default&& def){
return std::forward<Default>(def);
}
template<ProgressBarOption Id> template <ProgressBarOption Id, typename Default, typename T, typename... Args>
typename get_ret_type<Id>::type get(){ auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
static_assert(!always_true<Id>::value, "No default value for option specified!");
} // customization point, should never be called
template <ProgressBarOption Id, typename T, typename... Args>
auto get(T&& first, Args&&... tail) -> typename std::enable_if<
(std::decay<T>::type::id == Id), (std::decay<T>::type::id == Id),
decltype(std::forward<T>(first))> decltype(std::forward<T>(first))>
::type{ ::type{
return std::forward<T>(first); return std::forward<T>(first);
} }
template <ProgressBarOption Id, typename T, typename... Args> template <ProgressBarOption Id, typename Default, typename T, typename... Args>
auto get(T&& first, Args&&... tail) -> typename std::enable_if< auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if<
(std::decay<T>::type::id != Id), (std::decay<T>::type::id != Id),
decltype(get<Id>(std::forward<Args>(tail)...))>::type{ decltype(get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...))>::type{
return get<Id>(std::forward<Args>(tail)...); 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)...)){
template <ProgressBarOption Id, typename... Args, typename = typename std::enable_if<are_settings<Args...>::value, void>::type> return details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...);
auto get(Args&&... args) -> decltype(detail::get<Id>(std::forward<Args>(args)...)){
return detail::get<Id>(std::forward<Args>(args)...);
} }
template <ProgressBarOption Id> template <ProgressBarOption Id>
@@ -138,174 +179,44 @@ using IntegerSetting = Setting<std::size_t, Id>;
template <ProgressBarOption Id> template <ProgressBarOption Id>
using BooleanSetting = Setting<bool, Id>; using BooleanSetting = Setting<bool, Id>;
namespace option{ template <ProgressBarOption Id, typename Tuple, std::size_t counter =0>
using BarWidth = IntegerSetting<ProgressBarOption::BAR_WIDTH>; struct option_idx;
using PrefixText = StringSetting<ProgressBarOption::PREFIX_TEXT>;
using PostfixText = StringSetting<ProgressBarOption::POSTFIX_TEXT>; template <ProgressBarOption Id, typename T, typename... Settings, std::size_t counter>
using Start = StringSetting<ProgressBarOption::START>; struct option_idx<Id, std::tuple<T, Settings...>, counter> : if_else_type<(Id == T::id),
using End = StringSetting<ProgressBarOption::END>; std::integral_constant<std::size_t, counter>,
using Fill = StringSetting<ProgressBarOption::FILL>; option_idx<Id, std::tuple<Settings...>, counter+1>>::type{};
using Lead = StringSetting<ProgressBarOption::LEAD>;
using Remainder = StringSetting<ProgressBarOption::REMAINDER>; template <ProgressBarOption Id, std::size_t counter>
using MaxPostfixTextLen = IntegerSetting<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>; struct option_idx<Id, std::tuple<>, counter>{
using Completed = BooleanSetting<ProgressBarOption::COMPLETED>; static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found");
using ShowPercentage = BooleanSetting<ProgressBarOption::SHOW_PERCENTAGE>; };
using ShowElapsedTime = BooleanSetting<ProgressBarOption::SHOW_ELAPSED_TIME>;
using ShowRemainingTime = BooleanSetting<ProgressBarOption::SHOW_REMAINING_TIME>; template <ProgressBarOption Id, typename Settings>
using SavedStartTime = BooleanSetting<ProgressBarOption::SAVED_START_TIME>; auto get_value(Settings&& settings) -> decltype((std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::declval<Settings&&>()))){
using ForegroundColor = Setting<Color, ProgressBarOption::FOREGROUND_COLOR>; return std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(std::forward<Settings>(settings));
} }
namespace detail{ }
template<>
struct get_ret_type<ProgressBarOption::BAR_WIDTH>{
using type = ::indicators::option::BarWidth;
};
template<> get_ret_type<ProgressBarOption::BAR_WIDTH>::type
get<ProgressBarOption::BAR_WIDTH>(){
return indicators::option::BarWidth{100};
}
template<>
struct get_ret_type<ProgressBarOption::PREFIX_TEXT>{
using type = ::indicators::option::PrefixText;
};
template<> get_ret_type<ProgressBarOption::PREFIX_TEXT>::type
get<ProgressBarOption::PREFIX_TEXT>(){
return indicators::option::PrefixText{};
}
template<>
struct get_ret_type<ProgressBarOption::POSTFIX_TEXT>{
using type = ::indicators::option::PostfixText;
};
template<> get_ret_type<ProgressBarOption::POSTFIX_TEXT>::type
get<ProgressBarOption::POSTFIX_TEXT>(){
return indicators::option::PostfixText{};
}
template<>
struct get_ret_type<ProgressBarOption::START>{
using type = ::indicators::option::Start;
};
template<> get_ret_type<ProgressBarOption::START>::type
get<ProgressBarOption::START>(){
return indicators::option::Start{"["};
}
template<>
struct get_ret_type<ProgressBarOption::FILL>{
using type = ::indicators::option::Fill;
};
template<> get_ret_type<ProgressBarOption::FILL>::type
get<ProgressBarOption::FILL>(){
return indicators::option::Fill{"="};
}
template<>
struct get_ret_type<ProgressBarOption::LEAD>{
using type = ::indicators::option::Lead;
};
template<> get_ret_type<ProgressBarOption::LEAD>::type
get<ProgressBarOption::LEAD>(){
return indicators::option::Lead{">"};
}
template<>
struct get_ret_type<ProgressBarOption::REMAINDER>{
using type = ::indicators::option::Remainder;
};
template<> get_ret_type<ProgressBarOption::REMAINDER>::type
get<ProgressBarOption::REMAINDER>(){
return indicators::option::Remainder{" "};
}
template<>
struct get_ret_type<ProgressBarOption::END>{
using type = ::indicators::option::End;
};
template<> get_ret_type<ProgressBarOption::END>::type
get<ProgressBarOption::END>(){
return indicators::option::End{"]"};
}
template<>
struct get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>{
using type = ::indicators::option::MaxPostfixTextLen;
};
template<> get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>::type
get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(){
return indicators::option::MaxPostfixTextLen{0};
}
template<>
struct get_ret_type<ProgressBarOption::COMPLETED>{
using type = ::indicators::option::Completed;
};
template<> get_ret_type<ProgressBarOption::COMPLETED>::type
get<ProgressBarOption::COMPLETED>(){
return indicators::option::Completed{false};
}
template<>
struct get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>{
using type = ::indicators::option::ShowPercentage;
};
template<> get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>::type
get<ProgressBarOption::SHOW_PERCENTAGE>(){
return indicators::option::ShowPercentage{false};
}
template<>
struct get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>{
using type = ::indicators::option::ShowElapsedTime;
};
template<> get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>::type
get<ProgressBarOption::SHOW_ELAPSED_TIME>(){
return indicators::option::ShowElapsedTime{false};
}
template<>
struct get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>{
using type = ::indicators::option::ShowRemainingTime;
};
template<> get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>::type
get<ProgressBarOption::SHOW_REMAINING_TIME>(){
return indicators::option::ShowRemainingTime{false};
}
template<>
struct get_ret_type<ProgressBarOption::SAVED_START_TIME>{
using type = ::indicators::option::SavedStartTime;
};
template<> get_ret_type<ProgressBarOption::SAVED_START_TIME>::type
get<ProgressBarOption::SAVED_START_TIME>(){
return indicators::option::SavedStartTime{false};
}
template<>
struct get_ret_type<ProgressBarOption::FOREGROUND_COLOR>{
using type = ::indicators::option::ForegroundColor;
};
template<> get_ret_type<ProgressBarOption::FOREGROUND_COLOR>::type
get<ProgressBarOption::FOREGROUND_COLOR>(){
return indicators::option::ForegroundColor{::indicators::Color::WHITE};
}
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 SpinnerShow = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
using SpinnerStates = details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
} }
} }

View File

@@ -5,22 +5,21 @@ int main() {
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; std::cout << "\e[?25l";
indicators::ProgressSpinner spinner; indicators::ProgressSpinner spinner{
indicators::option::PostfixText{"Checking credentials"},
// Configure the spinner indicators::option::ForegroundColor{indicators::Color::YELLOW},
spinner.set_postfix_text("Checking credentials"); indicators::option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "", "", ""}},
spinner.set_foreground_color(indicators::Color::YELLOW); };
spinner.set_spinner_states({"", "", "", "", "", "", "", ""});
// Update spinner state // Update spinner state
auto job = [&spinner]() { auto job = [&spinner]() {
while (true) { while (true) {
if (spinner.is_completed()) { if (spinner.is_completed()) {
spinner.set_foreground_color(indicators::Color::GREEN); spinner.set_option(indicators::option::ForegroundColor{indicators::Color::GREEN});
spinner.set_prefix_text(""); spinner.set_option(indicators::option::PrefixText{""});
spinner.hide_spinner(); spinner.set_option(indicators::option::SpinnerShow{false});
spinner.hide_percentage(); spinner.set_option(indicators::option::ShowPercentage{false});
spinner.set_postfix_text("Authenticated!"); spinner.set_option(indicators::option::PostfixText{"Authenticated!"});
spinner.mark_as_completed(); spinner.mark_as_completed();
break; break;
} else } else