Updated multiple bars sample

This commit is contained in:
Pranav Srinivas KumaR
2019-12-03 23:07:44 -06:00
parent 841e175d3f
commit a57a492216
2 changed files with 10 additions and 16 deletions

View File

@@ -57,12 +57,7 @@ public:
void set_progress(float value) { void set_progress(float value) {
{ {
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
if (_completed)
return;
_progress = value; _progress = value;
if (_progress >= 100.0) {
_completed = true;
}
} }
_print_progress(); _print_progress();
} }
@@ -70,12 +65,7 @@ public:
void tick() { void tick() {
{ {
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
if (_completed)
return;
_progress += 1; _progress += 1;
if (_progress >= 100.0) {
_completed = true;
}
} }
_print_progress(); _print_progress();
} }
@@ -146,8 +136,12 @@ private:
if (_show_percentage) { if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%"; std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
} }
if (_max_text_after_length == 0) _max_text_after_length = 20;
std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r"; std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r";
std::cout.flush(); std::cout.flush();
if (_progress > 100.0) {
_completed = true;
}
if (_completed) if (_completed)
std::cout << termcolor::reset << std::endl; std::cout << termcolor::reset << std::endl;
} }

View File

@@ -47,13 +47,12 @@ int main() {
// //
ProgressBar p2; ProgressBar p2;
p2.bar_width(50); p2.bar_width(50);
p2.start_with("["); p2.start_with("Getting started [");
p2.fill_progress_with("#"); p2.fill_progress_with("#");
p2.lead_progress_with("#"); p2.lead_progress_with("#");
p2.fill_remainder_with(" "); p2.fill_remainder_with(" ");
p2.end_with("]"); p2.end_with("]");
p2.color(ProgressBar::Color::GREEN); p2.color(ProgressBar::Color::GREEN);
p2.append_text("Getting started");
auto job2 = [&p2]() { auto job2 = [&p2]() {
while (true) { while (true) {
if (p2.completed()) if (p2.completed())
@@ -61,13 +60,13 @@ int main() {
p2.tick(); p2.tick();
auto ticks = p2.current(); auto ticks = p2.current();
if (ticks > 20 && ticks < 50) if (ticks > 20 && ticks < 50)
p2.append_text("Calling Lionel Messi"); p2.start_with("Delaying the inevitable [");
else if (ticks > 50 && ticks < 80) else if (ticks > 50 && ticks < 80)
p2.append_text("Crying quietly"); p2.start_with("Crying quietly [");
else if (ticks > 80) else if (ticks > 80)
p2.append_text("Almost there"); p2.start_with("Almost there [");
else if (ticks > 98) else if (ticks > 98)
p2.append_text("Done"); p2.start_with("Done [");
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
}; };
@@ -91,6 +90,7 @@ int main() {
p3.start_with("Downloading the Sun " + std::to_string(p3.current()) + "% "); p3.start_with("Downloading the Sun " + std::to_string(p3.current()) + "% ");
p3.tick(); p3.tick();
if (p3.completed()) { if (p3.completed()) {
p3.start_with("Downloading the Sun 100%");
break; break;
} }
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));