Skip to content

Commit 9c855c9

Browse files
authored
Merge pull request #136 from Kicer86/progressbars_ownership
Make DynamicProgress take ownership over progress bars
2 parents 222382c + 7962c54 commit 9c855c9

File tree

3 files changed

+82
-54
lines changed

3 files changed

+82
-54
lines changed

README.md

+51-27
Original file line numberDiff line numberDiff line change
@@ -442,35 +442,59 @@ using namespace indicators;
442442

443443
int main() {
444444

445-
ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red},
446-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
447-
option::PrefixText{"5c90d4a2d1a8: Downloading "}};
448-
449-
ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
450-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
451-
option::PrefixText{"22337bfd13a9: Downloading "}};
452-
453-
ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green},
454-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
455-
option::PrefixText{"10f26c680a34: Downloading "}};
456-
457-
ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white},
458-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
459-
option::PrefixText{"6364e0d7a283: Downloading "}};
460-
461-
ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue},
462-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
463-
option::PrefixText{"ff1356ba118b: Downloading "}};
464-
465-
ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
466-
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
467-
option::PrefixText{"5a17453338b4: Downloading "}};
445+
auto bar1 = std::make_unique<ProgressBar>(option::BarWidth{50},
446+
option::ForegroundColor{Color::red},
447+
option::ShowElapsedTime{true},
448+
option::ShowRemainingTime{true},
449+
option::PrefixText{"5c90d4a2d1a8: Downloading "},
450+
indicators::option::FontStyles{
451+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
452+
453+
auto bar2 = std::make_unique<ProgressBar>(option::BarWidth{50},
454+
option::ForegroundColor{Color::yellow},
455+
option::ShowElapsedTime{true},
456+
option::ShowRemainingTime{true},
457+
option::PrefixText{"22337bfd13a9: Downloading "},
458+
indicators::option::FontStyles{
459+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
460+
461+
auto bar3 = std::make_unique<ProgressBar>(option::BarWidth{50},
462+
option::ForegroundColor{Color::green},
463+
option::ShowElapsedTime{true},
464+
option::ShowRemainingTime{true},
465+
option::PrefixText{"10f26c680a34: Downloading "},
466+
indicators::option::FontStyles{
467+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
468+
469+
auto bar4 = std::make_unique<ProgressBar>(option::BarWidth{50},
470+
option::ForegroundColor{Color::white},
471+
option::ShowElapsedTime{true},
472+
option::ShowRemainingTime{true},
473+
option::PrefixText{"6364e0d7a283: Downloading "},
474+
indicators::option::FontStyles{
475+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
476+
477+
auto bar5 = std::make_unique<ProgressBar>(option::BarWidth{50},
478+
option::ForegroundColor{Color::blue},
479+
option::ShowElapsedTime{true},
480+
option::ShowRemainingTime{true},
481+
option::PrefixText{"ff1356ba118b: Downloading "},
482+
indicators::option::FontStyles{
483+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
484+
485+
auto bar6 = std::make_unique<ProgressBar>(option::BarWidth{50},
486+
option::ForegroundColor{Color::cyan},
487+
option::ShowElapsedTime{true},
488+
option::ShowRemainingTime{true},
489+
option::PrefixText{"5a17453338b4: Downloading "},
490+
indicators::option::FontStyles{
491+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
468492

469493
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
470494

471495
// Construct with 3 progress bars. We'll add 3 more at a later point
472496
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
473-
497+
474498
// Do not hide bars when completed
475499
bars.set_option(option::HideBarWhenComplete{false});
476500

@@ -518,7 +542,7 @@ int main() {
518542
if (bars[0].is_completed()) {
519543
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
520544
// bar1 is completed, adding bar6
521-
auto i = bars.push_back(bar6);
545+
auto i = bars.push_back(std::move(bar6));
522546
sixth_job = std::thread(job6, i);
523547
sixth_job.join();
524548
break;
@@ -533,7 +557,7 @@ int main() {
533557
if (bars[1].is_completed()) {
534558
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
535559
// bar2 is completed, adding bar5
536-
auto i = bars.push_back(bar5);
560+
auto i = bars.push_back(std::move(bar5));
537561
fifth_job = std::thread(job5, i);
538562
fifth_job.join();
539563
break;
@@ -548,7 +572,7 @@ int main() {
548572
if (bars[2].is_completed()) {
549573
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
550574
// bar3 is completed, adding bar4
551-
auto i = bars.push_back(bar4);
575+
auto i = bars.push_back(std::move(bar4));
552576
fourth_job = std::thread(job4, i);
553577
fourth_job.join();
554578
break;

include/indicators/dynamic_progress.hpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <indicators/cursor_movement.hpp>
1111
#include <indicators/details/stream_helper.hpp>
1212
#include <iostream>
13+
#include <memory>
1314
#include <mutex>
1415
#include <vector>
1516

@@ -19,10 +20,10 @@ template <typename Indicator> class DynamicProgress {
1920
using Settings = std::tuple<option::HideBarWhenComplete>;
2021

2122
public:
22-
template <typename... Indicators> explicit DynamicProgress(Indicators &... bars) {
23-
bars_ = {bars...};
23+
template <typename... Indicators> explicit DynamicProgress(Indicators &&... bars) {
24+
(bars_.emplace_back(std::move(bars)), ...);
2425
for (auto &bar : bars_) {
25-
bar.get().multi_progress_mode_ = true;
26+
bar->multi_progress_mode_ = true;
2627
++total_count_;
2728
++incomplete_count_;
2829
}
@@ -31,13 +32,13 @@ template <typename Indicator> class DynamicProgress {
3132
Indicator &operator[](size_t index) {
3233
print_progress();
3334
std::lock_guard<std::mutex> lock{mutex_};
34-
return bars_[index].get();
35+
return *bars_[index];
3536
}
3637

37-
size_t push_back(Indicator &bar) {
38+
size_t push_back(std::unique_ptr<Indicator> bar) {
3839
std::lock_guard<std::mutex> lock{mutex_};
39-
bar.multi_progress_mode_ = true;
40-
bars_.push_back(bar);
40+
bar->multi_progress_mode_ = true;
41+
bars_.push_back(std::move(bar));
4142
return bars_.size() - 1;
4243
}
4344

@@ -63,7 +64,7 @@ template <typename Indicator> class DynamicProgress {
6364
Settings settings_;
6465
std::atomic<bool> started_{false};
6566
std::mutex mutex_;
66-
std::vector<std::reference_wrapper<Indicator>> bars_;
67+
std::vector<std::unique_ptr<Indicator>> bars_;
6768
std::atomic<size_t> total_count_{0};
6869
std::atomic<size_t> incomplete_count_{0};
6970

@@ -93,8 +94,8 @@ template <typename Indicator> class DynamicProgress {
9394
}
9495
incomplete_count_ = 0;
9596
for (auto &bar : bars_) {
96-
if (!bar.get().is_completed()) {
97-
bar.get().print_progress(true);
97+
if (!bar->is_completed()) {
98+
bar->print_progress(true);
9899
std::cout << "\n";
99100
++incomplete_count_;
100101
}
@@ -106,7 +107,7 @@ template <typename Indicator> class DynamicProgress {
106107
if (started_)
107108
move_up(static_cast<int>(total_count_));
108109
for (auto &bar : bars_) {
109-
bar.get().print_progress(true);
110+
bar->print_progress(true);
110111
std::cout << "\n";
111112
}
112113
if (!started_)
@@ -119,4 +120,4 @@ template <typename Indicator> class DynamicProgress {
119120

120121
} // namespace indicators
121122

122-
#endif
123+
#endif

samples/dynamic_progress.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,60 @@ using namespace indicators;
44

55
int main() {
66

7-
ProgressBar bar1{option::BarWidth{50},
7+
auto bar1 = std::make_unique<ProgressBar>(option::BarWidth{50},
88
option::ForegroundColor{Color::red},
99
option::ShowElapsedTime{true},
1010
option::ShowRemainingTime{true},
1111
option::PrefixText{"5c90d4a2d1a8: Downloading "},
1212
indicators::option::FontStyles{
13-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
13+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
1414

15-
ProgressBar bar2{option::BarWidth{50},
15+
auto bar2 = std::make_unique<ProgressBar>(option::BarWidth{50},
1616
option::ForegroundColor{Color::yellow},
1717
option::ShowElapsedTime{true},
1818
option::ShowRemainingTime{true},
1919
option::PrefixText{"22337bfd13a9: Downloading "},
2020
indicators::option::FontStyles{
21-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
21+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
2222

23-
ProgressBar bar3{option::BarWidth{50},
23+
auto bar3 = std::make_unique<ProgressBar>(option::BarWidth{50},
2424
option::ForegroundColor{Color::green},
2525
option::ShowElapsedTime{true},
2626
option::ShowRemainingTime{true},
2727
option::PrefixText{"10f26c680a34: Downloading "},
2828
indicators::option::FontStyles{
29-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
29+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
3030

31-
ProgressBar bar4{option::BarWidth{50},
31+
auto bar4 = std::make_unique<ProgressBar>(option::BarWidth{50},
3232
option::ForegroundColor{Color::white},
3333
option::ShowElapsedTime{true},
3434
option::ShowRemainingTime{true},
3535
option::PrefixText{"6364e0d7a283: Downloading "},
3636
indicators::option::FontStyles{
37-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
37+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
3838

39-
ProgressBar bar5{option::BarWidth{50},
39+
auto bar5 = std::make_unique<ProgressBar>(option::BarWidth{50},
4040
option::ForegroundColor{Color::blue},
4141
option::ShowElapsedTime{true},
4242
option::ShowRemainingTime{true},
4343
option::PrefixText{"ff1356ba118b: Downloading "},
4444
indicators::option::FontStyles{
45-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
45+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
4646

47-
ProgressBar bar6{option::BarWidth{50},
47+
auto bar6 = std::make_unique<ProgressBar>(option::BarWidth{50},
4848
option::ForegroundColor{Color::cyan},
4949
option::ShowElapsedTime{true},
5050
option::ShowRemainingTime{true},
5151
option::PrefixText{"5a17453338b4: Downloading "},
5252
indicators::option::FontStyles{
53-
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
53+
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
5454

5555
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
5656

57+
// Construct with 3 progress bars. We'll add 3 more at a later point
5758
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
59+
60+
// Do not hide bars when completed
5861
bars.set_option(option::HideBarWhenComplete{false});
5962

6063
std::thread fourth_job, fifth_job, sixth_job;
@@ -101,7 +104,7 @@ int main() {
101104
if (bars[0].is_completed()) {
102105
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
103106
// bar1 is completed, adding bar6
104-
auto i = bars.push_back(bar6);
107+
auto i = bars.push_back(std::move(bar6));
105108
sixth_job = std::thread(job6, i);
106109
sixth_job.join();
107110
break;
@@ -116,7 +119,7 @@ int main() {
116119
if (bars[1].is_completed()) {
117120
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
118121
// bar2 is completed, adding bar5
119-
auto i = bars.push_back(bar5);
122+
auto i = bars.push_back(std::move(bar5));
120123
fifth_job = std::thread(job5, i);
121124
fifth_job.join();
122125
break;
@@ -131,7 +134,7 @@ int main() {
131134
if (bars[2].is_completed()) {
132135
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
133136
// bar3 is completed, adding bar4
134-
auto i = bars.push_back(bar4);
137+
auto i = bars.push_back(std::move(bar4));
135138
fourth_job = std::thread(job4, i);
136139
fourth_job.join();
137140
break;

0 commit comments

Comments
 (0)