mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-13 02:28:53 +08:00
Clang format
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
find ./include ./samples/ -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i
|
find ./include ./demo/ -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i
|
||||||
|
|||||||
348
demo/demo.cpp
348
demo/demo.cpp
@@ -22,27 +22,27 @@ int main() {
|
|||||||
|
|
||||||
std::atomic<size_t> index{0};
|
std::atomic<size_t> index{0};
|
||||||
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
std::vector<std::string> status_text = {"Rocket.exe is not responding",
|
||||||
"Buying more snacks",
|
"Buying more snacks",
|
||||||
"Finding a replacement engineer",
|
"Finding a replacement engineer",
|
||||||
"Assimilating the modding community",
|
"Assimilating the modding community",
|
||||||
"Crossing fingers",
|
"Crossing fingers",
|
||||||
"Porting KSP to a Nokia 3310",
|
"Porting KSP to a Nokia 3310",
|
||||||
"Flexing struts",
|
"Flexing struts",
|
||||||
"Releasing space whales",
|
"Releasing space whales",
|
||||||
"Watching paint dry"};
|
"Watching paint dry"};
|
||||||
|
|
||||||
auto job = [&p, &index, &status_text]() {
|
auto job = [&p, &index, &status_text]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
p.set_postfix_text(status_text[index % status_text.size()]);
|
p.set_postfix_text(status_text[index % status_text.size()]);
|
||||||
p.set_progress(index * 10);
|
p.set_progress(index * 10);
|
||||||
index += 1;
|
index += 1;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -60,18 +60,17 @@ int main() {
|
|||||||
p.set_foreground_color(indica::Color::WHITE);
|
p.set_foreground_color(indica::Color::WHITE);
|
||||||
p.hide_percentage();
|
p.hide_percentage();
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p.set_prefix_text("Reading package list... " +
|
p.set_prefix_text("Reading package list... " + std::to_string(p.current()) + "% ");
|
||||||
std::to_string(p.current()) + "% ");
|
if (p.current() + 2 >= 100)
|
||||||
if (p.current() + 2 >= 100)
|
p.set_prefix_text("Reading package list... Done");
|
||||||
p.set_prefix_text("Reading package list... Done");
|
p.tick();
|
||||||
p.tick();
|
if (p.is_completed()) {
|
||||||
if (p.is_completed()) {
|
break;
|
||||||
break;
|
}
|
||||||
}
|
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
@@ -90,33 +89,31 @@ int main() {
|
|||||||
p.set_postfix_text("Getting started");
|
p.set_postfix_text("Getting started");
|
||||||
p.set_foreground_color(indica::Color::GREEN);
|
p.set_foreground_color(indica::Color::GREEN);
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p.current();
|
auto ticks = p.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p.set_postfix_text("Delaying the inevitable");
|
p.set_postfix_text("Delaying the inevitable");
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p.set_postfix_text("Crying quietly");
|
p.set_postfix_text("Crying quietly");
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p.set_postfix_text("Almost there");
|
p.set_postfix_text("Almost there");
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p.set_postfix_text("Done");
|
p.set_postfix_text("Done");
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// PROGRESS BAR 4
|
// PROGRESS BAR 4
|
||||||
//
|
//
|
||||||
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼",
|
std::vector<std::string> lead_spinner{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||||
"⠴", "⠦", "⠧", "⠇", "⠏"};
|
|
||||||
indica::ProgressBar p4;
|
indica::ProgressBar p4;
|
||||||
p4.set_bar_width(40);
|
p4.set_bar_width(40);
|
||||||
p4.start_bar_with("");
|
p4.start_bar_with("");
|
||||||
@@ -129,28 +126,28 @@ int main() {
|
|||||||
p4.hide_percentage();
|
p4.hide_percentage();
|
||||||
std::atomic<size_t> index4{0};
|
std::atomic<size_t> index4{0};
|
||||||
auto job4 = [&p4, &index4, &lead_spinner]() {
|
auto job4 = [&p4, &index4, &lead_spinner]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
|
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
|
||||||
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
|
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
|
||||||
index4 += 1;
|
index4 += 1;
|
||||||
if (p4.current() + 2 >= 100) {
|
if (p4.current() + 2 >= 100) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p4.set_foreground_color(indica::Color::RED);
|
p4.set_foreground_color(indica::Color::RED);
|
||||||
p4.set_prefix_text("{ ERROR }");
|
p4.set_prefix_text("{ ERROR }");
|
||||||
p4.start_bar_with("");
|
p4.start_bar_with("");
|
||||||
p4.fill_bar_progress_with("");
|
p4.fill_bar_progress_with("");
|
||||||
p4.lead_bar_progress_with("");
|
p4.lead_bar_progress_with("");
|
||||||
p4.fill_bar_remainder_with("");
|
p4.fill_bar_remainder_with("");
|
||||||
p4.end_bar_with("");
|
p4.end_bar_with("");
|
||||||
p4.show_percentage();
|
p4.show_percentage();
|
||||||
p4.set_postfix_text("Failed to restore system");
|
p4.set_postfix_text("Failed to restore system");
|
||||||
p4.mark_as_completed();
|
p4.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
p4.tick();
|
p4.tick();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread4(job4);
|
std::thread thread4(job4);
|
||||||
thread4.join();
|
thread4.join();
|
||||||
|
|
||||||
@@ -172,47 +169,46 @@ int main() {
|
|||||||
p.set_postfix_text("Reverting system restore");
|
p.set_postfix_text("Reverting system restore");
|
||||||
std::atomic<size_t> progress{100};
|
std::atomic<size_t> progress{100};
|
||||||
auto job = [&p, &progress]() {
|
auto job = [&p, &progress]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
progress -= 1;
|
progress -= 1;
|
||||||
p.set_progress(progress);
|
p.set_progress(progress);
|
||||||
if (progress == 0) {
|
if (progress == 0) {
|
||||||
p.set_postfix_text("Revert complete!");
|
p.set_postfix_text("Revert complete!");
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// PROGRESS BAR 5
|
// PROGRESS BAR 5
|
||||||
//
|
//
|
||||||
indica::ProgressSpinner p;
|
indica::ProgressSpinner p;
|
||||||
p.set_prefix_text("");
|
p.set_prefix_text("");
|
||||||
p.set_postfix_text("Checking credentials");
|
p.set_postfix_text("Checking credentials");
|
||||||
p.set_foreground_color(indica::Color::YELLOW);
|
p.set_foreground_color(indica::Color::YELLOW);
|
||||||
p.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
p.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (p.is_completed()) {
|
if (p.is_completed()) {
|
||||||
p.set_foreground_color(indica::Color::GREEN);
|
p.set_foreground_color(indica::Color::GREEN);
|
||||||
p.set_prefix_text("✔");
|
p.set_prefix_text("✔");
|
||||||
p.hide_spinner();
|
p.hide_spinner();
|
||||||
p.hide_percentage();
|
p.hide_percentage();
|
||||||
p.set_postfix_text("Authenticated!");
|
p.set_postfix_text("Authenticated!");
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
p.tick();
|
p.tick();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
@@ -221,7 +217,7 @@ int main() {
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
// PROGRESS BAR 6
|
// PROGRESS BAR 6
|
||||||
//
|
//
|
||||||
indica::ProgressSpinner p;
|
indica::ProgressSpinner p;
|
||||||
p.set_prefix_text(" - ");
|
p.set_prefix_text(" - ");
|
||||||
p.set_postfix_text("Searching for the Moon");
|
p.set_postfix_text("Searching for the Moon");
|
||||||
@@ -229,82 +225,82 @@ int main() {
|
|||||||
p.set_spinner_states({"▖", "▘", "▝", "▗"});
|
p.set_spinner_states({"▖", "▘", "▝", "▗"});
|
||||||
p.hide_percentage();
|
p.hide_percentage();
|
||||||
auto job = [&p]() {
|
auto job = [&p]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto current = p.current();
|
auto current = p.current();
|
||||||
if (current == 24) {
|
if (current == 24) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_prefix_text(" - ✔");
|
||||||
p.hide_spinner();
|
p.hide_spinner();
|
||||||
} else if (current == 25) {
|
} else if (current == 25) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.show_spinner();
|
||||||
p.set_prefix_text(" - ");
|
p.set_prefix_text(" - ");
|
||||||
p.set_postfix_text("Contacting Kerbal headquarters");
|
p.set_postfix_text("Contacting Kerbal headquarters");
|
||||||
} else if (current == 49) {
|
} else if (current == 49) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_prefix_text(" - ✔");
|
||||||
p.hide_spinner();
|
p.hide_spinner();
|
||||||
} else if (current == 50) {
|
} else if (current == 50) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.show_spinner();
|
||||||
p.set_prefix_text(" - ");
|
p.set_prefix_text(" - ");
|
||||||
p.set_postfix_text("Designing spaceship");
|
p.set_postfix_text("Designing spaceship");
|
||||||
} else if (current == 74) {
|
} else if (current == 74) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_prefix_text(" - ✔");
|
||||||
p.hide_spinner();
|
p.hide_spinner();
|
||||||
} else if (current == 75) {
|
} else if (current == 75) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
p.show_spinner();
|
p.show_spinner();
|
||||||
p.set_prefix_text(" - ");
|
p.set_prefix_text(" - ");
|
||||||
p.set_postfix_text("Launching rocket");
|
p.set_postfix_text("Launching rocket");
|
||||||
} else if (current == 95) {
|
} else if (current == 95) {
|
||||||
p.set_prefix_text(" - ✔");
|
p.set_prefix_text(" - ✔");
|
||||||
p.hide_spinner();
|
p.hide_spinner();
|
||||||
} else if (current == 99) {
|
} else if (current == 99) {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
//
|
//
|
||||||
// NESTED PROGRESS BAR
|
// NESTED PROGRESS BAR
|
||||||
//
|
//
|
||||||
indica::ProgressBar p2;
|
indica::ProgressBar p2;
|
||||||
p2.set_bar_width(30);
|
p2.set_bar_width(30);
|
||||||
p2.set_prefix_text(" - ");
|
p2.set_prefix_text(" - ");
|
||||||
p2.start_bar_with("🌎");
|
p2.start_bar_with("🌎");
|
||||||
p2.fill_bar_progress_with("·");
|
p2.fill_bar_progress_with("·");
|
||||||
p2.lead_bar_progress_with("🚀");
|
p2.lead_bar_progress_with("🚀");
|
||||||
p2.fill_bar_remainder_with(" ");
|
p2.fill_bar_remainder_with(" ");
|
||||||
p2.end_bar_with("🌑");
|
p2.end_bar_with("🌑");
|
||||||
p2.set_postfix_text("Achieved low-Earth orbit");
|
p2.set_postfix_text("Achieved low-Earth orbit");
|
||||||
p2.set_foreground_color(indica::Color::WHITE);
|
p2.set_foreground_color(indica::Color::WHITE);
|
||||||
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
std::vector<std::string> ship_trail{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
||||||
std::atomic<int> ship_trail_index{0};
|
std::atomic<int> ship_trail_index{0};
|
||||||
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
auto ticks = p2.current();
|
auto ticks = p2.current();
|
||||||
if (ticks > 20 && ticks < 50)
|
if (ticks > 20 && ticks < 50)
|
||||||
p2.set_postfix_text("Switching to trans-lunar trajectory");
|
p2.set_postfix_text("Switching to trans-lunar trajectory");
|
||||||
else if (ticks > 50 && ticks < 80)
|
else if (ticks > 50 && ticks < 80)
|
||||||
p2.set_postfix_text("Transferred to Lunar lander");
|
p2.set_postfix_text("Transferred to Lunar lander");
|
||||||
else if (ticks > 80 && ticks < 98)
|
else if (ticks > 80 && ticks < 98)
|
||||||
p2.set_postfix_text("Almost there");
|
p2.set_postfix_text("Almost there");
|
||||||
else if (ticks >= 98)
|
else if (ticks >= 98)
|
||||||
p2.set_postfix_text("Landed on the Moon");
|
p2.set_postfix_text("Landed on the Moon");
|
||||||
p2.tick();
|
p2.tick();
|
||||||
ship_trail_index += 1;
|
ship_trail_index += 1;
|
||||||
if (p2.is_completed())
|
if (p2.is_completed())
|
||||||
break;
|
break;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(80));
|
std::this_thread::sleep_for(std::chrono::milliseconds(80));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread2(job2);
|
std::thread thread2(job2);
|
||||||
thread2.join();
|
thread2.join();
|
||||||
p.set_postfix_text("Mission successful!");
|
p.set_postfix_text("Mission successful!");
|
||||||
p.mark_as_completed();
|
p.mark_as_completed();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
p.tick();
|
p.tick();
|
||||||
if (p.is_completed())
|
if (p.is_completed())
|
||||||
break;
|
break;
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread thread(job);
|
std::thread thread(job);
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,5 +28,5 @@ SOFTWARE.
|
|||||||
#include <indica/termcolor.hpp>
|
#include <indica/termcolor.hpp>
|
||||||
|
|
||||||
namespace indica {
|
namespace indica {
|
||||||
enum class Color { GREY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
|
enum class Color { GREY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,4 +187,4 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace indica
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public:
|
|||||||
|
|
||||||
void show_spinner() { _show_spinner = true; }
|
void show_spinner() { _show_spinner = true; }
|
||||||
|
|
||||||
void hide_spinner() { _show_spinner = false; }
|
void hide_spinner() { _show_spinner = false; }
|
||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
{
|
{
|
||||||
@@ -90,21 +90,21 @@ public:
|
|||||||
_print_progress();
|
_print_progress();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_spinner_states(const std::vector<std::string>& states) {
|
void set_spinner_states(const std::vector<std::string> &states) {
|
||||||
std::unique_lock<std::mutex> lock{_mutex};
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
_states = states;
|
_states = states;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
std::string _prefix_text{""};
|
std::string _prefix_text{""};
|
||||||
size_t _index{0};
|
size_t _index{0};
|
||||||
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||||
std::string _postfix_text{""};
|
std::string _postfix_text{""};
|
||||||
std::atomic<size_t> _max_postfix_text_length{0};
|
std::atomic<size_t> _max_postfix_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::atomic<bool> _show_spinner{true};
|
std::atomic<bool> _show_spinner{true};
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
Color _foreground_color;
|
Color _foreground_color;
|
||||||
|
|
||||||
@@ -156,4 +156,4 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace indica
|
||||||
|
|||||||
Reference in New Issue
Block a user