Minor API updates

This commit is contained in:
Pranav Srinivas Kumar
2019-12-04 07:56:57 -06:00
parent 94ee778cbd
commit c31e341558
2 changed files with 15 additions and 15 deletions

View File

@@ -45,11 +45,11 @@ public:
_end = end; _end = end;
} }
void append_text(const std::string& text) { void set_status_text(const std::string& text) {
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
_text_after = text; _status_text = text;
if (_text_after.length() > _max_text_after_length) if (_status_text.length() > _max_status_text_length)
_max_text_after_length = _text_after.length(); _max_status_text_length = _status_text.length();
} }
void show_percentage(bool flag) { _show_percentage = flag; } void show_percentage(bool flag) { _show_percentage = flag; }
@@ -86,8 +86,8 @@ private:
std::string _lead{">"}; std::string _lead{">"};
std::string _remainder{" "}; std::string _remainder{" "};
std::string _end{"]"}; std::string _end{"]"};
std::string _text_after{""}; std::string _status_text{""};
std::atomic<size_t> _max_text_after_length{0}; std::atomic<size_t> _max_status_text_length{0};
std::atomic<bool> _completed{false}; std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true}; std::atomic<bool> _show_percentage{true};
std::mutex _mutex; std::mutex _mutex;
@@ -136,8 +136,8 @@ 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 = 10; if (_max_status_text_length == 0) _max_status_text_length = 10;
std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r"; std::cout << " " << _status_text << std::string(_max_status_text_length, ' ') << "\r";
std::cout.flush(); std::cout.flush();
if (_progress > 100.0) { if (_progress > 100.0) {
_completed = true; _completed = true;

View File

@@ -15,7 +15,7 @@ int main() {
p1.fill_progress_with(""); p1.fill_progress_with("");
p1.lead_progress_with(""); p1.lead_progress_with("");
p1.fill_remainder_with(" "); p1.fill_remainder_with(" ");
p1.end_with(" ]"); p1.end_with("]");
p1.color(ProgressBar::Color::YELLOW); p1.color(ProgressBar::Color::YELLOW);
std::atomic<size_t> index1{0}; std::atomic<size_t> index1{0};
@@ -36,7 +36,7 @@ int main() {
while (true) { while (true) {
if (p1.completed()) if (p1.completed())
break; break;
p1.append_text(status_text1[index1 % status_text1.size()]); p1.set_status_text(status_text1[index1 % status_text1.size()]);
p1.set_progress(index1 * 10); p1.set_progress(index1 * 10);
index1 += 1; index1 += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(600)); std::this_thread::sleep_for(std::chrono::milliseconds(600));
@@ -55,19 +55,19 @@ int main() {
p2.lead_progress_with(">"); p2.lead_progress_with(">");
p2.fill_remainder_with(" "); p2.fill_remainder_with(" ");
p2.end_with("]"); p2.end_with("]");
p2.append_text("Getting started"); p2.set_status_text("Getting started");
p2.color(ProgressBar::Color::GREEN); p2.color(ProgressBar::Color::GREEN);
auto job2 = [&p2]() { auto job2 = [&p2]() {
while (true) { while (true) {
auto ticks = p2.current(); auto ticks = p2.current();
if (ticks > 20 && ticks < 50) if (ticks > 20 && ticks < 50)
p2.append_text("Delaying the inevitable"); p2.set_status_text("Delaying the inevitable");
else if (ticks > 50 && ticks < 80) else if (ticks > 50 && ticks < 80)
p2.append_text("Crying quietly"); p2.set_status_text("Crying quietly");
else if (ticks > 80 && ticks < 98) else if (ticks > 80 && ticks < 98)
p2.append_text("Almost there"); p2.set_status_text("Almost there");
else if (ticks >= 98) else if (ticks >= 98)
p2.append_text("Done"); p2.set_status_text("Done");
p2.tick(); p2.tick();
if (p2.completed()) if (p2.completed())
break; break;