mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Merge pull request #136 from Kicer86/progressbars_ownership
Make DynamicProgress take ownership over progress bars
This commit is contained in:
66
README.md
66
README.md
@@ -442,29 +442,53 @@ using namespace indicators;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red},
|
auto bar1 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::red},
|
||||||
option::PrefixText{"5c90d4a2d1a8: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"5c90d4a2d1a8: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
|
auto bar2 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::yellow},
|
||||||
option::PrefixText{"22337bfd13a9: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"22337bfd13a9: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green},
|
auto bar3 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::green},
|
||||||
option::PrefixText{"10f26c680a34: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"10f26c680a34: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white},
|
auto bar4 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::white},
|
||||||
option::PrefixText{"6364e0d7a283: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"6364e0d7a283: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue},
|
auto bar5 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::blue},
|
||||||
option::PrefixText{"ff1356ba118b: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"ff1356ba118b: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
|
auto bar6 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
|
option::ForegroundColor{Color::cyan},
|
||||||
option::PrefixText{"5a17453338b4: Downloading "}};
|
option::ShowElapsedTime{true},
|
||||||
|
option::ShowRemainingTime{true},
|
||||||
|
option::PrefixText{"5a17453338b4: Downloading "},
|
||||||
|
indicators::option::FontStyles{
|
||||||
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
|
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
|
||||||
|
|
||||||
@@ -518,7 +542,7 @@ int main() {
|
|||||||
if (bars[0].is_completed()) {
|
if (bars[0].is_completed()) {
|
||||||
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
|
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
|
||||||
// bar1 is completed, adding bar6
|
// bar1 is completed, adding bar6
|
||||||
auto i = bars.push_back(bar6);
|
auto i = bars.push_back(std::move(bar6));
|
||||||
sixth_job = std::thread(job6, i);
|
sixth_job = std::thread(job6, i);
|
||||||
sixth_job.join();
|
sixth_job.join();
|
||||||
break;
|
break;
|
||||||
@@ -533,7 +557,7 @@ int main() {
|
|||||||
if (bars[1].is_completed()) {
|
if (bars[1].is_completed()) {
|
||||||
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
|
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
|
||||||
// bar2 is completed, adding bar5
|
// bar2 is completed, adding bar5
|
||||||
auto i = bars.push_back(bar5);
|
auto i = bars.push_back(std::move(bar5));
|
||||||
fifth_job = std::thread(job5, i);
|
fifth_job = std::thread(job5, i);
|
||||||
fifth_job.join();
|
fifth_job.join();
|
||||||
break;
|
break;
|
||||||
@@ -548,7 +572,7 @@ int main() {
|
|||||||
if (bars[2].is_completed()) {
|
if (bars[2].is_completed()) {
|
||||||
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
|
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
|
||||||
// bar3 is completed, adding bar4
|
// bar3 is completed, adding bar4
|
||||||
auto i = bars.push_back(bar4);
|
auto i = bars.push_back(std::move(bar4));
|
||||||
fourth_job = std::thread(job4, i);
|
fourth_job = std::thread(job4, i);
|
||||||
fourth_job.join();
|
fourth_job.join();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <indicators/cursor_movement.hpp>
|
#include <indicators/cursor_movement.hpp>
|
||||||
#include <indicators/details/stream_helper.hpp>
|
#include <indicators/details/stream_helper.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -19,10 +20,10 @@ template <typename Indicator> class DynamicProgress {
|
|||||||
using Settings = std::tuple<option::HideBarWhenComplete>;
|
using Settings = std::tuple<option::HideBarWhenComplete>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename... Indicators> explicit DynamicProgress(Indicators &... bars) {
|
template <typename... Indicators> explicit DynamicProgress(Indicators &&... bars) {
|
||||||
bars_ = {bars...};
|
(bars_.emplace_back(std::move(bars)), ...);
|
||||||
for (auto &bar : bars_) {
|
for (auto &bar : bars_) {
|
||||||
bar.get().multi_progress_mode_ = true;
|
bar->multi_progress_mode_ = true;
|
||||||
++total_count_;
|
++total_count_;
|
||||||
++incomplete_count_;
|
++incomplete_count_;
|
||||||
}
|
}
|
||||||
@@ -31,13 +32,13 @@ public:
|
|||||||
Indicator &operator[](size_t index) {
|
Indicator &operator[](size_t index) {
|
||||||
print_progress();
|
print_progress();
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
return bars_[index].get();
|
return *bars_[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t push_back(Indicator &bar) {
|
size_t push_back(std::unique_ptr<Indicator> bar) {
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
bar.multi_progress_mode_ = true;
|
bar->multi_progress_mode_ = true;
|
||||||
bars_.push_back(bar);
|
bars_.push_back(std::move(bar));
|
||||||
return bars_.size() - 1;
|
return bars_.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ private:
|
|||||||
Settings settings_;
|
Settings settings_;
|
||||||
std::atomic<bool> started_{false};
|
std::atomic<bool> started_{false};
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
std::vector<std::reference_wrapper<Indicator>> bars_;
|
std::vector<std::unique_ptr<Indicator>> bars_;
|
||||||
std::atomic<size_t> total_count_{0};
|
std::atomic<size_t> total_count_{0};
|
||||||
std::atomic<size_t> incomplete_count_{0};
|
std::atomic<size_t> incomplete_count_{0};
|
||||||
|
|
||||||
@@ -93,8 +94,8 @@ public:
|
|||||||
}
|
}
|
||||||
incomplete_count_ = 0;
|
incomplete_count_ = 0;
|
||||||
for (auto &bar : bars_) {
|
for (auto &bar : bars_) {
|
||||||
if (!bar.get().is_completed()) {
|
if (!bar->is_completed()) {
|
||||||
bar.get().print_progress(true);
|
bar->print_progress(true);
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
++incomplete_count_;
|
++incomplete_count_;
|
||||||
}
|
}
|
||||||
@@ -106,7 +107,7 @@ public:
|
|||||||
if (started_)
|
if (started_)
|
||||||
move_up(static_cast<int>(total_count_));
|
move_up(static_cast<int>(total_count_));
|
||||||
for (auto &bar : bars_) {
|
for (auto &bar : bars_) {
|
||||||
bar.get().print_progress(true);
|
bar->print_progress(true);
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
if (!started_)
|
if (!started_)
|
||||||
|
|||||||
@@ -4,57 +4,60 @@ using namespace indicators;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
ProgressBar bar1{option::BarWidth{50},
|
auto bar1 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::red},
|
option::ForegroundColor{Color::red},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"5c90d4a2d1a8: Downloading "},
|
option::PrefixText{"5c90d4a2d1a8: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar2{option::BarWidth{50},
|
auto bar2 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::yellow},
|
option::ForegroundColor{Color::yellow},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"22337bfd13a9: Downloading "},
|
option::PrefixText{"22337bfd13a9: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar3{option::BarWidth{50},
|
auto bar3 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::green},
|
option::ForegroundColor{Color::green},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"10f26c680a34: Downloading "},
|
option::PrefixText{"10f26c680a34: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar4{option::BarWidth{50},
|
auto bar4 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::white},
|
option::ForegroundColor{Color::white},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"6364e0d7a283: Downloading "},
|
option::PrefixText{"6364e0d7a283: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar5{option::BarWidth{50},
|
auto bar5 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::blue},
|
option::ForegroundColor{Color::blue},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"ff1356ba118b: Downloading "},
|
option::PrefixText{"ff1356ba118b: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
ProgressBar bar6{option::BarWidth{50},
|
auto bar6 = std::make_unique<ProgressBar>(option::BarWidth{50},
|
||||||
option::ForegroundColor{Color::cyan},
|
option::ForegroundColor{Color::cyan},
|
||||||
option::ShowElapsedTime{true},
|
option::ShowElapsedTime{true},
|
||||||
option::ShowRemainingTime{true},
|
option::ShowRemainingTime{true},
|
||||||
option::PrefixText{"5a17453338b4: Downloading "},
|
option::PrefixText{"5a17453338b4: Downloading "},
|
||||||
indicators::option::FontStyles{
|
indicators::option::FontStyles{
|
||||||
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}}};
|
std::vector<indicators::FontStyle>{indicators::FontStyle::bold}});
|
||||||
|
|
||||||
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
|
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
|
||||||
|
|
||||||
|
// Construct with 3 progress bars. We'll add 3 more at a later point
|
||||||
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
|
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
|
||||||
|
|
||||||
|
// Do not hide bars when completed
|
||||||
bars.set_option(option::HideBarWhenComplete{false});
|
bars.set_option(option::HideBarWhenComplete{false});
|
||||||
|
|
||||||
std::thread fourth_job, fifth_job, sixth_job;
|
std::thread fourth_job, fifth_job, sixth_job;
|
||||||
@@ -101,7 +104,7 @@ int main() {
|
|||||||
if (bars[0].is_completed()) {
|
if (bars[0].is_completed()) {
|
||||||
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
|
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
|
||||||
// bar1 is completed, adding bar6
|
// bar1 is completed, adding bar6
|
||||||
auto i = bars.push_back(bar6);
|
auto i = bars.push_back(std::move(bar6));
|
||||||
sixth_job = std::thread(job6, i);
|
sixth_job = std::thread(job6, i);
|
||||||
sixth_job.join();
|
sixth_job.join();
|
||||||
break;
|
break;
|
||||||
@@ -116,7 +119,7 @@ int main() {
|
|||||||
if (bars[1].is_completed()) {
|
if (bars[1].is_completed()) {
|
||||||
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
|
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
|
||||||
// bar2 is completed, adding bar5
|
// bar2 is completed, adding bar5
|
||||||
auto i = bars.push_back(bar5);
|
auto i = bars.push_back(std::move(bar5));
|
||||||
fifth_job = std::thread(job5, i);
|
fifth_job = std::thread(job5, i);
|
||||||
fifth_job.join();
|
fifth_job.join();
|
||||||
break;
|
break;
|
||||||
@@ -131,7 +134,7 @@ int main() {
|
|||||||
if (bars[2].is_completed()) {
|
if (bars[2].is_completed()) {
|
||||||
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
|
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
|
||||||
// bar3 is completed, adding bar4
|
// bar3 is completed, adding bar4
|
||||||
auto i = bars.push_back(bar4);
|
auto i = bars.push_back(std::move(bar4));
|
||||||
fourth_job = std::thread(job4, i);
|
fourth_job = std::thread(job4, i);
|
||||||
fourth_job.join();
|
fourth_job.join();
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user