|
39 | 39 | * [Progress Bar](#progress-bar)
|
40 | 40 | * [Block Progress Bar](#block-progress-bar)
|
41 | 41 | * [Multi Progress](#multiprogress)
|
| 42 | +* [Dynamic Progress](#dynamicprogress) |
42 | 43 | * [Progress Spinner](#progress-spinner)
|
43 | 44 | * [Contributing](#contributing)
|
44 | 45 | * [License](#license)
|
@@ -350,6 +351,159 @@ int main() {
|
350 | 351 | }
|
351 | 352 | ```
|
352 | 353 |
|
| 354 | +# DynamicProgress |
| 355 | + |
| 356 | +`DynamicProgress` is a container class, similar to `MultiProgress`, for managing multiple progress bars. As the name suggests, with `DynamicProgress`, you can dynamically add new progress bars. Simply call `bars.push_back`. |
| 357 | + |
| 358 | +Below is an example `DynamicProgress` object that manages six `ProgressBar` objects. Three of these bars are added dynamically. |
| 359 | + |
| 360 | +<p align="center"> |
| 361 | + <img src="img/dynamic_progress_bar.gif"/> |
| 362 | +</p> |
| 363 | + |
| 364 | +```cpp |
| 365 | +#include <indicators/dynamic_progress.hpp> |
| 366 | +#include <indicators/progress_bar.hpp> |
| 367 | +using namespace indicators; |
| 368 | + |
| 369 | +int main() { |
| 370 | + |
| 371 | + ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red}, |
| 372 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 373 | + option::PrefixText{"5c90d4a2d1a8: Downloading "}}; |
| 374 | + |
| 375 | + ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow}, |
| 376 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 377 | + option::PrefixText{"22337bfd13a9: Downloading "}}; |
| 378 | + |
| 379 | + ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green}, |
| 380 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 381 | + option::PrefixText{"10f26c680a34: Downloading "}}; |
| 382 | + |
| 383 | + ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white}, |
| 384 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 385 | + option::PrefixText{"6364e0d7a283: Downloading "}}; |
| 386 | + |
| 387 | + ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue}, |
| 388 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 389 | + option::PrefixText{"ff1356ba118b: Downloading "}}; |
| 390 | + |
| 391 | + ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan}, |
| 392 | + option::ShowElapsedTime{true}, option::ShowRemainingTime{true}, |
| 393 | + option::PrefixText{"5a17453338b4: Downloading "}}; |
| 394 | + |
| 395 | + std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n"; |
| 396 | + |
| 397 | + // Construct with 3 progress bars. We'll add 3 more at a later point |
| 398 | + DynamicProgress<ProgressBar> bars(bar1, bar2, bar3); |
| 399 | + |
| 400 | + // Do not hide bars when completed |
| 401 | + bars.set_option(option::HideBarWhenComplete{false}); |
| 402 | + |
| 403 | + std::thread fourth_job, fifth_job, sixth_job; |
| 404 | + |
| 405 | + auto job4 = [&bars](size_t i) { |
| 406 | + while (true) { |
| 407 | + bars[i].tick(); |
| 408 | + if (bars[i].is_completed()) { |
| 409 | + bars[i].set_option(option::PrefixText{"6364e0d7a283: Pull complete "}); |
| 410 | + bars[i].mark_as_completed(); |
| 411 | + break; |
| 412 | + } |
| 413 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 414 | + } |
| 415 | + }; |
| 416 | + |
| 417 | + auto job5 = [&bars](size_t i) { |
| 418 | + while (true) { |
| 419 | + bars[i].tick(); |
| 420 | + if (bars[i].is_completed()) { |
| 421 | + bars[i].set_option(option::PrefixText{"ff1356ba118b: Pull complete "}); |
| 422 | + bars[i].mark_as_completed(); |
| 423 | + break; |
| 424 | + } |
| 425 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 426 | + } |
| 427 | + }; |
| 428 | + |
| 429 | + auto job6 = [&bars](size_t i) { |
| 430 | + while (true) { |
| 431 | + bars[i].tick(); |
| 432 | + if (bars[i].is_completed()) { |
| 433 | + bars[i].set_option(option::PrefixText{"5a17453338b4: Pull complete "}); |
| 434 | + bars[i].mark_as_completed(); |
| 435 | + break; |
| 436 | + } |
| 437 | + std::this_thread::sleep_for(std::chrono::milliseconds(40)); |
| 438 | + } |
| 439 | + }; |
| 440 | + |
| 441 | + auto job1 = [&bars, &bar6, &sixth_job, &job6]() { |
| 442 | + while (true) { |
| 443 | + bars[0].tick(); |
| 444 | + if (bars[0].is_completed()) { |
| 445 | + bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "}); |
| 446 | + // bar1 is completed, adding bar6 |
| 447 | + auto i = bars.push_back(bar6); |
| 448 | + sixth_job = std::thread(job6, i); |
| 449 | + sixth_job.join(); |
| 450 | + break; |
| 451 | + } |
| 452 | + std::this_thread::sleep_for(std::chrono::milliseconds(140)); |
| 453 | + } |
| 454 | + }; |
| 455 | + |
| 456 | + auto job2 = [&bars, &bar5, &fifth_job, &job5]() { |
| 457 | + while (true) { |
| 458 | + bars[1].tick(); |
| 459 | + if (bars[1].is_completed()) { |
| 460 | + bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "}); |
| 461 | + // bar2 is completed, adding bar5 |
| 462 | + auto i = bars.push_back(bar5); |
| 463 | + fifth_job = std::thread(job5, i); |
| 464 | + fifth_job.join(); |
| 465 | + break; |
| 466 | + } |
| 467 | + std::this_thread::sleep_for(std::chrono::milliseconds(25)); |
| 468 | + } |
| 469 | + }; |
| 470 | + |
| 471 | + auto job3 = [&bars, &bar4, &fourth_job, &job4]() { |
| 472 | + while (true) { |
| 473 | + bars[2].tick(); |
| 474 | + if (bars[2].is_completed()) { |
| 475 | + bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "}); |
| 476 | + // bar3 is completed, adding bar4 |
| 477 | + auto i = bars.push_back(bar4); |
| 478 | + fourth_job = std::thread(job4, i); |
| 479 | + fourth_job.join(); |
| 480 | + break; |
| 481 | + } |
| 482 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 483 | + } |
| 484 | + }; |
| 485 | + |
| 486 | + std::thread first_job(job1); |
| 487 | + std::thread second_job(job2); |
| 488 | + std::thread third_job(job3); |
| 489 | + |
| 490 | + third_job.join(); |
| 491 | + second_job.join(); |
| 492 | + first_job.join(); |
| 493 | + |
| 494 | + std::cout << termcolor::bold << termcolor::green << "✔ Downloaded image foo/bar:baz" << std::endl; |
| 495 | + std::cout << termcolor::reset; |
| 496 | + |
| 497 | + return 0; |
| 498 | +} |
| 499 | +``` |
| 500 | + |
| 501 | +In the above code, notice the option `bars.set_option(option::HideBarWhenComplete{true});`. Yes, you can hide progress bars as and when they complete by setting this option to `true`. If you do so, the above example will look like this: |
| 502 | + |
| 503 | +<p align="center"> |
| 504 | + <img src="img/dynamic_progress_bar_hide_completed.gif"/> |
| 505 | +</p> |
| 506 | + |
353 | 507 | # Progress Spinner
|
354 | 508 |
|
355 | 509 | To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object. Here's the general structure of a progress spinner:
|
|
0 commit comments