diff --git a/README.md b/README.md index 9eb0e60..ce81310 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ * [Multi Progress](#multiprogress) * [Dynamic Progress](#dynamicprogress) * [Progress Spinner](#progress-spinner) +* [Working with Iterables](#working-with-iterables) * [Building Samples](#building-samples) * [Contributing](#contributing) * [License](#license) @@ -623,6 +624,68 @@ int main() { } ``` +## Working with Iterables + +If you'd like to use progress bars to indicate progress while iterating over iterables, e.g., a list of numbers, this +can be achieved by using the `option::MaxProgress`: + +

+ +

+ +```cpp +#include +#include +#include +#include + +int main() { + + // Hide cursor + indicators::show_console_cursor(false); + + // Random list of numbers + std::vector numbers; + for (size_t i = 0; i < 1259438; ++i) { + numbers.push_back(i); + } + + using namespace indicators; + BlockProgressBar bar{ + option::BarWidth{80}, + option::ForegroundColor{Color::white}, + option::FontStyles{ + std::vector{FontStyle::bold}}, + option::MaxProgress{numbers.size()} + }; + + std::cout << "Iterating over a list of numbers (size = " + << numbers.size() << ")\n"; + + std::vector result; + for (size_t i = 0; i < numbers.size(); ++i) { + + // Perform some computation + result.push_back(numbers[i] * numbers[i]); + + // Show iteration as postfix text + bar.set_option(option::PostfixText{ + std::to_string(i) + "/" + std::to_string(numbers.size()) + }); + + // update progress bar + bar.tick(); + } + + bar.mark_as_completed(); + + // Show cursor + indicators::show_console_cursor(true); + + return 0; +} +``` + ## Building Samples ```bash diff --git a/img/block_progress_bar_iterable.gif b/img/block_progress_bar_iterable.gif new file mode 100644 index 0000000..bab646a Binary files /dev/null and b/img/block_progress_bar_iterable.gif differ diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 674086d..463cd58 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -2,6 +2,9 @@ add_executable(block_progress_bar block_progress_bar.cpp) target_link_libraries(block_progress_bar PRIVATE indicators::indicators) +add_executable(block_progress_bar_iterable block_progress_bar_iterable.cpp) +target_link_libraries(block_progress_bar_iterable PRIVATE indicators::indicators) + add_executable(multi_threaded_bar multi_threaded_bar.cpp) target_link_libraries(multi_threaded_bar PRIVATE indicators::indicators) diff --git a/samples/block_progress_bar_iterable.cpp b/samples/block_progress_bar_iterable.cpp new file mode 100644 index 0000000..31814dd --- /dev/null +++ b/samples/block_progress_bar_iterable.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +int main() { + + // Hide cursor + indicators::show_console_cursor(false); + + // Random list of numbers + std::vector numbers; + for (size_t i = 0; i < 1259438; ++i) { + numbers.push_back(i); + } + + using namespace indicators; + BlockProgressBar bar{ + option::BarWidth{80}, + option::ForegroundColor{Color::white}, + option::FontStyles{ + std::vector{FontStyle::bold}}, + option::MaxProgress{numbers.size()} + }; + + std::cout << "Iterating over a list of numbers (size = " + << numbers.size() << ")\n"; + + std::vector result; + for (size_t i = 0; i < numbers.size(); ++i) { + + // Perform some computation + result.push_back(numbers[i] * numbers[i]); + + // Show iteration as postfix text + bar.set_option(option::PostfixText{ + std::to_string(i) + "/" + std::to_string(numbers.size()) + }); + + // update progress bar + bar.tick(); + } + + bar.mark_as_completed(); + + // Show cursor + indicators::show_console_cursor(true); + + return 0; +}