API changes for readability/clarity

This commit is contained in:
Pranav Srinivas Kumar
2019-12-04 11:18:52 -06:00
parent b0f8107eb3
commit 30c943d55d
2 changed files with 72 additions and 65 deletions

View File

@@ -10,46 +10,51 @@ class ProgressBar {
public:
enum class Color { GREY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
void color(Color color) {
void set_foreground_color(Color color) {
std::unique_lock<std::mutex> lock{_mutex};
_color = color;
_foreground_color = color;
}
void bar_width(size_t bar_width) {
void set_bar_width(size_t bar_width) {
std::unique_lock<std::mutex> lock{_mutex};
_bar_width = bar_width;
}
void start_with(const std::string &start) {
void start_bar_with(const std::string &start) {
std::unique_lock<std::mutex> lock{_mutex};
_start = start;
}
void fill_progress_with(const std::string &fill) {
void fill_bar_progress_with(const std::string &fill) {
std::unique_lock<std::mutex> lock{_mutex};
_fill = fill;
}
void lead_progress_with(const std::string &lead) {
void lead_bar_progress_with(const std::string &lead) {
std::unique_lock<std::mutex> lock{_mutex};
_lead = lead;
}
void fill_remainder_with(const std::string &remainder) {
void fill_bar_remainder_with(const std::string &remainder) {
std::unique_lock<std::mutex> lock{_mutex};
_remainder = remainder;
}
void end_with(const std::string &end) {
void end_bar_with(const std::string &end) {
std::unique_lock<std::mutex> lock{_mutex};
_end = end;
}
void set_status_text(const std::string& text) {
void set_prefix_text(const std::string& text) {
std::unique_lock<std::mutex> lock{_mutex};
_status_text = text;
if (_status_text.length() > _max_status_text_length)
_max_status_text_length = _status_text.length();
_prefix_text = text;
}
void set_postfix_text(const std::string& text) {
std::unique_lock<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
}
void show_percentage(bool flag) { _show_percentage = flag; }
@@ -80,23 +85,23 @@ public:
private:
float _progress{0.0};
size_t _bar_width{100};
std::string _text_before{""};
std::string _prefix_text{""};
std::string _start{"["};
std::string _fill{"="};
std::string _lead{">"};
std::string _remainder{" "};
std::string _end{"]"};
std::string _status_text{""};
std::atomic<size_t> _max_status_text_length{0};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::mutex _mutex;
Color _color;
Color _foreground_color;
void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex};
std::cout << termcolor::bold;
switch(_color) {
switch(_foreground_color) {
case Color::GREY:
std::cout << termcolor::grey;
break;
@@ -122,6 +127,7 @@ private:
std::cout << termcolor::white;
break;
}
std::cout << _prefix_text;
std::cout << _start;
float pos = _progress * static_cast<float>(_bar_width) / 100.0;
for (size_t i = 0; i < _bar_width; ++i) {
@@ -136,8 +142,8 @@ private:
if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
if (_max_status_text_length == 0) _max_status_text_length = 10;
std::cout << " " << _status_text << std::string(_max_status_text_length, ' ') << "\r";
if (_max_postfix_text_length == 0) _max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
std::cout.flush();
if (_progress > 100.0) {
_completed = true;

View File

@@ -50,13 +50,13 @@ int main() {
// PROGRESS BAR 1
//
ProgressBar p1;
p1.bar_width(50);
p1.start_with("[");
p1.fill_progress_with("");
p1.lead_progress_with("");
p1.fill_remainder_with(" ");
p1.end_with("]");
p1.color(ProgressBar::Color::YELLOW);
p1.set_bar_width(50);
p1.start_bar_with("[");
p1.fill_bar_progress_with("");
p1.lead_bar_progress_with("");
p1.fill_bar_remainder_with(" ");
p1.end_bar_with("]");
p1.set_foreground_color(ProgressBar::Color::YELLOW);
std::atomic<size_t> index1{0};
std::vector<std::string> status_text1 = {"Rocket.exe is not responding",
@@ -73,7 +73,7 @@ int main() {
while (true) {
if (p1.completed())
break;
p1.set_status_text(status_text1[index1 % status_text1.size()]);
p1.set_postfix_text(status_text1[index1 % status_text1.size()]);
p1.set_progress(index1 * 10);
index1 += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(600));
@@ -86,20 +86,21 @@ int main() {
// PROGRESS BAR 3
//
ProgressBar p3;
p3.bar_width(40);
p3.start_with("Reading package list... ");
p3.fill_progress_with("");
p3.lead_progress_with("");
p3.fill_remainder_with("");
p3.end_with("");
p3.color(ProgressBar::Color::WHITE);
p3.set_bar_width(40);
p3.set_prefix_text("Reading package list... ");
p3.start_bar_with("");
p3.fill_bar_progress_with("");
p3.lead_bar_progress_with("");
p3.fill_bar_remainder_with("");
p3.end_bar_with("");
p3.set_foreground_color(ProgressBar::Color::WHITE);
p3.show_percentage(false);
auto job3 = [&p3]() {
while (true) {
p3.start_with("Reading package list... " + std::to_string(p3.current()) +
p3.set_prefix_text("Reading package list... " + std::to_string(p3.current()) +
"% ");
if (p3.current() + 2 >= 100)
p3.start_with("Reading package list... Done");
p3.set_prefix_text("Reading package list... Done");
p3.tick();
if (p3.completed()) {
break;
@@ -114,25 +115,25 @@ int main() {
// PROGRESS BAR 2
//
ProgressBar p2;
p2.bar_width(50);
p2.start_with("[");
p2.fill_progress_with("=");
p2.lead_progress_with(">");
p2.fill_remainder_with(" ");
p2.end_with("]");
p2.set_status_text("Getting started");
p2.color(ProgressBar::Color::GREEN);
p2.set_bar_width(50);
p2.start_bar_with("[");
p2.fill_bar_progress_with("=");
p2.lead_bar_progress_with(">");
p2.fill_bar_remainder_with(" ");
p2.end_bar_with("]");
p2.set_postfix_text("Getting started");
p2.set_foreground_color(ProgressBar::Color::GREEN);
auto job2 = [&p2]() {
while (true) {
auto ticks = p2.current();
if (ticks > 20 && ticks < 50)
p2.set_status_text("Delaying the inevitable");
p2.set_postfix_text("Delaying the inevitable");
else if (ticks > 50 && ticks < 80)
p2.set_status_text("Crying quietly");
p2.set_postfix_text("Crying quietly");
else if (ticks > 80 && ticks < 98)
p2.set_status_text("Almost there");
p2.set_postfix_text("Almost there");
else if (ticks >= 98)
p2.set_status_text("Done");
p2.set_postfix_text("Done");
p2.tick();
if (p2.completed())
break;
@@ -146,14 +147,14 @@ int main() {
// PROGRESS BAR 5
//
ProgressBar p5;
p5.bar_width(100);
p5.start_with("🌏");
p5.fill_progress_with("-");
p5.lead_progress_with("🛸");
p5.fill_remainder_with(" ");
p5.end_with("🌑");
p5.set_bar_width(100);
p5.start_bar_with("🌏");
p5.fill_bar_progress_with("-");
p5.lead_bar_progress_with("🛸");
p5.fill_bar_remainder_with(" ");
p5.end_bar_with("🌑");
p5.show_percentage(true);
p5.color(ProgressBar::Color::WHITE);
p5.set_foreground_color(ProgressBar::Color::WHITE);
auto job5 = [&p5]() {
while (true) {
p5.tick();
@@ -172,21 +173,21 @@ int main() {
std::vector<std::string> lead_spinner{"", "", "", "", "",
"", "", "", "", ""};
ProgressBar p4;
p4.bar_width(50);
p4.start_with("[");
p4.fill_progress_with("");
p4.lead_progress_with("");
p4.fill_remainder_with(" ");
p4.end_with(" ]");
p4.color(ProgressBar::Color::CYAN);
p4.set_status_text("Restoring system state");
p4.set_bar_width(50);
p4.start_bar_with("[");
p4.fill_bar_progress_with("");
p4.lead_bar_progress_with("");
p4.fill_bar_remainder_with(" ");
p4.end_bar_with(" ]");
p4.set_foreground_color(ProgressBar::Color::CYAN);
p4.set_postfix_text("Restoring system state");
std::atomic<size_t> index4{0};
auto job4 = [&p4, &index4, &lead_spinner]() {
while (true) {
p4.lead_progress_with(lead_spinner[index4 % lead_spinner.size()]);
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
index4 += 1;
if (p4.current() + 2 >= 100)
p4.set_status_text("State restored to Restore_Point_1241531");
p4.set_postfix_text("State restored to Restore_Point_1241531");
p4.tick();
if (p4.completed()) {
break;