mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
API change - progress bar can be constructed with proper settings.
This commit is contained in:
156
demo/demo.cpp
156
demo/demo.cpp
@@ -6,19 +6,18 @@ int main() {
|
|||||||
|
|
||||||
// Hide cursor
|
// Hide cursor
|
||||||
std::cout << "\e[?25l";
|
std::cout << "\e[?25l";
|
||||||
|
using namespace indicators;
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// PROGRESS BAR 1
|
// PROGRESS BAR 1
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p{option::BarWidth{50},
|
||||||
p.set_bar_width(50);
|
option::Start{"{"},
|
||||||
p.start_bar_with("[");
|
option::Fill{"■"},
|
||||||
p.fill_bar_progress_with("■");
|
option::Lead{"■"},
|
||||||
p.lead_bar_progress_with("■");
|
option::Remainder{" "},
|
||||||
p.fill_bar_remainder_with(" ");
|
option::End{" ]"},
|
||||||
p.end_bar_with(" ]");
|
option::ForegroundColor{indicators::Color::YELLOW}};
|
||||||
p.set_foreground_color(indicators::Color::YELLOW);
|
|
||||||
|
|
||||||
std::atomic<size_t> index{0};
|
std::atomic<size_t> index{0};
|
||||||
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
||||||
@@ -35,7 +34,7 @@ int main() {
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
p.set_postfix_text(status_text[index % status_text.size()]);
|
p.set_option(option::PostfixText{status_text[index % status_text.size()]});
|
||||||
p.set_progress(index * 10);
|
p.set_progress(index * 10);
|
||||||
index += 1;
|
index += 1;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||||
@@ -50,20 +49,20 @@ int main() {
|
|||||||
// PROGRESS BAR 2
|
// PROGRESS BAR 2
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p;
|
||||||
p.set_bar_width(40);
|
p.set_option(option::BarWidth{40});
|
||||||
p.set_prefix_text("Reading package list... ");
|
p.set_option(option::PrefixText{"Reading package list... "});
|
||||||
p.start_bar_with("");
|
p.set_option(option::Start{""});
|
||||||
p.fill_bar_progress_with("");
|
p.set_option(option::Fill{""});
|
||||||
p.lead_bar_progress_with("");
|
p.set_option(option::Lead{""});
|
||||||
p.fill_bar_remainder_with("");
|
p.set_option(option::Remainder{""});
|
||||||
p.end_bar_with("");
|
p.set_option(option::End{""});
|
||||||
p.set_foreground_color(indicators::Color::WHITE);
|
p.set_option(option::ForegroundColor{indicators::Color::WHITE});
|
||||||
p.hide_percentage();
|
p.set_option(option::ShowPercentage{false});
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p.set_prefix_text("Reading package list... " + std::to_string(p.current()) + "% ");
|
p.set_option(option::PrefixText{"Reading package list... " + std::to_string(p.current()) + "% "});
|
||||||
if (p.current() + 2 >= 100)
|
if (p.current() + 2 >= 100)
|
||||||
p.set_prefix_text("Reading package list... Done");
|
p.set_option(option::PrefixText{"Reading package list... Done"});
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed()) {
|
if (p.is_completed()) {
|
||||||
break;
|
break;
|
||||||
@@ -80,25 +79,25 @@ int main() {
|
|||||||
// PROGRESS BAR 3
|
// PROGRESS BAR 3
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p;
|
||||||
p.set_bar_width(50);
|
p.set_option(option::BarWidth{50});
|
||||||
p.start_bar_with("[");
|
p.set_option(option::Start{"["});
|
||||||
p.fill_bar_progress_with("=");
|
p.set_option(option::Fill{"="});
|
||||||
p.lead_bar_progress_with(">");
|
p.set_option(option::Lead{">"});
|
||||||
p.fill_bar_remainder_with(" ");
|
p.set_option(option::Remainder{" "});
|
||||||
p.end_bar_with("]");
|
p.set_option(option::End{"]"});
|
||||||
p.set_postfix_text("Getting started");
|
p.set_option(option::PostfixText{"Getting started"});
|
||||||
p.set_foreground_color(indicators::Color::GREEN);
|
p.set_option(option::ForegroundColor{indicators::Color::GREEN});
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p.current();
|
auto ticks = p.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p.set_postfix_text("Delaying the inevitable");
|
p.set_option(option::PostfixText{"Delaying the inevitable"});
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p.set_postfix_text("Crying quietly");
|
p.set_option(option::PostfixText{"Crying quietly"});
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p.set_postfix_text("Almost there");
|
p.set_option(option::PostfixText{"Almost there"});
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p.set_postfix_text("Done");
|
p.set_option(option::PostfixText{"Done"});
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
@@ -115,32 +114,31 @@ int main() {
|
|||||||
//
|
//
|
||||||
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||||
indicators::ProgressBar p4;
|
indicators::ProgressBar p4;
|
||||||
p4.set_bar_width(40);
|
p4.set_option(option::BarWidth{40});
|
||||||
p4.start_bar_with("");
|
p4.set_option(option::Start{""});
|
||||||
p4.fill_bar_progress_with("⠸");
|
p4.set_option(option::Fill{"⠸"});
|
||||||
p4.lead_bar_progress_with("");
|
p4.set_option(option::Lead{""});
|
||||||
p4.fill_bar_remainder_with(" ");
|
p4.set_option(option::Remainder{" "});
|
||||||
p4.end_bar_with("");
|
p4.set_option(option::End{""});
|
||||||
p4.set_foreground_color(indicators::Color::CYAN);
|
p4.set_option(option::ForegroundColor{indicators::Color::CYAN});
|
||||||
p4.set_postfix_text("Restoring system state");
|
p4.set_option(option::PostfixText{"Restoring system state"});
|
||||||
p4.hide_percentage();
|
p4.set_option(option::ShowPercentage{false});
|
||||||
std::atomic<size_t> index4{0};
|
std::atomic<size_t> index4{0};
|
||||||
auto job4 = [&p4, &index4, &lead_spinner]() {
|
auto job4 = [&p4, &index4, &lead_spinner]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
|
p4.set_option(option::PrefixText{"{ " + std::to_string(p4.current()) + "% } "});
|
||||||
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
|
p4.set_option(option::Lead{lead_spinner[index4 % lead_spinner.size()]});
|
||||||
index4 += 1;
|
index4 += 1;
|
||||||
if (p4.current() + 2 >= 100) {
|
if (p4.current() + 2 >= 100) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p4.set_foreground_color(indicators::Color::RED);
|
p4.set_option(option::ForegroundColor{indicators::Color::RED});
|
||||||
p4.set_prefix_text("{ ERROR }");
|
p4.set_option(option::PrefixText{"{ ERROR }"});
|
||||||
p4.start_bar_with("");
|
p4.set_option(option::Start{});
|
||||||
p4.fill_bar_progress_with("");
|
p4.set_option(option::Fill{});
|
||||||
p4.lead_bar_progress_with("");
|
p4.set_option(option::Lead{});
|
||||||
p4.fill_bar_remainder_with("");
|
p4.set_option(option::Remainder{});
|
||||||
p4.end_bar_with("");
|
p4.set_option(option::ShowPercentage{true});
|
||||||
p4.show_percentage();
|
p4.set_option(option::PostfixText{"Failed to restore system"});
|
||||||
p4.set_postfix_text("Failed to restore system");
|
|
||||||
p4.mark_as_completed();
|
p4.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -157,23 +155,24 @@ int main() {
|
|||||||
//
|
//
|
||||||
// GOING BACKWARDS
|
// GOING BACKWARDS
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p;
|
indicators::ProgressBar p{
|
||||||
p.set_bar_width(50);
|
option::BarWidth {50},
|
||||||
p.start_bar_with("[");
|
option::Start{"["},
|
||||||
p.fill_bar_progress_with("■");
|
option::Fill{"■"},
|
||||||
p.lead_bar_progress_with("■");
|
option::Lead{"■"},
|
||||||
p.fill_bar_remainder_with("-");
|
option::Remainder{"-"},
|
||||||
p.end_bar_with("]");
|
option::End{"]"},
|
||||||
p.set_progress(100);
|
option::ForegroundColor{indicators::Color::WHITE},
|
||||||
p.set_foreground_color(indicators::Color::WHITE);
|
option::PostfixText{"Reverting system restore"}
|
||||||
p.set_postfix_text("Reverting system restore");
|
};
|
||||||
|
p.set_progress(100); // TODO backwards as an option?
|
||||||
std::atomic<size_t> progress{100};
|
std::atomic<size_t> progress{100};
|
||||||
auto job = [&p, &progress]() {
|
auto job = [&p, &progress]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
progress -= 1;
|
progress -= 1;
|
||||||
p.set_progress(progress);
|
p.set_progress(progress);
|
||||||
if (progress == 0) {
|
if (progress == 0) {
|
||||||
p.set_postfix_text("Revert complete!");
|
p.set_option(option::PostfixText{"Revert complete!"});
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -259,29 +258,30 @@ int main() {
|
|||||||
//
|
//
|
||||||
// NESTED PROGRESS BAR
|
// NESTED PROGRESS BAR
|
||||||
//
|
//
|
||||||
indicators::ProgressBar p2;
|
indicators::ProgressBar p2{
|
||||||
p2.set_bar_width(30);
|
option::BarWidth{30},
|
||||||
p2.set_prefix_text(" - ");
|
option::PrefixText{" - "},
|
||||||
p2.start_bar_with("🌎");
|
option::Start{"🌎"},
|
||||||
p2.fill_bar_progress_with("·");
|
option::Fill{"·"},
|
||||||
p2.lead_bar_progress_with("🚀");
|
option::Lead{"🚀"},
|
||||||
p2.fill_bar_remainder_with(" ");
|
option::Remainder{" "},
|
||||||
p2.end_bar_with("🌑");
|
option::End{"🌑"},
|
||||||
p2.set_postfix_text("Achieved low-Earth orbit");
|
option::PostfixText{"Achieved low-Earth orbit"},
|
||||||
p2.set_foreground_color(indicators::Color::WHITE);
|
option::ForegroundColor{indicators::Color::WHITE}
|
||||||
|
};
|
||||||
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
||||||
std::atomic<int> ship_trail_index{0};
|
std::atomic<int> ship_trail_index{0};
|
||||||
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p2.current();
|
auto ticks = p2.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p2.set_postfix_text("Switching to trans-lunar trajectory");
|
p2.set_option(option::PostfixText{"Switching to trans-lunar trajectory"});
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p2.set_postfix_text("Transferred to Lunar lander");
|
p2.set_option(option::PostfixText{"Transferred to Lunar lander"});
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p2.set_postfix_text("Almost there");
|
p2.set_option(option::PostfixText{"Almost there"});
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p2.set_postfix_text("Landed on the Moon");
|
p2.set_option(option::PostfixText{"Landed on the Moon"});
|
||||||
p2.tick();
|
p2.tick();
|
||||||
ship_trail_index += 1;
|
ship_trail_index += 1;
|
||||||
if (p2.is_completed())
|
if (p2.is_completed())
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ SOFTWARE.
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <indicators/color.hpp>
|
#include <indicators/color.hpp>
|
||||||
|
#include <indicators/setting.hpp>
|
||||||
|
#include <tuple>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -44,70 +46,64 @@ namespace indicators {
|
|||||||
|
|
||||||
class ProgressBar {
|
class ProgressBar {
|
||||||
public:
|
public:
|
||||||
void set_foreground_color(Color color) {
|
template <typename... Args>
|
||||||
std::lock_guard<std::mutex> lock{_mutex};
|
explicit ProgressBar(Args&&... args) :
|
||||||
_foreground_color = color;
|
settings_(
|
||||||
|
indicators::get<ProgressBarOption::BAR_WIDTH>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::PREFIX_TEXT>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::POSTFIX_TEXT>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::START>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::END>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::FILL>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::LEAD>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::REMAINDER>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::COMPLETED>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::SHOW_PERCENTAGE>(std::forward<Args>(args)...),
|
||||||
|
indicators::get<ProgressBarOption::SHOW_ELAPSED_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::FOREGROUND_COLOR>(std::forward<Args>(args)...)
|
||||||
|
)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T, ProgressBarOption id>
|
||||||
|
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!");
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<id>() = std::move(setting).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bar_width(size_t bar_width) {
|
template <typename T, ProgressBarOption id>
|
||||||
std::lock_guard<std::mutex> lock{_mutex};
|
void set_option(const Setting<T, id>& setting){
|
||||||
_bar_width = bar_width;
|
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);
|
||||||
|
get_value<id>() = setting.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_bar_with(const std::string &start) {
|
void set_option(const Setting<std::string, ProgressBarOption::POSTFIX_TEXT>& setting){
|
||||||
std::lock_guard<std::mutex> lock{_mutex};
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
_start = start;
|
get_value<ProgressBarOption::POSTFIX_TEXT>() = setting.value;
|
||||||
}
|
if(setting.value.length() > get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>()){
|
||||||
|
get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() = setting.value.length();
|
||||||
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(Setting<std::string, ProgressBarOption::POSTFIX_TEXT>&& setting){
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
get_value<ProgressBarOption::POSTFIX_TEXT>() = std::move(setting).value;
|
||||||
|
auto& new_value = get_value<ProgressBarOption::POSTFIX_TEXT>();
|
||||||
|
if(new_value.length() > get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>()){
|
||||||
|
get_value<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();
|
_save_start_time();
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
@@ -126,78 +122,98 @@ 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<ProgressBarOption::COMPLETED>(); }
|
||||||
|
|
||||||
void mark_as_completed() {
|
void mark_as_completed() {
|
||||||
_completed = true;
|
get_value<ProgressBarOption::COMPLETED>() = true;
|
||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _progress{0.0};
|
using Settings = std::tuple<
|
||||||
size_t _bar_width{100};
|
option::BarWidth,
|
||||||
std::string _prefix_text{""};
|
option::PrefixText,
|
||||||
std::string _start{"["};
|
option::PostfixText,
|
||||||
std::string _fill{"="};
|
option::Start,
|
||||||
std::string _lead{">"};
|
option::End,
|
||||||
std::string _remainder{" "};
|
option::Fill,
|
||||||
std::string _end{"]"};
|
option::Lead,
|
||||||
std::string _postfix_text{""};
|
option::Remainder,
|
||||||
std::atomic<size_t> _max_postfix_text_length{0};
|
option::MaxPostfixTextLen,
|
||||||
std::atomic<bool> _completed{false};
|
option::Completed,
|
||||||
std::atomic<bool> _show_percentage{true};
|
option::ShowPercentage,
|
||||||
std::atomic<bool> _show_elapsed_time{false};
|
option::ShowElapsedTime,
|
||||||
std::atomic<bool> _show_remaining_time{false};
|
option::ShowRemainingTime,
|
||||||
std::atomic<bool> _saved_start_time{false};
|
option::SavedStartTime,
|
||||||
|
option::ForegroundColor
|
||||||
|
>;
|
||||||
|
|
||||||
|
template <ProgressBarOption id>
|
||||||
|
auto get_value() -> decltype((std::get<static_cast<int>(id)>(std::declval<Settings&>()).value)) {
|
||||||
|
return std::get<static_cast<int>(id)>(settings_).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption id>
|
||||||
|
auto get_value() const -> decltype((std::get<static_cast<int>(id)>(std::declval<const Settings&>()).value)) {
|
||||||
|
return std::get<static_cast<int>(id)>(settings_).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
float _progress{0};
|
||||||
|
Settings settings_;
|
||||||
std::chrono::nanoseconds _elapsed;
|
std::chrono::nanoseconds _elapsed;
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
Color _foreground_color{indicators::Color::WHITE};
|
|
||||||
|
|
||||||
template <typename Indicator, size_t count> friend class MultiProgress;
|
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||||
std::atomic<bool> _multi_progress_mode{false};
|
std::atomic<bool> _multi_progress_mode{false};
|
||||||
|
|
||||||
void _save_start_time() {
|
void _save_start_time() {
|
||||||
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
|
auto& show_elapsed_time = get_value<ProgressBarOption::SHOW_ELAPSED_TIME>();
|
||||||
|
auto& saved_start_time = get_value<ProgressBarOption::SAVED_START_TIME>();
|
||||||
|
auto& show_remaining_time = get_value<ProgressBarOption::SHOW_REMAINING_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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
_completed = true;
|
get_value<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 (!_completed)
|
if (!get_value<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, _foreground_color);
|
details::set_stream_color(std::cout, get_value<ProgressBarOption::FOREGROUND_COLOR>());
|
||||||
std::cout << _prefix_text;
|
std::cout << get_value<ProgressBarOption::PREFIX_TEXT>();
|
||||||
|
|
||||||
std::cout << _start;
|
std::cout << get_value<ProgressBarOption::START>();
|
||||||
|
|
||||||
details::ProgressScaleWriter writer{std::cout, _bar_width, _fill, _lead, _remainder};
|
details::ProgressScaleWriter writer{std::cout, get_value<ProgressBarOption::BAR_WIDTH>(),
|
||||||
|
get_value<ProgressBarOption::FILL>(),
|
||||||
|
get_value<ProgressBarOption::LEAD>(),
|
||||||
|
get_value<ProgressBarOption::REMAINDER>()};
|
||||||
writer.write(_progress);
|
writer.write(_progress);
|
||||||
|
|
||||||
std::cout << _end;
|
std::cout << get_value<ProgressBarOption::END>();
|
||||||
|
|
||||||
if (_show_percentage) {
|
if (get_value<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<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<ProgressBarOption::SHOW_REMAINING_TIME>()) {
|
||||||
if (_show_elapsed_time)
|
if (get_value<ProgressBarOption::SHOW_ELAPSED_TIME>())
|
||||||
std::cout << "<";
|
std::cout << "<";
|
||||||
else
|
else
|
||||||
std::cout << " [";
|
std::cout << " [";
|
||||||
@@ -207,18 +223,18 @@ 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<ProgressBarOption::SHOW_ELAPSED_TIME>())
|
||||||
std::cout << "]";
|
std::cout << "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_max_postfix_text_length == 0)
|
if (get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() == 0)
|
||||||
_max_postfix_text_length = 10;
|
get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>() = 10;
|
||||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
std::cout << " " << get_value<ProgressBarOption::POSTFIX_TEXT>() << std::string(get_value<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(), ' ') << "\r";
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
if (_progress > 100.0) {
|
if (_progress > 100.0) {
|
||||||
_completed = true;
|
get_value<ProgressBarOption::COMPLETED>() = true;
|
||||||
}
|
}
|
||||||
if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
if (get_value<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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
308
include/indicators/setting.hpp
Normal file
308
include/indicators/setting.hpp
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
/*
|
||||||
|
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 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.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{
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
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)...){}
|
||||||
|
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 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>
|
||||||
|
struct are_settings{
|
||||||
|
static constexpr bool value = are_settings_impl<Args...>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct are_settings<>{
|
||||||
|
static constexpr bool value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace detail{
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
struct always_true{
|
||||||
|
static constexpr auto value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <ProgressBarOption Id>
|
||||||
|
struct get_ret_type;
|
||||||
|
|
||||||
|
template<ProgressBarOption Id>
|
||||||
|
typename get_ret_type<Id>::type get(){
|
||||||
|
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),
|
||||||
|
decltype(std::forward<T>(first).value)>
|
||||||
|
::type{
|
||||||
|
return std::forward<T>(first).value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename T, typename... Args>
|
||||||
|
auto get(T&& first, Args&&... tail) -> typename std::enable_if<
|
||||||
|
(std::decay<T>::type::id != Id),
|
||||||
|
decltype(get<Id>(std::forward<Args>(tail)...))>::type{
|
||||||
|
return get<Id>(std::forward<Args>(tail)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ProgressBarOption Id, typename... Args, typename = typename std::enable_if<are_settings<Args...>::value, void>::type>
|
||||||
|
auto get(Args&&... args) -> decltype(detail::get<Id>(std::forward<Args>(args)...)){
|
||||||
|
return detail::get<Id>(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>;
|
||||||
|
|
||||||
|
namespace option{
|
||||||
|
using BarWidth = IntegerSetting<ProgressBarOption::BAR_WIDTH>;
|
||||||
|
using PrefixText = StringSetting<ProgressBarOption::PREFIX_TEXT>;
|
||||||
|
using PostfixText = StringSetting<ProgressBarOption::POSTFIX_TEXT>;
|
||||||
|
using Start = StringSetting<ProgressBarOption::START>;
|
||||||
|
using End = StringSetting<ProgressBarOption::END>;
|
||||||
|
using Fill = StringSetting<ProgressBarOption::FILL>;
|
||||||
|
using Lead = StringSetting<ProgressBarOption::LEAD>;
|
||||||
|
using Remainder = StringSetting<ProgressBarOption::REMAINDER>;
|
||||||
|
using MaxPostfixTextLen = IntegerSetting<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>;
|
||||||
|
using Completed = BooleanSetting<ProgressBarOption::COMPLETED>;
|
||||||
|
using ShowPercentage = BooleanSetting<ProgressBarOption::SHOW_PERCENTAGE>;
|
||||||
|
using ShowElapsedTime = BooleanSetting<ProgressBarOption::SHOW_ELAPSED_TIME>;
|
||||||
|
using ShowRemainingTime = BooleanSetting<ProgressBarOption::SHOW_REMAINING_TIME>;
|
||||||
|
using SavedStartTime = BooleanSetting<ProgressBarOption::SAVED_START_TIME>;
|
||||||
|
using ForegroundColor = Setting<Color, ProgressBarOption::FOREGROUND_COLOR>;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace detail{
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::BAR_WIDTH>{
|
||||||
|
using type = std::size_t;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::BAR_WIDTH>::type
|
||||||
|
get<ProgressBarOption::BAR_WIDTH>(){
|
||||||
|
return std::size_t{100};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::PREFIX_TEXT>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::PREFIX_TEXT>::type
|
||||||
|
get<ProgressBarOption::PREFIX_TEXT>(){
|
||||||
|
return std::string{};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::POSTFIX_TEXT>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::POSTFIX_TEXT>::type
|
||||||
|
get<ProgressBarOption::POSTFIX_TEXT>(){
|
||||||
|
return std::string{};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::START>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::START>::type
|
||||||
|
get<ProgressBarOption::START>(){
|
||||||
|
return std::string{"["};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::FILL>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::FILL>::type
|
||||||
|
get<ProgressBarOption::FILL>(){
|
||||||
|
return std::string{"="};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::LEAD>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::LEAD>::type
|
||||||
|
get<ProgressBarOption::LEAD>(){
|
||||||
|
return std::string{">"};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::REMAINDER>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::REMAINDER>::type
|
||||||
|
get<ProgressBarOption::REMAINDER>(){
|
||||||
|
return std::string{" "};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::END>{
|
||||||
|
using type = std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::END>::type
|
||||||
|
get<ProgressBarOption::END>(){
|
||||||
|
return std::string{"]"};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>{
|
||||||
|
using type = std::size_t;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>::type
|
||||||
|
get<ProgressBarOption::MAX_POSTFIX_TEXT_LEN>(){
|
||||||
|
return std::size_t{0};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::COMPLETED>{
|
||||||
|
using type = bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::COMPLETED>::type
|
||||||
|
get<ProgressBarOption::COMPLETED>(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>{
|
||||||
|
using type = bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::SHOW_PERCENTAGE>::type
|
||||||
|
get<ProgressBarOption::SHOW_PERCENTAGE>(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>{
|
||||||
|
using type = bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::SHOW_ELAPSED_TIME>::type
|
||||||
|
get<ProgressBarOption::SHOW_ELAPSED_TIME>(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>{
|
||||||
|
using type = bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::SHOW_REMAINING_TIME>::type
|
||||||
|
get<ProgressBarOption::SHOW_REMAINING_TIME>(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::SAVED_START_TIME>{
|
||||||
|
using type = bool;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::SAVED_START_TIME>::type
|
||||||
|
get<ProgressBarOption::SAVED_START_TIME>(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct get_ret_type<ProgressBarOption::FOREGROUND_COLOR>{
|
||||||
|
using type = ::indicators::Color;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> get_ret_type<ProgressBarOption::FOREGROUND_COLOR>::type
|
||||||
|
get<ProgressBarOption::FOREGROUND_COLOR>(){
|
||||||
|
return ::indicators::Color::WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user