mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-12 01:18:52 +08:00
Added progress bar samples
This commit is contained in:
63
samples/multi_threaded_bar.cpp
Normal file
63
samples/multi_threaded_bar.cpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
indicators::ProgressBar bar;
|
||||||
|
bar.set_bar_width(50);
|
||||||
|
bar.start_bar_with("[");
|
||||||
|
bar.fill_bar_progress_with("■");
|
||||||
|
bar.lead_bar_progress_with("■");
|
||||||
|
bar.fill_bar_remainder_with("-");
|
||||||
|
bar.end_bar_with("]");
|
||||||
|
bar.set_foreground_color(indicators::Color::YELLOW);
|
||||||
|
|
||||||
|
// As configured, the bar will look like this:
|
||||||
|
//
|
||||||
|
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
std::atomic<size_t> index{0};
|
||||||
|
std::vector<std::string> status_text =
|
||||||
|
{
|
||||||
|
"Rocket.exe is not responding",
|
||||||
|
"Finding a replacement engineer",
|
||||||
|
"Buying more snacks",
|
||||||
|
"Assimilating the modding community",
|
||||||
|
"Crossing fingers",
|
||||||
|
"Porting KSP to a Nokia 3310"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Let's say you want to append some status text to the right of the progress bar
|
||||||
|
// You can use bar.set_postfix_text(...) to append text to the right
|
||||||
|
//
|
||||||
|
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70% Finding a replacement engineer
|
||||||
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
auto job = [&bar, &index, &status_text]() {
|
||||||
|
while (true) {
|
||||||
|
if (bar.is_completed()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bar.set_postfix_text(status_text[index % status_text.size()]);
|
||||||
|
bar.tick();
|
||||||
|
index += 1;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::thread first_job(job);
|
||||||
|
std::thread second_job(job);
|
||||||
|
std::thread third_job(job);
|
||||||
|
std::thread last_job(job);
|
||||||
|
|
||||||
|
first_job.join();
|
||||||
|
second_job.join();
|
||||||
|
third_job.join();
|
||||||
|
last_job.join();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
38
samples/progress_bar_set_progress.cpp
Normal file
38
samples/progress_bar_set_progress.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
indicators::ProgressBar bar;
|
||||||
|
|
||||||
|
// Configure the bar
|
||||||
|
bar.set_bar_width(50);
|
||||||
|
bar.start_bar_with("[");
|
||||||
|
bar.fill_bar_progress_with("=");
|
||||||
|
bar.lead_bar_progress_with(">");
|
||||||
|
bar.fill_bar_remainder_with(" ");
|
||||||
|
bar.end_bar_with("]");
|
||||||
|
bar.set_postfix_text("Getting started");
|
||||||
|
bar.set_foreground_color(indicators::Color::GREEN);
|
||||||
|
|
||||||
|
// Update bar state
|
||||||
|
|
||||||
|
bar.set_progress(10); // 10% done
|
||||||
|
|
||||||
|
// do some work
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
|
bar.set_progress(30); // 30% done
|
||||||
|
|
||||||
|
// do some more work
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||||
|
|
||||||
|
bar.set_progress(65); // 65% done
|
||||||
|
|
||||||
|
// do final bit of work
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||||
|
|
||||||
|
bar.set_progress(100); // all done
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
samples/progress_bar_tick.cpp
Normal file
27
samples/progress_bar_tick.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
indicators::ProgressBar bar;
|
||||||
|
|
||||||
|
// Configure the bar
|
||||||
|
bar.set_bar_width(50);
|
||||||
|
bar.start_bar_with("[");
|
||||||
|
bar.fill_bar_progress_with("=");
|
||||||
|
bar.lead_bar_progress_with(">");
|
||||||
|
bar.fill_bar_remainder_with(" ");
|
||||||
|
bar.end_bar_with("]");
|
||||||
|
bar.set_postfix_text("Getting started");
|
||||||
|
bar.set_foreground_color(indicators::Color::GREEN);
|
||||||
|
|
||||||
|
// Update bar state
|
||||||
|
while (true) {
|
||||||
|
bar.tick();
|
||||||
|
if (bar.is_completed())
|
||||||
|
break;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user