ProgressBar API change - make ProgressBar constructible with options

This commit is contained in:
pilarski
2020-02-03 21:12:33 +01:00
parent 3c8975aa34
commit 46bccdcc1c
7 changed files with 119 additions and 117 deletions

View File

@@ -64,19 +64,19 @@ public:
indicators::get<ProgressBarOption::SHOW_REMAINING_TIME>(std::forward<Args>(args)...), indicators::get<ProgressBarOption::SHOW_REMAINING_TIME>(std::forward<Args>(args)...),
indicators::get<ProgressBarOption::SAVED_START_TIME>(std::forward<Args>(args)...), indicators::get<ProgressBarOption::SAVED_START_TIME>(std::forward<Args>(args)...),
indicators::get<ProgressBarOption::FOREGROUND_COLOR>(std::forward<Args>(args)...) indicators::get<ProgressBarOption::FOREGROUND_COLOR>(std::forward<Args>(args)...)
) )
{} {}
template <typename T, ProgressBarOption id> template <typename T, ProgressBarOption id>
void set_option(Setting<T, id>&& setting){ void set_option(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(detail::get<id>())>::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, ProgressBarOption id>
void set_option(const Setting<T, id>& setting){ void set_option(const 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(detail::get<id>())>::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;
} }

View File

@@ -57,6 +57,9 @@ template <typename T, ProgressBarOption Id>
struct Setting{ struct Setting{
template <typename... Args, typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type> template <typename... Args, typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type>
explicit Setting(Args&&... args) : value(std::forward<Args>(args)...){} explicit Setting(Args&&... args) : value(std::forward<Args>(args)...){}
Setting(const Setting&) = default;
Setting(Setting&&) = default;
static constexpr auto id = Id; static constexpr auto id = Id;
using type = T; using type = T;
@@ -107,9 +110,9 @@ typename get_ret_type<Id>::type get(){
template <ProgressBarOption Id, typename T, typename... Args> template <ProgressBarOption Id, typename T, typename... Args>
auto get(T&& first, Args&&... tail) -> typename std::enable_if< 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).value)> decltype(std::forward<T>(first))>
::type{ ::type{
return std::forward<T>(first).value; return std::forward<T>(first);
} }
template <ProgressBarOption Id, typename T, typename... Args> template <ProgressBarOption Id, typename T, typename... Args>
@@ -156,152 +159,152 @@ namespace option{
namespace detail{ namespace detail{
template<> template<>
struct get_ret_type<ProgressBarOption::BAR_WIDTH>{ struct get_ret_type<ProgressBarOption::BAR_WIDTH>{
using type = std::size_t; using type = ::indicators::option::BarWidth;
}; };
template<> get_ret_type<ProgressBarOption::BAR_WIDTH>::type template<> get_ret_type<ProgressBarOption::BAR_WIDTH>::type
get<ProgressBarOption::BAR_WIDTH>(){ get<ProgressBarOption::BAR_WIDTH>(){
return std::size_t{100}; return indicators::option::BarWidth{100};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::PREFIX_TEXT>{ struct get_ret_type<ProgressBarOption::PREFIX_TEXT>{
using type = std::string; using type = ::indicators::option::PrefixText;
}; };
template<> get_ret_type<ProgressBarOption::PREFIX_TEXT>::type template<> get_ret_type<ProgressBarOption::PREFIX_TEXT>::type
get<ProgressBarOption::PREFIX_TEXT>(){ get<ProgressBarOption::PREFIX_TEXT>(){
return std::string{}; return indicators::option::PrefixText{};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::POSTFIX_TEXT>{ struct get_ret_type<ProgressBarOption::POSTFIX_TEXT>{
using type = std::string; using type = ::indicators::option::PostfixText;
}; };
template<> get_ret_type<ProgressBarOption::POSTFIX_TEXT>::type template<> get_ret_type<ProgressBarOption::POSTFIX_TEXT>::type
get<ProgressBarOption::POSTFIX_TEXT>(){ get<ProgressBarOption::POSTFIX_TEXT>(){
return std::string{}; return indicators::option::PostfixText{};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::START>{ struct get_ret_type<ProgressBarOption::START>{
using type = std::string; using type = ::indicators::option::Start;
}; };
template<> get_ret_type<ProgressBarOption::START>::type template<> get_ret_type<ProgressBarOption::START>::type
get<ProgressBarOption::START>(){ get<ProgressBarOption::START>(){
return std::string{"["}; return indicators::option::Start{"["};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::FILL>{ struct get_ret_type<ProgressBarOption::FILL>{
using type = std::string; using type = ::indicators::option::Fill;
}; };
template<> get_ret_type<ProgressBarOption::FILL>::type template<> get_ret_type<ProgressBarOption::FILL>::type
get<ProgressBarOption::FILL>(){ get<ProgressBarOption::FILL>(){
return std::string{"="}; return indicators::option::Fill{"="};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::LEAD>{ struct get_ret_type<ProgressBarOption::LEAD>{
using type = std::string; using type = ::indicators::option::Lead;
}; };
template<> get_ret_type<ProgressBarOption::LEAD>::type template<> get_ret_type<ProgressBarOption::LEAD>::type
get<ProgressBarOption::LEAD>(){ get<ProgressBarOption::LEAD>(){
return std::string{">"}; return indicators::option::Lead{">"};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::REMAINDER>{ struct get_ret_type<ProgressBarOption::REMAINDER>{
using type = std::string; using type = ::indicators::option::Remainder;
}; };
template<> get_ret_type<ProgressBarOption::REMAINDER>::type template<> get_ret_type<ProgressBarOption::REMAINDER>::type
get<ProgressBarOption::REMAINDER>(){ get<ProgressBarOption::REMAINDER>(){
return std::string{" "}; return indicators::option::Remainder{" "};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::END>{ struct get_ret_type<ProgressBarOption::END>{
using type = std::string; using type = ::indicators::option::End;
}; };
template<> get_ret_type<ProgressBarOption::END>::type template<> get_ret_type<ProgressBarOption::END>::type
get<ProgressBarOption::END>(){ get<ProgressBarOption::END>(){
return std::string{"]"}; return indicators::option::End{"]"};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>{ struct get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>{
using type = std::size_t; using type = ::indicators::option::MaxPostfixTextLen;
}; };
template<> get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>::type template<> get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>::type
get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(){ get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(){
return std::size_t{0}; return indicators::option::MaxPostfixTextLen{0};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::COMPLETED>{ struct get_ret_type<ProgressBarOption::COMPLETED>{
using type = bool; using type = ::indicators::option::Completed;
}; };
template<> get_ret_type<ProgressBarOption::COMPLETED>::type template<> get_ret_type<ProgressBarOption::COMPLETED>::type
get<ProgressBarOption::COMPLETED>(){ get<ProgressBarOption::COMPLETED>(){
return false; return indicators::option::Completed{false};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>{ struct get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>{
using type = bool; using type = ::indicators::option::ShowPercentage;
}; };
template<> get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>::type template<> get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>::type
get<ProgressBarOption::SHOW_PERCENTAGE>(){ get<ProgressBarOption::SHOW_PERCENTAGE>(){
return true; return indicators::option::ShowPercentage{false};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>{ struct get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>{
using type = bool; using type = ::indicators::option::ShowElapsedTime;
}; };
template<> get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>::type template<> get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>::type
get<ProgressBarOption::SHOW_ELAPSED_TIME>(){ get<ProgressBarOption::SHOW_ELAPSED_TIME>(){
return false; return indicators::option::ShowElapsedTime{false};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>{ struct get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>{
using type = bool; using type = ::indicators::option::ShowRemainingTime;
}; };
template<> get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>::type template<> get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>::type
get<ProgressBarOption::SHOW_REMAINING_TIME>(){ get<ProgressBarOption::SHOW_REMAINING_TIME>(){
return false; return indicators::option::ShowRemainingTime{false};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::SAVED_START_TIME>{ struct get_ret_type<ProgressBarOption::SAVED_START_TIME>{
using type = bool; using type = ::indicators::option::SavedStartTime;
}; };
template<> get_ret_type<ProgressBarOption::SAVED_START_TIME>::type template<> get_ret_type<ProgressBarOption::SAVED_START_TIME>::type
get<ProgressBarOption::SAVED_START_TIME>(){ get<ProgressBarOption::SAVED_START_TIME>(){
return false; return indicators::option::SavedStartTime{false};
} }
template<> template<>
struct get_ret_type<ProgressBarOption::FOREGROUND_COLOR>{ struct get_ret_type<ProgressBarOption::FOREGROUND_COLOR>{
using type = ::indicators::Color; using type = ::indicators::option::ForegroundColor;
}; };
template<> get_ret_type<ProgressBarOption::FOREGROUND_COLOR>::type template<> get_ret_type<ProgressBarOption::FOREGROUND_COLOR>::type
get<ProgressBarOption::FOREGROUND_COLOR>(){ get<ProgressBarOption::FOREGROUND_COLOR>(){
return ::indicators::Color::WHITE; return indicators::option::ForegroundColor{::indicators::Color::WHITE};
} }
} }

View File

@@ -3,41 +3,44 @@
int main() { int main() {
indicators::ProgressBar bar1; indicators::ProgressBar bar1{
bar1.set_bar_width(50); indicators::option::BarWidth{50},
bar1.start_bar_with("["); indicators::option::Start{"["},
bar1.fill_bar_progress_with(""); indicators::option::Fill{""},
bar1.lead_bar_progress_with(""); indicators::option::Lead{""},
bar1.fill_bar_remainder_with(" "); indicators::option::Remainder{" "},
bar1.end_bar_with(" ]"); indicators::option::End{" ]"},
bar1.set_foreground_color(indicators::Color::YELLOW); indicators::option::ForegroundColor{indicators::Color::YELLOW},
bar1.show_elapsed_time(); indicators::option::ShowElapsedTime{true},
bar1.show_remaining_time(); indicators::option::ShowRemainingTime{true},
bar1.set_prefix_text("Progress Bar #1 "); indicators::option::PrefixText{"Progress Bar #1 "}
};
indicators::ProgressBar bar2; indicators::ProgressBar bar2{
bar2.set_bar_width(50); indicators::option::BarWidth{50},
bar2.start_bar_with("["); indicators::option::Start{"["},
bar2.fill_bar_progress_with("="); indicators::option::Fill{"="},
bar2.lead_bar_progress_with(">"); indicators::option::Lead{">"},
bar2.fill_bar_remainder_with(" "); indicators::option::Remainder{" "},
bar2.end_bar_with(" ]"); indicators::option::End{" ]"},
bar2.set_foreground_color(indicators::Color::CYAN); indicators::option::ForegroundColor{indicators::Color::CYAN},
bar2.show_elapsed_time(); indicators::option::ShowElapsedTime{true},
bar2.show_remaining_time(); indicators::option::ShowRemainingTime{true},
bar2.set_prefix_text("Progress Bar #2 "); indicators::option::PrefixText{"Progress Bar #2 "}
};
indicators::ProgressBar bar3; indicators::ProgressBar bar3{
bar3.set_bar_width(50); indicators::option::BarWidth{50},
bar3.start_bar_with("["); indicators::option::Start{"["},
bar3.fill_bar_progress_with("#"); indicators::option::Fill{"#"},
bar3.lead_bar_progress_with("#"); indicators::option::Lead{"#"},
bar3.fill_bar_remainder_with(" "); indicators::option::Remainder{" "},
bar3.end_bar_with(" ]"); indicators::option::End{" ]"},
bar3.set_foreground_color(indicators::Color::RED); indicators::option::ForegroundColor{indicators::Color::CYAN},
bar3.show_elapsed_time(); indicators::option::ShowElapsedTime{true},
bar3.show_remaining_time(); indicators::option::ShowRemainingTime{true},
bar3.set_prefix_text("Progress Bar #3 "); indicators::option::PrefixText{"Progress Bar #3 "}
};
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3); indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);

View File

@@ -3,14 +3,15 @@
int main() { int main() {
indicators::ProgressBar bar; indicators::ProgressBar bar{
bar.set_bar_width(50); indicators::option::BarWidth{50},
bar.start_bar_with("["); indicators::option::Start{"["},
bar.fill_bar_progress_with(""); indicators::option::Fill{""},
bar.lead_bar_progress_with(""); indicators::option::Lead{""},
bar.fill_bar_remainder_with("-"); indicators::option::Remainder{"-"},
bar.end_bar_with("]"); indicators::option::End{" ]"},
bar.set_foreground_color(indicators::Color::YELLOW); indicators::option::ForegroundColor{indicators::Color::YELLOW},
};
// As configured, the bar will look like this: // As configured, the bar will look like this:
// //
@@ -39,7 +40,7 @@ int main() {
if (bar.is_completed()) { if (bar.is_completed()) {
break; break;
} }
bar.set_postfix_text(status_text[index % status_text.size()]); bar.set_option(indicators::option::PostfixText{status_text[index % status_text.size()]});
bar.tick(); bar.tick();
index += 1; index += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));

View File

@@ -7,17 +7,16 @@ int main() {
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; std::cout << "\e[?25l";
indicators::ProgressBar bar; indicators::ProgressBar bar{
indicators::option::BarWidth{50},
// Configure the bar indicators::option::Start{"["},
bar.set_bar_width(50); indicators::option::Fill{""},
bar.start_bar_with("["); indicators::option::Lead{""},
bar.fill_bar_progress_with(""); indicators::option::Remainder{"-"},
bar.lead_bar_progress_with(""); indicators::option::End{" ]"},
bar.fill_bar_remainder_with("-"); indicators::option::PostfixText{"Loading dependency 1/4"},
bar.end_bar_with(" ]"); indicators::option::ForegroundColor{indicators::Color::CYAN},
bar.set_postfix_text("Loading dependency 1/4"); };
bar.set_foreground_color(indicators::Color::CYAN);
// Update bar state // Update bar state
bar.set_progress(10); // 10% done bar.set_progress(10); // 10% done
@@ -25,21 +24,21 @@ int main() {
// do some work // do some work
std::this_thread::sleep_for(std::chrono::milliseconds(800)); std::this_thread::sleep_for(std::chrono::milliseconds(800));
bar.set_postfix_text("Loading dependency 2/4"); bar.set_option(indicators::option::PostfixText{"Loading dependency 2/4"});
bar.set_progress(30); // 30% done bar.set_progress(30); // 30% done
// do some more work // do some more work
std::this_thread::sleep_for(std::chrono::milliseconds(700)); std::this_thread::sleep_for(std::chrono::milliseconds(700));
bar.set_postfix_text("Loading dependency 3/4"); bar.set_option(indicators::option::PostfixText{"Loading dependency 3/4"});
bar.set_progress(65); // 65% done bar.set_progress(65); // 65% done
// do final bit of work // do final bit of work
std::this_thread::sleep_for(std::chrono::milliseconds(900)); std::this_thread::sleep_for(std::chrono::milliseconds(900));
bar.set_postfix_text("Loaded dependencies!"); bar.set_option(indicators::option::PostfixText{"Loaded dependencies!"});
bar.set_progress(100); // all done bar.set_progress(100); // all done

View File

@@ -3,17 +3,16 @@
#include <thread> #include <thread>
int main() { int main() {
indicators::ProgressBar bar; indicators::ProgressBar bar{
indicators::option::BarWidth{50},
// Configure the bar indicators::option::Start{"["},
bar.set_bar_width(50); indicators::option::Fill{"="},
bar.start_bar_with("["); indicators::option::Lead{">"},
bar.fill_bar_progress_with("="); indicators::option::Remainder{" "},
bar.lead_bar_progress_with(">"); indicators::option::End{" ]"},
bar.fill_bar_remainder_with(" "); indicators::option::PostfixText{"Getting started"},
bar.end_bar_with("]"); indicators::option::ForegroundColor{indicators::Color::GREEN},
bar.set_postfix_text("Getting started"); };
bar.set_foreground_color(indicators::Color::GREEN);
// Update bar state // Update bar state
while (true) { while (true) {

View File

@@ -3,21 +3,18 @@
#include <thread> #include <thread>
int main() { int main() {
indicators::ProgressBar bar; indicators::ProgressBar bar{
indicators::option::BarWidth{50},
// Configure the bar indicators::option::Start{" ["},
bar.set_bar_width(50); indicators::option::Fill{""},
bar.start_bar_with(" ["); indicators::option::Lead{""},
bar.fill_bar_progress_with(""); indicators::option::Remainder{"-"},
bar.lead_bar_progress_with(""); indicators::option::End{"]"},
bar.fill_bar_remainder_with("-"); indicators::option::PostfixText{"Training Gaze Network "},
bar.end_bar_with("]"); indicators::option::ForegroundColor{indicators::Color::YELLOW},
bar.set_prefix_text("Training Gaze Network 👀"); indicators::option::ShowElapsedTime{true},
bar.set_foreground_color(indicators::Color::YELLOW); indicators::option::ShowRemainingTime{true},
};
// Show time elapsed and remaining
bar.show_elapsed_time();
bar.show_remaining_time();
// Update bar state // Update bar state
while (true) { while (true) {