diff --git a/README.md b/README.md index 17323ea..477063f 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,17 @@ You can update the progress bar using `bar.tick()` which increments progress by #include int main() { - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with("["); - bar.fill_bar_progress_with("="); - bar.lead_bar_progress_with(">"); - bar.fill_bar_remainder_with(" "); - bar.end_bar_with("]"); - bar.set_postfix_text("Extracting Archive"); - bar.set_foreground_color(indicators::Color::GREEN); + using namespace indicators; + ProgressBar bar{ + option::BarWidth{50}, + option::Start{"["}, + option::Fill{"="}, + option::Lead{">"}, + option::Remainder{" "}, + option::End{"]"}, + option::PostfixText{"Extracting Archive"}; + option::ForegroundColor{Color::GREEN}; + }; // Update bar state while (true) { @@ -106,18 +106,17 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with("["); - bar.fill_bar_progress_with("■"); - bar.lead_bar_progress_with("■"); - bar.fill_bar_remainder_with("-"); - bar.end_bar_with(" ]"); - bar.set_postfix_text("Loading dependency 1/4"); - bar.set_foreground_color(indicators::Color::CYAN); + using namespace indicators; + ProgressBar bar{ + option::BarWidth{50}, + option::Start{"["}, + option::Fill{"■"}, + option::Lead{"■"}, + option::Remainder{"-"}, + option::End{" ]"}, + option::PostfixText{"Loading dependency 1/4"}, + option::ForegroundColor{Color::CYAN} + }; // Update bar state bar.set_progress(10); // 10% done @@ -125,21 +124,21 @@ int main() { // do some work std::this_thread::sleep_for(std::chrono::milliseconds(800)); - bar.set_postfix_text("Loading dependency 2/4"); + bar.set_option(option::PostfixText{"Loading dependency 2/4"}); bar.set_progress(30); // 30% done // do some more work std::this_thread::sleep_for(std::chrono::milliseconds(700)); - bar.set_postfix_text("Loading dependency 3/4"); + bar.set_option(option::PostfixText{"Loading dependency 3/4"}); bar.set_progress(65); // 65% done // do final bit of work std::this_thread::sleep_for(std::chrono::milliseconds(900)); - bar.set_postfix_text("Loaded dependencies!"); + bar.set_option(option::PostfixText{"Loaded dependencies!"}); bar.set_progress(100); // all done @@ -166,21 +165,19 @@ All progress bars and spinners in `indicators` support showing time elapsed and #include int main() { - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with(" ["); - bar.fill_bar_progress_with("█"); - bar.lead_bar_progress_with("█"); - bar.fill_bar_remainder_with("-"); - bar.end_bar_with("]"); - bar.set_prefix_text("Training Gaze Network 👀"); - bar.set_foreground_color(indicators::Color::YELLOW); - - // Show time elapsed and remaining - bar.show_elapsed_time(); - bar.show_remaining_time(); + using namespace indicators; + indicators::ProgressBar bar{ + option::BarWidth{50}, + option::Start{" ["}, + option::Fill{"█"}, + option::Lead{"█"}, + option::Remainder{"-"}, + option::End{"]"}, + option::PrefixText{"Training Gaze Network "}, + option::ForegroundColor{Color::YELLOW} + option::ShowElapsedTime{true}; + option::ShowRemainingTime{true}; + }; // Update bar state while (true) { @@ -214,14 +211,14 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - - indicators::BlockProgressBar bar; - - // Configure the bar - bar.set_bar_width(80); - bar.start_bar_with("["); - bar.end_bar_with("]"); - bar.set_foreground_color(indicators::Color::WHITE); + using namespace indicators; + + BlockProgressBar bar{ + option::BarWidth{80}, + option::Start{"["}, + option::End{"]"}, + option::ForegroundColor{Color::WHITE} + }; // Update bar state auto progress = 0.0f; @@ -257,45 +254,49 @@ Below is an example `MultiProgress` object that manages three `ProgressBar` obje #include int main() { - + using namespace indicators; // Configure first progress bar - indicators::ProgressBar bar1; - bar1.set_bar_width(50); - bar1.start_bar_with("["); - bar1.fill_bar_progress_with("■"); - bar1.lead_bar_progress_with("■"); - bar1.fill_bar_remainder_with(" "); - bar1.end_bar_with(" ]"); - bar1.set_foreground_color(indicators::Color::YELLOW); - bar1.show_elapsed_time(); - bar1.show_remaining_time(); - bar1.set_prefix_text("Progress Bar #1 "); + ProgressBar bar1{ + option::BarWidth{50}, + option::Start{"["}, + option::Fill{"■"}, + option::Lead{"■"}, + option::Remainder{" "}, + option::End{" ]"}, + option::ForegroundColor{Color::YELLOW}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #1 "} + }; // Configure second progress bar - indicators::ProgressBar bar2; - bar2.set_bar_width(50); - bar2.start_bar_with("["); - bar2.fill_bar_progress_with("="); - bar2.lead_bar_progress_with(">"); - bar2.fill_bar_remainder_with(" "); - bar2.end_bar_with(" ]"); - bar2.set_foreground_color(indicators::Color::CYAN); - bar2.show_elapsed_time(); - bar2.show_remaining_time(); - bar2.set_prefix_text("Progress Bar #2 "); + ProgressBar bar1{ + option::BarWidth{50}, + option::Start{"["}, + option::Fill{"="}, + option::Lead{">"}, + option::Remainder{" "}, + option::End{" ]"}, + option::ForegroundColor{Color::CYAN}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #2 "} + }; + // Configure third progress bar - indicators::ProgressBar bar3; - bar3.set_bar_width(50); - bar3.start_bar_with("["); - bar3.fill_bar_progress_with("#"); - bar3.lead_bar_progress_with("#"); - bar3.fill_bar_remainder_with(" "); - bar3.end_bar_with(" ]"); - bar3.set_foreground_color(indicators::Color::RED); - bar3.show_elapsed_time(); - bar3.show_remaining_time(); - bar3.set_prefix_text("Progress Bar #3 "); + indicators::ProgressBar bar3{ + option::BarWidth{50}, + option::Start{"["}, + option::Fill{"#"}, + option::Lead{"#"}, + option::Remainder{" "}, + option::End{" ]"}, + option::ForegroundColor{Color::RED}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #3 "} + }; // Construct MultiProgress object indicators::MultiProgress bars(bar1, bar2, bar3); @@ -359,22 +360,22 @@ ProgressSpinner has a vector of strings: `spinner_states`. At each update, the s #include int main() { - indicators::ProgressSpinner spinner; - - // Configure the spinner - spinner.set_postfix_text("Checking credentials"); - spinner.set_foreground_color(indicators::Color::YELLOW); - spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}); - + using namespace indicators; + indicators::ProgressSpinner spinner{ + option::PostfixText{"Checking credentials"}, + option::ForegroundColor{Color::YELLOW}, + option::SpinnerStates{std::vector{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}} + }; + // Update spinner state auto job = [&spinner]() { while (true) { if (spinner.is_completed()) { - spinner.set_foreground_color(indicators::Color::GREEN); - spinner.set_prefix_text("✔"); - spinner.hide_spinner(); - spinner.hide_percentage(); - spinner.set_postfix_text("Authenticated!"); + spinner.set_option(option::ForegroundColor{Color::GREEN}); + spinner.set_option(option::PrefixText{"✔"}); + spinner.set_option(option::ShowSpinner{false}); + spinner.set_option(option::ShowPercentage{false}); + spinner.set_option(option::PostfixText{"Authenticated!"}); spinner.mark_as_completed(); break; } else diff --git a/demo/demo.cpp b/demo/demo.cpp index 09da63a..084d746 100644 --- a/demo/demo.cpp +++ b/demo/demo.cpp @@ -6,19 +6,18 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - + using namespace indicators; { // // PROGRESS BAR 1 // - indicators::ProgressBar p; - p.set_bar_width(50); - p.start_bar_with("["); - p.fill_bar_progress_with("■"); - p.lead_bar_progress_with("■"); - p.fill_bar_remainder_with(" "); - p.end_bar_with(" ]"); - p.set_foreground_color(indicators::Color::YELLOW); + indicators::ProgressBar p{option::BarWidth{50}, + option::Start{"["}, + option::Fill{"■"}, + option::Lead{"■"}, + option::Remainder{" "}, + option::End{" ]"}, + option::ForegroundColor{indicators::Color::YELLOW}}; std::atomic index{0}; std::vector status_text = {"Rocket.exe is not responding", @@ -35,7 +34,7 @@ int main() { while (true) { if (p.is_completed()) 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); index += 1; std::this_thread::sleep_for(std::chrono::milliseconds(600)); @@ -50,20 +49,20 @@ int main() { // PROGRESS BAR 2 // indicators::ProgressBar p; - p.set_bar_width(40); - p.set_prefix_text("Reading package list... "); - p.start_bar_with(""); - p.fill_bar_progress_with(""); - p.lead_bar_progress_with(""); - p.fill_bar_remainder_with(""); - p.end_bar_with(""); - p.set_foreground_color(indicators::Color::WHITE); - p.hide_percentage(); + p.set_option(option::BarWidth{40}); + p.set_option(option::PrefixText{"Reading package list... "}); + p.set_option(option::Start{""}); + p.set_option(option::Fill{""}); + p.set_option(option::Lead{""}); + p.set_option(option::Remainder{""}); + p.set_option(option::End{""}); + p.set_option(option::ForegroundColor{indicators::Color::WHITE}); + p.set_option(option::ShowPercentage{false}); auto job = [&p]() { 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) - p.set_prefix_text("Reading package list... Done"); + p.set_option(option::PrefixText{"Reading package list... Done"}); p.tick(); if (p.is_completed()) { break; @@ -80,25 +79,25 @@ int main() { // PROGRESS BAR 3 // indicators::ProgressBar p; - p.set_bar_width(50); - p.start_bar_with("["); - p.fill_bar_progress_with("="); - p.lead_bar_progress_with(">"); - p.fill_bar_remainder_with(" "); - p.end_bar_with("]"); - p.set_postfix_text("Getting started"); - p.set_foreground_color(indicators::Color::GREEN); + p.set_option(option::BarWidth{50}); + p.set_option(option::Start{"["}); + p.set_option(option::Fill{"="}); + p.set_option(option::Lead{">"}); + p.set_option(option::Remainder{" "}); + p.set_option(option::End{"]"}); + p.set_option(option::PostfixText{"Getting started"}); + p.set_option(option::ForegroundColor{indicators::Color::GREEN}); auto job = [&p]() { while (true) { auto ticks = p.current(); 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) - p.set_postfix_text("Crying quietly"); + p.set_option(option::PostfixText{"Crying quietly"}); else if (ticks > 80 && ticks < 98) - p.set_postfix_text("Almost there"); + p.set_option(option::PostfixText{"Almost there"}); else if (ticks >= 98) - p.set_postfix_text("Done"); + p.set_option(option::PostfixText{"Done"}); p.tick(); if (p.is_completed()) break; @@ -115,32 +114,31 @@ int main() { // std::vector lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}; indicators::ProgressBar p4; - p4.set_bar_width(40); - p4.start_bar_with(""); - p4.fill_bar_progress_with("⠸"); - p4.lead_bar_progress_with(""); - p4.fill_bar_remainder_with(" "); - p4.end_bar_with(""); - p4.set_foreground_color(indicators::Color::CYAN); - p4.set_postfix_text("Restoring system state"); - p4.hide_percentage(); + p4.set_option(option::BarWidth{40}); + p4.set_option(option::Start{""}); + p4.set_option(option::Fill{"⠸"}); + p4.set_option(option::Lead{""}); + p4.set_option(option::Remainder{" "}); + p4.set_option(option::End{""}); + p4.set_option(option::ForegroundColor{indicators::Color::CYAN}); + p4.set_option(option::PostfixText{"Restoring system state"}); + p4.set_option(option::ShowPercentage{false}); std::atomic index4{0}; auto job4 = [&p4, &index4, &lead_spinner]() { while (true) { - p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } "); - p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]); + p4.set_option(option::PrefixText{"{ " + std::to_string(p4.current()) + "% } "}); + p4.set_option(option::Lead{lead_spinner[index4 % lead_spinner.size()]}); index4 += 1; if (p4.current() + 2 >= 100) { std::cout << std::endl; - p4.set_foreground_color(indicators::Color::RED); - p4.set_prefix_text("{ ERROR }"); - p4.start_bar_with(""); - p4.fill_bar_progress_with(""); - p4.lead_bar_progress_with(""); - p4.fill_bar_remainder_with(""); - p4.end_bar_with(""); - p4.show_percentage(); - p4.set_postfix_text("Failed to restore system"); + p4.set_option(option::ForegroundColor{indicators::Color::RED}); + p4.set_option(option::PrefixText{"{ ERROR }"}); + p4.set_option(option::Start{}); + p4.set_option(option::Fill{}); + p4.set_option(option::Lead{}); + p4.set_option(option::Remainder{}); + p4.set_option(option::ShowPercentage{true}); + p4.set_option(option::PostfixText{"Failed to restore system"}); p4.mark_as_completed(); break; } @@ -157,23 +155,24 @@ int main() { // // GOING BACKWARDS // - indicators::ProgressBar p; - p.set_bar_width(50); - p.start_bar_with("["); - p.fill_bar_progress_with("■"); - p.lead_bar_progress_with("■"); - p.fill_bar_remainder_with("-"); - p.end_bar_with("]"); - p.set_progress(100); - p.set_foreground_color(indicators::Color::WHITE); - p.set_postfix_text("Reverting system restore"); + indicators::ProgressBar p{ + option::BarWidth {50}, + option::Start{"["}, + option::Fill{"■"}, + option::Lead{"■"}, + option::Remainder{"-"}, + option::End{"]"}, + option::ForegroundColor{indicators::Color::WHITE}, + option::PostfixText{"Reverting system restore"} + }; + p.set_progress(100); // TODO backwards as an option? std::atomic progress{100}; auto job = [&p, &progress]() { while (true) { progress -= 1; p.set_progress(progress); if (progress == 0) { - p.set_postfix_text("Revert complete!"); + p.set_option(option::PostfixText{"Revert complete!"}); p.mark_as_completed(); break; } @@ -189,19 +188,21 @@ int main() { // // PROGRESS BAR 5 // - indicators::ProgressSpinner p; - p.set_prefix_text(""); - p.set_postfix_text("Checking credentials"); - p.set_foreground_color(indicators::Color::YELLOW); - p.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}); + indicators::ProgressSpinner p{ + option::PrefixText{""}, + option::PostfixText{"Checking credentials"}, + option::ForegroundColor{indicators::Color::YELLOW}, + option::SpinnerStates{std::vector{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}} + }; + auto job = [&p]() { while (true) { if (p.is_completed()) { - p.set_foreground_color(indicators::Color::GREEN); - p.set_prefix_text("✔"); - p.hide_spinner(); - p.hide_percentage(); - p.set_postfix_text("Authenticated!"); + p.set_option(option::ForegroundColor{indicators::Color::GREEN}); + p.set_option(option::PrefixText{"✔"}); + p.set_option(option::ShowSpinner{false}); + p.set_option(option::ShowPercentage{false}); + p.set_option(option::PostfixText{"Authenticated!"}); p.mark_as_completed(); break; } else @@ -218,70 +219,72 @@ int main() { // // PROGRESS BAR 6 // - indicators::ProgressSpinner p; - p.set_prefix_text(" - "); - p.set_postfix_text("Searching for the Moon"); - p.set_foreground_color(indicators::Color::WHITE); - p.set_spinner_states({"▖", "▘", "▝", "▗"}); - p.hide_percentage(); + indicators::ProgressSpinner p{ + option::PrefixText{" - "}, + option::PostfixText{"Searching for the Moon"}, + option::ForegroundColor{indicators::Color::WHITE}, + option::ShowPercentage{false}, + option::SpinnerStates{std::vector{"▖", "▘", "▝", "▗"}} + }; auto job = [&p]() { while (true) { auto current = p.current(); if (current == 24) { - p.set_prefix_text(" - ✔"); - p.hide_spinner(); + p.set_option(option::PrefixText{" - ✔"}); + p.set_option(option::ShowSpinner{false}); } else if (current == 25) { std::cout << std::endl; - p.show_spinner(); - p.set_prefix_text(" - "); - p.set_postfix_text("Contacting Kerbal headquarters"); + p.set_option(option::ShowSpinner{true}); + p.set_option(option::PrefixText{" - "}); + p.set_option(option::PostfixText{"Contacting Kerbal headquarters"}); } else if (current == 49) { - p.set_prefix_text(" - ✔"); - p.hide_spinner(); + p.set_option(option::PrefixText{" - ✔"}); + p.set_option(option::ShowSpinner{false}); } else if (current == 50) { std::cout << std::endl; - p.show_spinner(); - p.set_prefix_text(" - "); - p.set_postfix_text("Designing spaceship"); + p.set_option(option::ShowSpinner{true}); + p.set_option(option::PrefixText{" - "}); + p.set_option(option::PostfixText{"Designing spaceship"}); } else if (current == 74) { - p.set_prefix_text(" - ✔"); - p.hide_spinner(); + p.set_option(option::PrefixText{" - ✔"}); + p.set_option(option::ShowSpinner{false}); } else if (current == 75) { std::cout << std::endl; - p.show_spinner(); - p.set_prefix_text(" - "); - p.set_postfix_text("Launching rocket"); + p.set_option(option::ShowSpinner{true}); + p.set_option(option::PrefixText{" - "}); + p.set_option(option::PostfixText{"Launching rocket"}); } else if (current == 95) { - p.set_prefix_text(" - ✔"); - p.hide_spinner(); + p.set_option(option::PrefixText{" - ✔"}); + p.set_option(option::ShowSpinner{false}); } else if (current == 99) { std::cout << std::endl; // // NESTED PROGRESS BAR // - indicators::ProgressBar p2; - p2.set_bar_width(30); - p2.set_prefix_text(" - "); - p2.start_bar_with("🌎"); - p2.fill_bar_progress_with("·"); - p2.lead_bar_progress_with("🚀"); - p2.fill_bar_remainder_with(" "); - p2.end_bar_with("🌑"); - p2.set_postfix_text("Achieved low-Earth orbit"); - p2.set_foreground_color(indicators::Color::WHITE); + indicators::ProgressBar p2{ + option::BarWidth{30}, + option::PrefixText{" - "}, + option::Start{"🌎"}, + option::Fill{"·"}, + option::Lead{"🚀"}, + option::Remainder{" "}, + option::End{"🌑"}, + option::PostfixText{"Achieved low-Earth orbit"}, + option::ForegroundColor{indicators::Color::WHITE} + }; std::vector ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"}; std::atomic ship_trail_index{0}; auto job2 = [&p2, &ship_trail_index, &ship_trail]() { while (true) { auto ticks = p2.current(); 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) - p2.set_postfix_text("Transferred to Lunar lander"); + p2.set_option(option::PostfixText{"Transferred to Lunar lander"}); else if (ticks > 80 && ticks < 98) - p2.set_postfix_text("Almost there"); + p2.set_option(option::PostfixText{"Almost there"}); else if (ticks >= 98) - p2.set_postfix_text("Landed on the Moon"); + p2.set_option(option::PostfixText{"Landed on the Moon"}); p2.tick(); ship_trail_index += 1; if (p2.is_completed()) @@ -291,7 +294,7 @@ int main() { }; std::thread thread2(job2); thread2.join(); - p.set_postfix_text("Mission successful!"); + p.set_option(indicators::option::PostfixText{"Mission successful!"}); p.mark_as_completed(); break; } diff --git a/include/indicators/block_progress_bar.hpp b/include/indicators/block_progress_bar.hpp index 1f0a05d..f44beca 100644 --- a/include/indicators/block_progress_bar.hpp +++ b/include/indicators/block_progress_bar.hpp @@ -37,56 +37,74 @@ SOFTWARE. #include #include #include +#include #include +#include "setting.hpp" namespace indicators { class BlockProgressBar { + using Settings = std::tuple< + option::ForegroundColor, + option::BarWidth, + option::Start, + option::End, + option::PrefixText, + option::PostfixText, + option::ShowPercentage, + option::ShowElapsedTime, + option::ShowRemainingTime, + option::Completed, + option::SavedStartTime, + option::MaxPostfixTextLen>; public: - void set_foreground_color(Color color) { - std::lock_guard lock{_mutex}; - _foreground_color = color; + template ::type...>::value, void*>::type = nullptr> + explicit BlockProgressBar(Args&&... args) : settings_( + details::get(option::ForegroundColor{Color::WHITE}, std::forward(args)...), + details::get(option::BarWidth{100}, std::forward(args)...), + details::get(option::Start{"["}, std::forward(args)...), + details::get(option::End{"]"}, std::forward(args)...), + details::get(option::PrefixText{""}, std::forward(args)...), + details::get(option::PostfixText{""}, std::forward(args)...), + details::get(option::ShowPercentage{true}, std::forward(args)...), + details::get(option::ShowElapsedTime{false}, std::forward(args)...), + details::get(option::ShowRemainingTime{false}, std::forward(args)...), + details::get(option::Completed{false}, std::forward(args)...), + details::get(option::SavedStartTime{false}, std::forward(args)...), + details::get(option::MaxPostfixTextLen{0}, std::forward(args)...) + ) {} + + template + void set_option(details::Setting&& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; } - void set_bar_width(size_t bar_width) { - std::lock_guard lock{_mutex}; - _bar_width = bar_width; + template + void set_option(const details::Setting& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = setting.value; } - void start_bar_with(const std::string &start) { - std::lock_guard lock{_mutex}; - _start = start; + void set_option(const details::Setting& setting){ + std::lock_guard lock(_mutex); + get_value() = setting.value; + if(setting.value.length() > get_value()){ + get_value() = setting.value.length(); + } } - void end_bar_with(const std::string &end) { - std::lock_guard lock{_mutex}; - _end = end; + void set_option(details::Setting&& setting){ + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; + auto& new_value = get_value(); + if(new_value.length() > get_value()){ + get_value() = new_value.length(); + } } - void set_prefix_text(const std::string &text) { - std::lock_guard lock{_mutex}; - _prefix_text = text; - } - - void set_postfix_text(const std::string &text) { - std::lock_guard 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 lock{_mutex}; @@ -110,44 +128,47 @@ public: return std::min(static_cast(_progress), size_t(100)); } - bool is_completed() const { return _completed; } + bool is_completed() const { return get_value(); } void mark_as_completed() { - _completed = true; + get_value() = true; _print_progress(); } private: + + template + auto get_value() -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + template + auto get_value() const -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + Settings settings_; float _progress{0.0}; - size_t _bar_width{100}; - std::string _prefix_text{""}; - std::string _start{"["}; - std::string _end{"]"}; - std::string _postfix_text{""}; - std::atomic _max_postfix_text_length{0}; - std::atomic _completed{false}; - std::atomic _show_percentage{true}; - std::atomic _show_elapsed_time{false}; - std::atomic _show_remaining_time{false}; - std::atomic _saved_start_time{false}; std::chrono::time_point _start_time_point; std::mutex _mutex; - Color _foreground_color{indicators::Color::WHITE}; template friend class MultiProgress; std::atomic _multi_progress_mode{false}; void _save_start_time() { - if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) { + auto& show_elapsed_time = get_value(); + auto& saved_start_time = get_value(); + auto& show_remaining_time = get_value(); + if ((show_elapsed_time || show_remaining_time) && !saved_start_time) { _start_time_point = std::chrono::high_resolution_clock::now(); - _saved_start_time = true; + saved_start_time = true; } } void _print_progress(bool from_multi_progress = false) { if (_multi_progress_mode && !from_multi_progress) { if (_progress > 100.0) { - _completed = true; + get_value() = true; } return; } @@ -156,25 +177,25 @@ private: auto elapsed = std::chrono::duration_cast(now - _start_time_point); std::cout << termcolor::bold; - details::set_stream_color(std::cout, _foreground_color); - std::cout << _prefix_text; - std::cout << _start; + details::set_stream_color(std::cout, get_value()); + std::cout << get_value(); + std::cout << get_value(); - details::BlockProgressScaleWriter writer{std::cout, _bar_width}; + details::BlockProgressScaleWriter writer{std::cout, get_value()}; writer.write(_progress); - std::cout << _end; - if (_show_percentage) { + std::cout << get_value(); + if (get_value()) { std::cout << " " << std::min(static_cast(_progress), size_t(100)) << "%"; } - if (_show_elapsed_time) { + if (get_value()) { std::cout << " ["; details::write_duration(std::cout, elapsed); } - if (_show_remaining_time) { - if (_show_elapsed_time) + if (get_value()) { + if (get_value()) std::cout << "<"; else std::cout << " ["; @@ -184,18 +205,18 @@ private: details::write_duration(std::cout, remaining); std::cout << "]"; } else { - if (_show_elapsed_time) + if (get_value()) std::cout << "]"; } - if (_max_postfix_text_length == 0) - _max_postfix_text_length = 10; - std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; + if (get_value() == 0) + get_value() = 10; + std::cout << " " << get_value() << std::string(get_value(), ' ') << "\r"; std::cout.flush(); if (_progress > 100.0) { - _completed = true; + get_value() = true; } - if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress + if (get_value() && !from_multi_progress) // Don't std::endl if calling from MultiProgress std::cout << termcolor::reset << std::endl; } }; diff --git a/include/indicators/progress_bar.hpp b/include/indicators/progress_bar.hpp index c2b27f9..887165d 100644 --- a/include/indicators/progress_bar.hpp +++ b/include/indicators/progress_bar.hpp @@ -34,80 +34,95 @@ SOFTWARE. #include #include #include +#include +#include #include #include #include #include #include +#include +#include namespace indicators { class ProgressBar { + using Settings = std::tuple< + option::BarWidth, + option::PrefixText, + option::PostfixText, + option::Start, + option::End, + option::Fill, + option::Lead, + option::Remainder, + option::MaxPostfixTextLen, + option::Completed, + option::ShowPercentage, + option::ShowElapsedTime, + option::ShowRemainingTime, + option::SavedStartTime, + option::ForegroundColor + >; public: - void set_foreground_color(Color color) { - std::lock_guard lock{_mutex}; - _foreground_color = color; + template ::type...>::value, void*>::type = nullptr> + explicit ProgressBar(Args&&... args) : + settings_( + details::get(option::BarWidth{100}, std::forward(args)...), + details::get(option::PrefixText{}, std::forward(args)...), + details::get(option::PostfixText{}, std::forward(args)...), + details::get(option::Start{"["}, std::forward(args)...), + details::get(option::End{"]"}, std::forward(args)...), + details::get(option::Fill{"="}, std::forward(args)...), + details::get(option::Lead{">"}, std::forward(args)...), + details::get(option::Remainder{" "}, std::forward(args)...), + details::get(option::MaxPostfixTextLen{0}, std::forward(args)...), + details::get(option::Completed{false}, std::forward(args)...), + details::get(option::ShowPercentage{false} ,std::forward(args)...), + details::get(option::ShowElapsedTime{false}, std::forward(args)...), + details::get(option::ShowRemainingTime{false}, std::forward(args)...), + details::get(option::SavedStartTime{false}, std::forward(args)...), + details::get(option::ForegroundColor{Color::WHITE}, std::forward(args)...) + ) + {} + + template + void set_option(details::Setting&& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; } - void set_bar_width(size_t bar_width) { - std::lock_guard lock{_mutex}; - _bar_width = bar_width; + template + void set_option(const details::Setting& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = setting.value; } - void start_bar_with(const std::string &start) { - std::lock_guard lock{_mutex}; - _start = start; - } - - void fill_bar_progress_with(const std::string &fill) { - std::lock_guard lock{_mutex}; - _fill = fill; - } - - void lead_bar_progress_with(const std::string &lead) { - std::lock_guard lock{_mutex}; - _lead = lead; - } - - void fill_bar_remainder_with(const std::string &remainder) { - std::lock_guard lock{_mutex}; - _remainder = remainder; - } - - void end_bar_with(const std::string &end) { - std::lock_guard lock{_mutex}; - _end = end; - } - - void set_prefix_text(const std::string &text) { - std::lock_guard lock{_mutex}; - _prefix_text = text; - } - - void set_postfix_text(const std::string &text) { - std::lock_guard 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 lock{_mutex}; - _progress = value; + void set_option(const details::Setting& setting){ + std::lock_guard lock(_mutex); + get_value() = setting.value; + if(setting.value.length() > get_value()){ + get_value() = setting.value.length(); } + } + + void set_option(details::Setting&& setting){ + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; + auto& new_value = get_value(); + if(new_value.length() > get_value()){ + get_value() = new_value.length(); + } + } + + void set_progress(float newProgress){ + { + std::lock_guard lck(_mutex); + _progress = newProgress; + } + _save_start_time(); _print_progress(); } @@ -126,78 +141,81 @@ public: return std::min(static_cast(_progress), size_t(100)); } - bool is_completed() const { return _completed; } + bool is_completed() const { return get_value(); } void mark_as_completed() { - _completed = true; + get_value() = true; _print_progress(); } private: - float _progress{0.0}; - size_t _bar_width{100}; - std::string _prefix_text{""}; - std::string _start{"["}; - std::string _fill{"="}; - std::string _lead{">"}; - std::string _remainder{" "}; - std::string _end{"]"}; - std::string _postfix_text{""}; - std::atomic _max_postfix_text_length{0}; - std::atomic _completed{false}; - std::atomic _show_percentage{true}; - std::atomic _show_elapsed_time{false}; - std::atomic _show_remaining_time{false}; - std::atomic _saved_start_time{false}; + + template + auto get_value() -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + template + auto get_value() const -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } + + float _progress{0}; + Settings settings_; std::chrono::nanoseconds _elapsed; std::chrono::time_point _start_time_point; std::mutex _mutex; - Color _foreground_color{indicators::Color::WHITE}; template friend class MultiProgress; std::atomic _multi_progress_mode{false}; void _save_start_time() { - if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) { + auto& show_elapsed_time = get_value(); + auto& saved_start_time = get_value(); + auto& show_remaining_time = get_value(); + if ((show_elapsed_time || show_remaining_time) && !saved_start_time) { _start_time_point = std::chrono::high_resolution_clock::now(); - _saved_start_time = true; + saved_start_time = true; } } void _print_progress(bool from_multi_progress = false) { if (_multi_progress_mode && !from_multi_progress) { if (_progress > 100.0) { - _completed = true; + get_value() = true; } return; } std::lock_guard lock{_mutex}; auto now = std::chrono::high_resolution_clock::now(); - if (!_completed) + if (!get_value()) _elapsed = std::chrono::duration_cast(now - _start_time_point); std::cout << termcolor::bold; - details::set_stream_color(std::cout, _foreground_color); - std::cout << _prefix_text; + details::set_stream_color(std::cout, get_value()); + std::cout << get_value(); - std::cout << _start; + std::cout << get_value(); - details::ProgressScaleWriter writer{std::cout, _bar_width, _fill, _lead, _remainder}; + details::ProgressScaleWriter writer{std::cout, get_value(), + get_value(), + get_value(), + get_value()}; writer.write(_progress); - std::cout << _end; + std::cout << get_value(); - if (_show_percentage) { + if (get_value()) { std::cout << " " << std::min(static_cast(_progress), size_t(100)) << "%"; } - if (_show_elapsed_time) { + if (get_value()) { std::cout << " ["; details::write_duration(std::cout, _elapsed); } - if (_show_remaining_time) { - if (_show_elapsed_time) + if (get_value()) { + if (get_value()) std::cout << "<"; else std::cout << " ["; @@ -207,18 +225,18 @@ private: details::write_duration(std::cout, remaining); std::cout << "]"; } else { - if (_show_elapsed_time) + if (get_value()) std::cout << "]"; } - if (_max_postfix_text_length == 0) - _max_postfix_text_length = 10; - std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; + if (get_value() == 0) + get_value() = 10; + std::cout << " " << get_value() << std::string(get_value(), ' ') << "\r"; std::cout.flush(); if (_progress > 100.0) { - _completed = true; + get_value() = true; } - if (_completed && !from_multi_progress) // Don't std::endl if calling from MultiProgress + if (get_value() && !from_multi_progress) // Don't std::endl if calling from MultiProgress std::cout << termcolor::reset << std::endl; } }; diff --git a/include/indicators/progress_spinner.hpp b/include/indicators/progress_spinner.hpp index c8f1c5e..ee98b59 100644 --- a/include/indicators/progress_spinner.hpp +++ b/include/indicators/progress_spinner.hpp @@ -34,49 +34,77 @@ SOFTWARE. #include #include #include +#include #include #include #include #include +#include #include #include namespace indicators { class ProgressSpinner { + using Settings = std::tuple< + option::ForegroundColor, + option::PrefixText, + option::PostfixText, + option::ShowPercentage, + option::ShowElapsedTime, + option::ShowRemainingTime, + option::ShowSpinner, + option::SavedStartTime, + option::Completed, + option::MaxPostfixTextLen, + option::SpinnerStates + >; public: - void set_foreground_color(Color color) { - std::lock_guard lock{_mutex}; - _foreground_color = color; + template ::type...>::value, void*>::type = nullptr> + explicit ProgressSpinner(Args&&... args) : settings_( + details::get(option::ForegroundColor{Color::WHITE}, std::forward(args)...), + details::get(option::PrefixText{}, std::forward(args)...), + details::get(option::PostfixText{}, std::forward(args)...), + details::get(option::ShowPercentage{true}, std::forward(args)...), + details::get(option::ShowElapsedTime{false}, std::forward(args)...), + details::get(option::ShowRemainingTime{false}, std::forward(args)...), + details::get(option::ShowSpinner{true}, std::forward(args)...), + details::get(option::SavedStartTime{false}, std::forward(args)...), + details::get(option::Completed{false}, std::forward(args)...), + details::get(option::MaxPostfixTextLen{0}, std::forward(args)...), + details::get(option::SpinnerStates{std::vector{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}}, std::forward(args)...) + ){} + + template + void set_option(details::Setting&& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; } - void set_prefix_text(const std::string &text) { - std::lock_guard lock{_mutex}; - _prefix_text = text; + template + void set_option(const details::Setting& setting){ + static_assert(!std::is_same(std::declval()))>::type>::value, "Setting has wrong type!"); + std::lock_guard lock(_mutex); + get_value() = setting.value; } - void set_postfix_text(const std::string &text) { - std::lock_guard lock{_mutex}; - _postfix_text = text; - if (_postfix_text.length() > _max_postfix_text_length) - _max_postfix_text_length = _postfix_text.length(); + void set_option(const details::Setting& setting){ + std::lock_guard lock(_mutex); + get_value() = setting.value; + if(setting.value.length() > get_value()){ + get_value() = setting.value.length(); + } } - void show_percentage() { _show_percentage = true; } - - void hide_percentage() { _show_percentage = false; } - - void show_elapsed_time() { _show_elapsed_time = true; } - - void hide_elapsed_time() { _show_elapsed_time = false; } - - void show_remaining_time() { _show_remaining_time = true; } - - void hide_remaining_time() { _show_remaining_time = false; } - - void show_spinner() { _show_spinner = true; } - - void hide_spinner() { _show_spinner = false; } + void set_option(details::Setting&& setting){ + std::lock_guard lock(_mutex); + get_value() = std::move(setting).value; + auto& new_value = get_value(); + if(new_value.length() > get_value()){ + get_value() = new_value.length(); + } + } void set_progress(float value) { { @@ -101,39 +129,37 @@ public: return std::min(static_cast(_progress), size_t(100)); } - bool is_completed() const { return _completed; } + bool is_completed() const { return get_value(); } void mark_as_completed() { - _completed = true; + get_value() = true; _print_progress(); } - void set_spinner_states(const std::vector &states) { - std::lock_guard lock{_mutex}; - _states = states; +private: + Settings settings_; + float _progress{0.0}; + size_t _index{0}; + std::chrono::time_point _start_time_point; + std::mutex _mutex; + + template + auto get_value() -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; } -private: - float _progress{0.0}; - std::string _prefix_text{""}; - size_t _index{0}; - std::vector _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}; - std::string _postfix_text{""}; - std::atomic _max_postfix_text_length{0}; - std::atomic _completed{false}; - std::atomic _show_percentage{true}; - std::atomic _show_elapsed_time{false}; - std::atomic _show_remaining_time{false}; - std::atomic _saved_start_time{false}; - std::chrono::time_point _start_time_point; - std::atomic _show_spinner{true}; - std::mutex _mutex; - Color _foreground_color; + template + auto get_value() const -> decltype((details::get_value(std::declval()).value)) { + return details::get_value(settings_).value; + } void _save_start_time() { - if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) { + auto& show_elapsed_time = get_value(); + auto& show_remaining_time = get_value(); + auto& saved_start_time = get_value(); + if ((show_elapsed_time || show_remaining_time) && !saved_start_time) { _start_time_point = std::chrono::high_resolution_clock::now(); - _saved_start_time = true; + saved_start_time = true; } } @@ -143,21 +169,21 @@ private: auto elapsed = std::chrono::duration_cast(now - _start_time_point); std::cout << termcolor::bold; - details::set_stream_color(std::cout, _foreground_color); - std::cout << _prefix_text; - if (_show_spinner) - std::cout << _states[_index % _states.size()]; - if (_show_percentage) { + details::set_stream_color(std::cout, get_value()); + std::cout << get_value(); + if (get_value()) + std::cout << get_value()[_index % get_value().size()]; + if (get_value()) { std::cout << " " << std::min(static_cast(_progress), size_t(100)) << "%"; } - if (_show_elapsed_time) { + if (get_value()) { std::cout << " ["; details::write_duration(std::cout, elapsed); } - if (_show_remaining_time) { - if (_show_elapsed_time) + if (get_value()) { + if (get_value()) std::cout << "<"; else std::cout << " ["; @@ -167,19 +193,19 @@ private: details::write_duration(std::cout, remaining); std::cout << "]"; } else { - if (_show_elapsed_time) + if (get_value()) std::cout << "]"; } - if (_max_postfix_text_length == 0) - _max_postfix_text_length = 10; - std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r"; + if (get_value() == 0) + get_value() = 10; + std::cout << " " << get_value() << std::string(get_value(), ' ') << "\r"; std::cout.flush(); _index += 1; if (_progress > 100.0) { - _completed = true; + get_value() = true; } - if (_completed) + if (get_value()) std::cout << termcolor::reset << std::endl; } }; diff --git a/include/indicators/setting.hpp b/include/indicators/setting.hpp new file mode 100644 index 0000000..535e666 --- /dev/null +++ b/include/indicators/setting.hpp @@ -0,0 +1,222 @@ +/* +Activity Indicators for Modern C++ +https://github.com/p-ranav/indicators + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2019 Dawid Pilarski . + +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 +#include +#include +#include +#include + + +namespace indicators{ + +namespace details{ + +template +struct if_else; + +template<> +struct if_else{ + using type = std::true_type; +}; + +template<> +struct if_else{ + using type = std::false_type ; +}; + +template +struct if_else_type; + +template +struct if_else_type{ + using type = True; +}; + +template +struct if_else_type{ + using type = False; +}; + +template +struct conjuction; + +template <> +struct conjuction<> : std::true_type {}; + +template +struct conjuction : if_else_type>::type {}; + +template +struct disjunction; + +template <> +struct disjunction<> : std::false_type {}; + +template +struct disjunction : if_else_type>::type {}; + +enum class ProgressBarOption{ + bar_width=0, + prefix_text, + postfix_text, + start, + end, + fill, + lead, + remainder, + max_postfix_text_len, + completed, + show_percentage, + show_elapsed_time, + show_remaining_time, + saved_start_time, + foreground_color, + spinner_show, + spinner_states +}; + +template +struct Setting{ + template ::value>::type> + explicit Setting(Args&&... args) : value(std::forward(args)...){} + Setting(const Setting&) = default; + Setting(Setting&&) = default; + + static constexpr auto id = Id; + using type = T; + + T value{}; +}; + +template +struct is_setting : std::false_type{}; + +template +struct is_setting> : std::true_type{}; + +template +struct are_settings : if_else...>::value>::type {}; + +template <> +struct are_settings<> : std::true_type{}; + +template +struct is_setting_from_tuple; + +template +struct is_setting_from_tuple> : std::true_type {}; + +template +struct is_setting_from_tuple> : + if_else...>::value>::type {}; + +template +struct are_settings_from_tuple : if_else...>::value>::type {}; + + +template +struct always_true{ + static constexpr auto value = true; +}; + +template +Default&& get_impl(Default&& def){ + return std::forward(def); +} + +template +auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if< + (std::decay::type::id == Id), + decltype(std::forward(first))> + ::type{ + return std::forward(first); +} + +template +auto get_impl(Default&& def, T&& first, Args&&... tail) -> typename std::enable_if< + (std::decay::type::id != Id), + decltype(get_impl(std::forward(def), std::forward(tail)...))>::type{ + return get_impl(std::forward(def), std::forward(tail)...); +} + +template ::value, void>::type> +auto get(Default&& def, Args&&... args) -> decltype(details::get_impl(std::forward(def), std::forward(args)...)){ + return details::get_impl(std::forward(def), std::forward(args)...); +} + +template +using StringSetting = Setting; + +template +using IntegerSetting = Setting; + +template +using BooleanSetting = Setting; + +template +struct option_idx; + +template +struct option_idx, counter> : if_else_type<(Id == T::id), + std::integral_constant, + option_idx, counter+1>>::type{}; + +template +struct option_idx, counter>{ + static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found"); +}; + +template +auto get_value(Settings&& settings) -> decltype((std::get::type>::value>(std::declval()))){ + return std::get::type>::value>(std::forward(settings)); +} + +} + + +namespace option{ + using BarWidth = details::IntegerSetting; + using PrefixText = details::StringSetting; + using PostfixText = details::StringSetting; + using Start = details::StringSetting; + using End = details::StringSetting; + using Fill = details::StringSetting; + using Lead = details::StringSetting; + using Remainder = details::StringSetting; + using MaxPostfixTextLen = details::IntegerSetting; + using Completed = details::BooleanSetting; + using ShowPercentage = details::BooleanSetting; + using ShowElapsedTime = details::BooleanSetting; + using ShowRemainingTime = details::BooleanSetting; + using SavedStartTime = details::BooleanSetting; + using ForegroundColor = details::Setting; + using ShowSpinner = details::BooleanSetting; + using SpinnerStates = details::Setting, details::ProgressBarOption::spinner_states>; +} +} \ No newline at end of file diff --git a/samples/block_progress_bar.cpp b/samples/block_progress_bar.cpp index d0cbcfd..2d6b1fd 100644 --- a/samples/block_progress_bar.cpp +++ b/samples/block_progress_bar.cpp @@ -7,13 +7,9 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - indicators::BlockProgressBar bar; - - // Configure the bar - bar.set_bar_width(80); - bar.start_bar_with("["); - bar.end_bar_with("]"); - bar.set_foreground_color(indicators::Color::WHITE); + indicators::BlockProgressBar bar{ + indicators::option::BarWidth {80} + }; // Update bar state auto progress = 0.0f; diff --git a/samples/multi_block_progress_bar.cpp b/samples/multi_block_progress_bar.cpp index de53051..49e7521 100644 --- a/samples/multi_block_progress_bar.cpp +++ b/samples/multi_block_progress_bar.cpp @@ -2,27 +2,30 @@ #include int main() { + using namespace indicators; + BlockProgressBar bar1{ + option::BarWidth{50}, + option::ForegroundColor{Color::YELLOW}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #1 "} + }; - indicators::BlockProgressBar bar1; - bar1.set_bar_width(50); - bar1.set_foreground_color(indicators::Color::YELLOW); - bar1.show_elapsed_time(); - bar1.show_remaining_time(); - bar1.set_prefix_text("Progress Bar #1 "); + BlockProgressBar bar2{ + option::BarWidth{50}, + option::ForegroundColor{Color::CYAN}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #2 "} + }; - indicators::BlockProgressBar bar2; - bar2.set_bar_width(50); - bar2.set_foreground_color(indicators::Color::CYAN); - bar2.show_elapsed_time(); - bar2.show_remaining_time(); - bar2.set_prefix_text("Progress Bar #2 "); - - indicators::BlockProgressBar bar3; - bar3.set_bar_width(50); - bar3.set_foreground_color(indicators::Color::RED); - bar3.show_elapsed_time(); - bar3.show_remaining_time(); - bar3.set_prefix_text("Progress Bar #3 "); + BlockProgressBar bar3{ + option::BarWidth{50}, + option::ForegroundColor{Color::RED}, + option::ShowElapsedTime{true}, + option::ShowRemainingTime{true}, + option::PrefixText{"Progress Bar #3 "} + }; indicators::MultiProgress bars(bar1, bar2, bar3); diff --git a/samples/multi_progress_bar.cpp b/samples/multi_progress_bar.cpp index 78360ec..10f05d6 100644 --- a/samples/multi_progress_bar.cpp +++ b/samples/multi_progress_bar.cpp @@ -3,41 +3,44 @@ int main() { - indicators::ProgressBar bar1; - bar1.set_bar_width(50); - bar1.start_bar_with("["); - bar1.fill_bar_progress_with("■"); - bar1.lead_bar_progress_with("■"); - bar1.fill_bar_remainder_with(" "); - bar1.end_bar_with(" ]"); - bar1.set_foreground_color(indicators::Color::YELLOW); - bar1.show_elapsed_time(); - bar1.show_remaining_time(); - bar1.set_prefix_text("Progress Bar #1 "); + indicators::ProgressBar bar1{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"■"}, + indicators::option::Lead{"■"}, + indicators::option::Remainder{" "}, + indicators::option::End{" ]"}, + indicators::option::ForegroundColor{indicators::Color::YELLOW}, + indicators::option::ShowElapsedTime{true}, + indicators::option::ShowRemainingTime{true}, + indicators::option::PrefixText{"Progress Bar #1 "} + }; - indicators::ProgressBar bar2; - bar2.set_bar_width(50); - bar2.start_bar_with("["); - bar2.fill_bar_progress_with("="); - bar2.lead_bar_progress_with(">"); - bar2.fill_bar_remainder_with(" "); - bar2.end_bar_with(" ]"); - bar2.set_foreground_color(indicators::Color::CYAN); - bar2.show_elapsed_time(); - bar2.show_remaining_time(); - bar2.set_prefix_text("Progress Bar #2 "); + indicators::ProgressBar bar2{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"="}, + indicators::option::Lead{">"}, + indicators::option::Remainder{" "}, + indicators::option::End{" ]"}, + indicators::option::ForegroundColor{indicators::Color::CYAN}, + indicators::option::ShowElapsedTime{true}, + indicators::option::ShowRemainingTime{true}, + indicators::option::PrefixText{"Progress Bar #2 "} + }; - indicators::ProgressBar bar3; - bar3.set_bar_width(50); - bar3.start_bar_with("["); - bar3.fill_bar_progress_with("#"); - bar3.lead_bar_progress_with("#"); - bar3.fill_bar_remainder_with(" "); - bar3.end_bar_with(" ]"); - bar3.set_foreground_color(indicators::Color::RED); - bar3.show_elapsed_time(); - bar3.show_remaining_time(); - bar3.set_prefix_text("Progress Bar #3 "); + indicators::ProgressBar bar3{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"#"}, + indicators::option::Lead{"#"}, + indicators::option::Remainder{" "}, + indicators::option::End{" ]"}, + indicators::option::ForegroundColor{indicators::Color::CYAN}, + indicators::option::ShowElapsedTime{true}, + indicators::option::ShowRemainingTime{true}, + indicators::option::PrefixText{"Progress Bar #3 "} + }; indicators::MultiProgress bars(bar1, bar2, bar3); diff --git a/samples/multi_threaded_bar.cpp b/samples/multi_threaded_bar.cpp index 457c479..ed91600 100644 --- a/samples/multi_threaded_bar.cpp +++ b/samples/multi_threaded_bar.cpp @@ -3,14 +3,15 @@ int main() { - indicators::ProgressBar bar; - bar.set_bar_width(50); - bar.start_bar_with("["); - bar.fill_bar_progress_with("■"); - bar.lead_bar_progress_with("■"); - bar.fill_bar_remainder_with("-"); - bar.end_bar_with("]"); - bar.set_foreground_color(indicators::Color::YELLOW); + indicators::ProgressBar bar{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"■"}, + indicators::option::Lead{"■"}, + indicators::option::Remainder{"-"}, + indicators::option::End{" ]"}, + indicators::option::ForegroundColor{indicators::Color::YELLOW}, + }; // As configured, the bar will look like this: // @@ -39,7 +40,7 @@ int main() { if (bar.is_completed()) { 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(); index += 1; std::this_thread::sleep_for(std::chrono::milliseconds(200)); diff --git a/samples/progress_bar_set_progress.cpp b/samples/progress_bar_set_progress.cpp index ec042bc..aec9d75 100644 --- a/samples/progress_bar_set_progress.cpp +++ b/samples/progress_bar_set_progress.cpp @@ -7,17 +7,16 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with("["); - bar.fill_bar_progress_with("■"); - bar.lead_bar_progress_with("■"); - bar.fill_bar_remainder_with("-"); - bar.end_bar_with(" ]"); - bar.set_postfix_text("Loading dependency 1/4"); - bar.set_foreground_color(indicators::Color::CYAN); + indicators::ProgressBar bar{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"■"}, + indicators::option::Lead{"■"}, + indicators::option::Remainder{"-"}, + indicators::option::End{" ]"}, + indicators::option::PostfixText{"Loading dependency 1/4"}, + indicators::option::ForegroundColor{indicators::Color::CYAN}, + }; // Update bar state bar.set_progress(10); // 10% done @@ -25,21 +24,21 @@ int main() { // do some work 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 // do some more work 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 // do final bit of work 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 diff --git a/samples/progress_bar_tick.cpp b/samples/progress_bar_tick.cpp index 5d8e594..8c845b3 100644 --- a/samples/progress_bar_tick.cpp +++ b/samples/progress_bar_tick.cpp @@ -3,17 +3,16 @@ #include int main() { - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with("["); - bar.fill_bar_progress_with("="); - bar.lead_bar_progress_with(">"); - bar.fill_bar_remainder_with(" "); - bar.end_bar_with("]"); - bar.set_postfix_text("Getting started"); - bar.set_foreground_color(indicators::Color::GREEN); + indicators::ProgressBar bar{ + indicators::option::BarWidth{50}, + indicators::option::Start{"["}, + indicators::option::Fill{"="}, + indicators::option::Lead{">"}, + indicators::option::Remainder{" "}, + indicators::option::End{" ]"}, + indicators::option::PostfixText{"Getting started"}, + indicators::option::ForegroundColor{indicators::Color::GREEN}, + }; // Update bar state while (true) { diff --git a/samples/progress_spinner.cpp b/samples/progress_spinner.cpp index 37de709..5db4057 100644 --- a/samples/progress_spinner.cpp +++ b/samples/progress_spinner.cpp @@ -5,22 +5,21 @@ int main() { // Hide cursor std::cout << "\e[?25l"; - indicators::ProgressSpinner spinner; - - // Configure the spinner - spinner.set_postfix_text("Checking credentials"); - spinner.set_foreground_color(indicators::Color::YELLOW); - spinner.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}); + indicators::ProgressSpinner spinner{ + indicators::option::PostfixText{"Checking credentials"}, + indicators::option::ForegroundColor{indicators::Color::YELLOW}, + indicators::option::SpinnerStates{std::vector{"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"}}, + }; // Update spinner state auto job = [&spinner]() { while (true) { if (spinner.is_completed()) { - spinner.set_foreground_color(indicators::Color::GREEN); - spinner.set_prefix_text("✔"); - spinner.hide_spinner(); - spinner.hide_percentage(); - spinner.set_postfix_text("Authenticated!"); + spinner.set_option(indicators::option::ForegroundColor{indicators::Color::GREEN}); + spinner.set_option(indicators::option::PrefixText{"✔"}); + spinner.set_option(indicators::option::ShowSpinner{false}); + spinner.set_option(indicators::option::ShowPercentage{false}); + spinner.set_option(indicators::option::PostfixText{"Authenticated!"}); spinner.mark_as_completed(); break; } else diff --git a/samples/time_meter.cpp b/samples/time_meter.cpp index 23e41cb..04542be 100644 --- a/samples/time_meter.cpp +++ b/samples/time_meter.cpp @@ -3,21 +3,18 @@ #include int main() { - indicators::ProgressBar bar; - - // Configure the bar - bar.set_bar_width(50); - bar.start_bar_with(" ["); - bar.fill_bar_progress_with("█"); - bar.lead_bar_progress_with("█"); - bar.fill_bar_remainder_with("-"); - bar.end_bar_with("]"); - bar.set_prefix_text("Training Gaze Network 👀"); - bar.set_foreground_color(indicators::Color::YELLOW); - - // Show time elapsed and remaining - bar.show_elapsed_time(); - bar.show_remaining_time(); + indicators::ProgressBar bar{ + indicators::option::BarWidth{50}, + indicators::option::Start{" ["}, + indicators::option::Fill{"█"}, + indicators::option::Lead{"█"}, + indicators::option::Remainder{"-"}, + indicators::option::End{"]"}, + indicators::option::PostfixText{"Training Gaze Network "}, + indicators::option::ForegroundColor{indicators::Color::YELLOW}, + indicators::option::ShowElapsedTime{true}, + indicators::option::ShowRemainingTime{true}, + }; // Update bar state while (true) {