mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Added sample to show max_progress working with iterables. Updated README
This commit is contained in:
63
README.md
63
README.md
@@ -36,6 +36,7 @@
|
|||||||
* [Multi Progress](#multiprogress)
|
* [Multi Progress](#multiprogress)
|
||||||
* [Dynamic Progress](#dynamicprogress)
|
* [Dynamic Progress](#dynamicprogress)
|
||||||
* [Progress Spinner](#progress-spinner)
|
* [Progress Spinner](#progress-spinner)
|
||||||
|
* [Working with Iterables](#working-with-iterables)
|
||||||
* [Building Samples](#building-samples)
|
* [Building Samples](#building-samples)
|
||||||
* [Contributing](#contributing)
|
* [Contributing](#contributing)
|
||||||
* [License](#license)
|
* [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`:
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="img/block_progress_bar_iterable.gif"/>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <chrono>
|
||||||
|
#include <indicators/block_progress_bar.hpp>
|
||||||
|
#include <indicators/cursor_control.hpp>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// Hide cursor
|
||||||
|
indicators::show_console_cursor(false);
|
||||||
|
|
||||||
|
// Random list of numbers
|
||||||
|
std::vector<size_t> 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>{FontStyle::bold}},
|
||||||
|
option::MaxProgress{numbers.size()}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::cout << "Iterating over a list of numbers (size = "
|
||||||
|
<< numbers.size() << ")\n";
|
||||||
|
|
||||||
|
std::vector<size_t> 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
|
## Building Samples
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
BIN
img/block_progress_bar_iterable.gif
Normal file
BIN
img/block_progress_bar_iterable.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 259 KiB |
@@ -2,6 +2,9 @@
|
|||||||
add_executable(block_progress_bar block_progress_bar.cpp)
|
add_executable(block_progress_bar block_progress_bar.cpp)
|
||||||
target_link_libraries(block_progress_bar PRIVATE indicators::indicators)
|
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)
|
add_executable(multi_threaded_bar multi_threaded_bar.cpp)
|
||||||
target_link_libraries(multi_threaded_bar PRIVATE indicators::indicators)
|
target_link_libraries(multi_threaded_bar PRIVATE indicators::indicators)
|
||||||
|
|
||||||
|
|||||||
50
samples/block_progress_bar_iterable.cpp
Normal file
50
samples/block_progress_bar_iterable.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <indicators/block_progress_bar.hpp>
|
||||||
|
#include <indicators/cursor_control.hpp>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// Hide cursor
|
||||||
|
indicators::show_console_cursor(false);
|
||||||
|
|
||||||
|
// Random list of numbers
|
||||||
|
std::vector<size_t> 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>{FontStyle::bold}},
|
||||||
|
option::MaxProgress{numbers.size()}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::cout << "Iterating over a list of numbers (size = "
|
||||||
|
<< numbers.size() << ")\n";
|
||||||
|
|
||||||
|
std::vector<size_t> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user