Files
indicators/samples/multiple_bars.cpp

218 lines
6.3 KiB
C++
Raw Normal View History

2019-12-04 11:47:04 -06:00
#include <gauge/progressbar.hpp>
2019-12-03 22:53:38 -06:00
#include <vector>
2019-12-04 10:54:25 -06:00
/*
[
"|/-\\",
"⠂-–—–-",
"◐◓◑◒",
"◴◷◶◵",
"◰◳◲◱",
"▖▘▝▗",
"■□▪▫",
"▌▀▐▄",
"▉▊▋▌▍▎▏▎▍▌▋▊▉",
"▁▃▄▅▆▇█▇▆▅▄▃",
"←↖↑↗→↘↓↙",
"┤┘┴└├┌┬┐",
"◢◣◤◥",
".oO°Oo.",
".oO@*",
["🌍", "🌎", "🌏"],
"◡◡ ⊙⊙ ◠◠",
"☱☲☴",
"⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏",
"⠋⠙⠚⠞⠖⠦⠴⠲⠳⠓",
"⠄⠆⠇⠋⠙⠸⠰⠠⠰⠸⠙⠋⠇⠆",
"⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋",
"⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁",
"⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈",
"⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈",
"⢄⢂⢁⡁⡈⡐⡠",
"⢹⢺⢼⣸⣇⡧⡗⡏",
"⣾⣽⣻⢿⡿⣟⣯⣷",
"⠁⠂⠄⡀⢀⠠⠐⠈",
["🌑", "🌒", "🌓", "🌔", "🌕", "🌝", "🌖", "🌗", "🌘", "🌚"],
["🕛", "🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚"]
]
*/
2019-12-03 22:53:38 -06:00
int main() {
2019-12-03 23:51:46 -06:00
// Hide cursor
std::cout << "\e[?25l";
2019-12-04 11:54:30 -06:00
{}
2019-12-04 10:54:25 -06:00
2019-12-03 22:53:38 -06:00
//
// PROGRESS BAR 1
//
ProgressBar p1;
2019-12-04 11:18:52 -06:00
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);
2019-12-03 22:53:38 -06:00
std::atomic<size_t> index1{0};
2019-12-04 09:40:09 -06:00
std::vector<std::string> status_text1 = {"Rocket.exe is not responding",
"Buying more snacks",
"Finding a replacement engineer",
"Assimilating the modding community",
"Crossing fingers",
"Porting KSP to a Nokia 3310",
"Flexing struts",
"Releasing space whales",
"Watching paint dry"};
2019-12-03 22:53:38 -06:00
auto job1 = [&p1, &index1, &status_text1]() {
2019-12-04 09:40:09 -06:00
while (true) {
2019-12-04 11:19:17 -06:00
if (p1.is_completed())
2019-12-04 09:40:09 -06:00
break;
2019-12-04 11:18:52 -06:00
p1.set_postfix_text(status_text1[index1 % status_text1.size()]);
2019-12-04 09:40:09 -06:00
p1.set_progress(index1 * 10);
index1 += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
};
2019-12-03 22:53:38 -06:00
std::thread thread1(job1);
thread1.join();
2019-12-04 09:37:22 -06:00
//
// PROGRESS BAR 3
//
ProgressBar p3;
2019-12-04 11:18:52 -06:00
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.hide_percentage();
2019-12-04 09:37:22 -06:00
auto job3 = [&p3]() {
2019-12-04 09:40:09 -06:00
while (true) {
2019-12-04 11:54:30 -06:00
p3.set_prefix_text("Reading package list... " + std::to_string(p3.current()) + "% ");
2019-12-04 09:40:09 -06:00
if (p3.current() + 2 >= 100)
2019-12-04 11:18:52 -06:00
p3.set_prefix_text("Reading package list... Done");
2019-12-04 09:40:09 -06:00
p3.tick();
2019-12-04 11:19:17 -06:00
if (p3.is_completed()) {
2019-12-04 09:40:09 -06:00
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
};
std::thread thread3(job3);
2019-12-04 09:37:22 -06:00
thread3.join();
2019-12-03 22:53:38 -06:00
//
// PROGRESS BAR 2
//
ProgressBar p2;
2019-12-04 11:18:52 -06:00
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);
2019-12-03 22:53:38 -06:00
auto job2 = [&p2]() {
2019-12-04 09:40:09 -06:00
while (true) {
auto ticks = p2.current();
if (ticks > 20 && ticks < 50)
2019-12-04 11:18:52 -06:00
p2.set_postfix_text("Delaying the inevitable");
2019-12-04 09:40:09 -06:00
else if (ticks > 50 && ticks < 80)
2019-12-04 11:18:52 -06:00
p2.set_postfix_text("Crying quietly");
2019-12-04 09:40:09 -06:00
else if (ticks > 80 && ticks < 98)
2019-12-04 11:18:52 -06:00
p2.set_postfix_text("Almost there");
2019-12-04 09:40:09 -06:00
else if (ticks >= 98)
2019-12-04 11:18:52 -06:00
p2.set_postfix_text("Done");
2019-12-04 09:40:09 -06:00
p2.tick();
2019-12-04 11:19:17 -06:00
if (p2.is_completed())
2019-12-04 09:40:09 -06:00
break;
std::this_thread::sleep_for(std::chrono::milliseconds(30));
}
};
std::thread thread2(job2);
2019-12-03 22:53:38 -06:00
thread2.join();
2019-12-04 08:49:02 -06:00
//
// PROGRESS BAR 4
//
2019-12-04 11:54:30 -06:00
std::vector<std::string> lead_spinner{"", "", "", "", "", "", "", "", "", ""};
2019-12-04 08:49:02 -06:00
ProgressBar p4;
2019-12-04 11:18:52 -06:00
p4.set_bar_width(50);
2019-12-04 11:35:48 -06:00
p4.start_bar_with("");
2019-12-04 11:18:52 -06:00
p4.fill_bar_progress_with("");
p4.lead_bar_progress_with("");
p4.fill_bar_remainder_with(" ");
2019-12-04 11:35:48 -06:00
p4.end_bar_with("");
2019-12-04 12:58:04 -06:00
p4.set_foreground_color(ProgressBar::Color::CYAN);
2019-12-04 11:18:52 -06:00
p4.set_postfix_text("Restoring system state");
p4.hide_percentage();
2019-12-04 08:49:02 -06:00
std::atomic<size_t> index4{0};
auto job4 = [&p4, &index4, &lead_spinner]() {
2019-12-04 09:40:09 -06:00
while (true) {
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
2019-12-04 11:18:52 -06:00
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
2019-12-04 09:40:09 -06:00
index4 += 1;
if (p4.current() + 2 >= 100) {
p4.set_foreground_color(ProgressBar::Color::RED);
2019-12-04 11:35:48 -06:00
p4.set_prefix_text("{ CRITICAL ERROR } ");
p4.show_percentage();
p4.set_postfix_text("Failed to restore system");
p4.mark_as_completed();
2019-12-04 09:40:09 -06:00
break;
}
p4.tick();
2019-12-04 12:58:04 -06:00
std::this_thread::sleep_for(std::chrono::milliseconds(40));
2019-12-04 09:40:09 -06:00
}
};
std::thread thread4(job4);
2019-12-04 08:49:02 -06:00
thread4.join();
2019-12-04 12:58:04 -06:00
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
//
// GOING BACKWARDS
//
ProgressBar p;
p.set_bar_width(50);
p.start_bar_with("[");
p.fill_bar_progress_with("");
p.lead_bar_progress_with("");
p.fill_bar_remainder_with(" ");
p.end_bar_with("]");
p.set_progress(100);
p.set_foreground_color(ProgressBar::Color::WHITE);
p.set_postfix_text("Reverting system restore");
std::atomic<size_t> progress{100};
auto job = [&p, &progress]() {
while (true) {
progress -= 1;
p.set_progress(progress);
if (progress == 0) {
p.set_postfix_text("Revert complete!");
p.mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
};
std::thread thread(job);
thread.join();
}
2019-12-04 09:40:09 -06:00
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
2019-12-03 23:51:46 -06:00
// Show cursor
std::cout << "\e[?25h";
2019-12-03 22:53:38 -06:00
return 0;
}