Files
indicators/demo/demo.cpp

302 lines
9.0 KiB
C++
Raw Normal View History

2019-12-04 15:03:52 -06:00
#include <indica/progress_bar.hpp>
#include <indica/progress_spinner.hpp>
2019-12-03 22:53:38 -06:00
#include <vector>
int main() {
2019-12-03 23:51:46 -06:00
// Hide cursor
std::cout << "\e[?25l";
2019-12-04 19:54:13 -06:00
{
//
// PROGRESS BAR 1
//
indica::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_foreground_color(indica::Color::YELLOW);
2019-12-03 22:53:38 -06:00
2019-12-04 19:54:13 -06:00
std::atomic<size_t> index{0};
std::vector<std::string> status_text = {"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-04 09:40:09 -06:00
2019-12-04 19:54:13 -06:00
auto job = [&p, &index, &status_text]() {
while (true) {
if (p.is_completed())
break;
p.set_postfix_text(status_text[index % status_text.size()]);
p.set_progress(index * 10);
index += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
};
std::thread thread(job);
thread.join();
}
2019-12-03 22:53:38 -06:00
2019-12-04 09:37:22 -06:00
//
// PROGRESS BAR 3
//
2019-12-04 15:03:52 -06:00
indica::ProgressBar p3;
2019-12-04 11:18:52 -06:00
p3.set_bar_width(40);
2019-12-04 19:54:13 -06:00
p3.set_prefix_text("Reading package list... ");
2019-12-04 11:18:52 -06:00
p3.start_bar_with("");
p3.fill_bar_progress_with("");
p3.lead_bar_progress_with("");
p3.fill_bar_remainder_with("");
p3.end_bar_with("");
2019-12-04 15:03:52 -06:00
p3.set_foreground_color(indica::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 19:54:13 -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 19:54:13 -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
//
2019-12-04 15:03:52 -06:00
indica::ProgressBar p2;
2019-12-04 11:18:52 -06:00
p2.set_bar_width(50);
2019-12-04 19:54:13 -06:00
p2.start_bar_with("[");
2019-12-04 11:18:52 -06:00
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");
2019-12-04 15:03:52 -06:00
p2.set_foreground_color(indica::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
//
std::vector<std::string> lead_spinner{"", "", "", "", "",
"", "", "", "", ""};
2019-12-04 15:03:52 -06:00
indica::ProgressBar p4;
p4.set_bar_width(40);
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 15:03:52 -06:00
p4.set_foreground_color(indica::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) {
2019-12-04 19:54:13 -06:00
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) {
std::cout << std::endl;
2019-12-04 15:03:52 -06:00
p4.set_foreground_color(indica::Color::RED);
2019-12-04 19:54:13 -06:00
p4.set_prefix_text("{ ERROR }");
p4.start_bar_with("");
p4.fill_bar_progress_with("");
p4.lead_bar_progress_with("");
p4.fill_bar_remainder_with("");
p4.end_bar_with("");
2019-12-04 11:35:48 -06:00
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
//
2019-12-04 15:03:52 -06:00
indica::ProgressBar p;
2019-12-04 12:58:04 -06:00
p.set_bar_width(50);
2019-12-04 19:54:13 -06:00
p.start_bar_with("[");
2019-12-04 12:58:04 -06:00
p.fill_bar_progress_with("");
p.lead_bar_progress_with("");
2019-12-04 13:02:59 -06:00
p.fill_bar_remainder_with("-");
2019-12-04 12:58:04 -06:00
p.end_bar_with("]");
p.set_progress(100);
2019-12-04 15:03:52 -06:00
p.set_foreground_color(indica::Color::WHITE);
2019-12-04 12:58:04 -06:00
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 13:16:46 -06:00
{
indica::ProgressSpinner p;
2019-12-04 19:54:13 -06:00
p.set_prefix_text("");
2019-12-04 13:16:46 -06:00
p.set_postfix_text("Checking credentials");
2019-12-04 15:03:52 -06:00
p.set_foreground_color(indica::Color::YELLOW);
2019-12-04 13:52:14 -06:00
p.set_spinner_states({"", "", "", "", "", "", "", ""});
auto job = [&p]() {
2019-12-04 13:16:46 -06:00
while (true) {
2019-12-04 13:52:14 -06:00
if (p.is_completed()) {
2019-12-04 15:03:52 -06:00
p.set_foreground_color(indica::Color::GREEN);
2019-12-04 19:54:13 -06:00
p.set_prefix_text("");
p.hide_spinner();
2019-12-04 13:16:46 -06:00
p.hide_percentage();
p.set_postfix_text("Authenticated!");
p.mark_as_completed();
break;
} else
p.tick();
std::this_thread::sleep_for(std::chrono::milliseconds(40));
2019-12-04 13:16:46 -06:00
}
};
std::thread thread(job);
thread.join();
}
2019-12-04 13:16:46 -06:00
std::cout << " Compiling mission\n";
2019-12-04 14:39:55 -06:00
{
2019-12-04 15:03:52 -06:00
indica::ProgressSpinner p;
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ");
p.set_postfix_text("Searching for the Moon");
p.set_foreground_color(indica::Color::WHITE);
2019-12-04 14:39:55 -06:00
p.set_spinner_states({"", "", "", ""});
2019-12-04 14:45:59 -06:00
p.hide_percentage();
2019-12-04 14:39:55 -06:00
auto job = [&p]() {
while (true) {
auto current = p.current();
if (current == 24) {
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ✔");
p.hide_spinner();
} else if (current == 25) {
std::cout << std::endl;
p.show_spinner();
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ");
p.set_postfix_text("Contacting Kerbal headquarters");
} else if (current == 49) {
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ✔");
p.hide_spinner();
} else if (current == 50) {
std::cout << std::endl;
p.show_spinner();
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ");
p.set_postfix_text("Designing spaceship");
} else if (current == 74) {
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ✔");
p.hide_spinner();
} else if (current == 75) {
std::cout << std::endl;
p.show_spinner();
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ");
p.set_postfix_text("Launching rocket");
} else if (current == 95) {
2019-12-04 19:54:13 -06:00
p.set_prefix_text(" - ✔");
p.hide_spinner();
} else if (current == 99) {
std::cout << std::endl;
//
// NESTED PROGRESS BAR
//
indica::ProgressBar p2;
p2.set_bar_width(30);
2019-12-04 19:54:13 -06:00
p2.set_prefix_text(" - ");
2019-12-04 18:32:10 -06:00
p2.start_bar_with("🌎");
p2.fill_bar_progress_with("·");
p2.lead_bar_progress_with("🚀");
p2.fill_bar_remainder_with(" ");
2019-12-04 18:32:10 -06:00
p2.end_bar_with("🌑");
p2.set_postfix_text("Achieved low-Earth orbit");
p2.set_foreground_color(indica::Color::WHITE);
std::vector<std::string> ship_trail{"", "", "", "", "", "", "", ""};
std::atomic<int> ship_trail_index{0};
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
while (true) {
auto ticks = p2.current();
if (ticks > 20 && ticks < 50)
p2.set_postfix_text("Switching to trans-lunar trajectory");
else if (ticks > 50 && ticks < 80)
2019-12-04 18:32:10 -06:00
p2.set_postfix_text("Transferred to Lunar lander");
else if (ticks > 80 && ticks < 98)
p2.set_postfix_text("Almost there");
else if (ticks >= 98)
p2.set_postfix_text("Landed on the Moon");
p2.tick();
ship_trail_index += 1;
if (p2.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(80));
}
};
std::thread thread2(job2);
thread2.join();
p.set_postfix_text("Mission successful!");
p.mark_as_completed();
break;
}
p.tick();
if (p.is_completed())
break;
2019-12-04 15:02:02 -06:00
std::this_thread::sleep_for(std::chrono::milliseconds(60));
2019-12-04 14:39:55 -06:00
}
};
std::thread thread(job);
thread.join();
}
2019-12-04 14:39:55 -06:00
2019-12-03 23:51:46 -06:00
// Show cursor
std::cout << "\e[?25h";
2019-12-03 22:53:38 -06:00
return 0;
}