Indicators: A Powerful C++ Library for Adding Cool Progress Bars and Indicators to Your Programs
indicators is a library designed for modern C++ that displays various progress bars and indicators in the terminal. It is ideal for tasks that require visual feedback, such as file downloads and task progress displays. Next, let’s take a look at some of the core features and usage methods of this library.
1. Basic Progress Bar
The progress bar is one of the most common features. indicators provides a simple and easy-to-use ProgressBar class that allows you to quickly create a progress bar.
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar{
indicators::option::BarWidth{50}, // Set the width of the progress bar
indicators::option::Start{"["}, // Set the starting character of the progress bar
indicators::option::Fill{"="}, // Set the filling character of the progress bar
indicators::option::Lead{">"}, // Set the leading character of the progress bar
indicators::option::Remainder{" "}, // Set the character for the remaining part
indicators::option::End{"]"}, // Set the ending character of the progress bar
indicators::option::ForegroundColor{indicators::Color::green}, // Set the foreground color
indicators::option::FontStyles{std::vector<indicators::FontStyle>{indicators::FontStyle::bold}} // Set the font style
};
while (!bar.is_completed()) {
bar.tick(); // Update progress
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}
After running this code, you will see a green progress bar gradually filling up in the terminal. The style of the progress bar can be flexibly configured through options such as color and characters.
2. Indeterminate Progress Bar
Sometimes the total progress of a task is unknown, such as downloading a file from a server that does not provide the file size. In this case, indicators provides the IndeterminateProgressBar, which will scroll indefinitely until the task is completed.
#include <indicators/indeterminate_progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::IndeterminateProgressBar bar{
indicators::option::BarWidth{40},
indicators::option::Start{"["},
indicators::option::Fill{"·"},
indicators::option::Lead{"<==>"},
indicators::option::End{"]"},
indicators::option::PostfixText{"Checking for Updates"},
indicators::option::ForegroundColor{indicators::Color::yellow}
};
// Simulate task
std::this_thread::sleep_for(std::chrono::seconds(5));
bar.mark_as_completed(); // Mark task as completed
return 0;
}
After running, you will see a continuously scrolling progress bar until the task is completed.
3. Multiple Progress Bars
If you have multiple tasks running simultaneously, indicators provides the MultiProgress class, which can display multiple progress bars at the same time.
#include <indicators/multi_progress.hpp>
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar1{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"■"},
indicators::option::Lead{"■"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PrefixText{"Task 1"},
indicators::option::ForegroundColor{indicators::Color::yellow}
};
indicators::ProgressBar bar2{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"="},
indicators::option::Lead{">"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PrefixText{"Task 2"},
indicators::option::ForegroundColor{indicators::Color::cyan}
};
indicators::MultiProgress<indicators::ProgressBar, 2> bars(bar1, bar2);
// Simulate tasks
std::thread t1([&bars]() {
while (!bars.is_completed<0>()) {
bars.tick<0>();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
std::thread t2([&bars]() {
while (!bars.is_completed<1>()) {
bars.tick<1>();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
});
t1.join();
t2.join();
return 0;
}
This code will display two progress bars simultaneously, with each progress bar updating independently.
4. Dynamic Progress Bars
Sometimes the number of tasks changes dynamically, and indicators provides the DynamicProgress class, which allows you to dynamically add or remove progress bars.
#include <indicators/dynamic_progress.hpp>
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
auto bar1 = std::make_unique<indicators::ProgressBar>(
indicators::option::BarWidth{50},
indicators::option::PrefixText{"Task 1"},
indicators::option::ForegroundColor{indicators::Color::red}
);
auto bar2 = std::make_unique<indicators::ProgressBar>(
indicators::option::BarWidth{50},
indicators::option::PrefixText{"Task 2"},
indicators::option::ForegroundColor{indicators::Color::green}
);
indicators::DynamicProgress<indicators::ProgressBar> bars(std::move(bar1), std::move(bar2));
// Simulate tasks
std::thread t1([&bars]() {
while (!bars[0].is_completed()) {
bars[0].tick();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
std::thread t2([&bars]() {
while (!bars[1].is_completed()) {
bars[1].tick();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
});
t1.join();
t2.join();
return 0;
}
After running, you will see two progress bars updating dynamically.
5. Progress Spinner
In addition to progress bars, indicators also provides a progress spinner (ProgressSpinner), which displays task progress through rotating characters.
#include <indicators/progress_spinner.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressSpinner spinner{
indicators::option::PostfixText{"Loading"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::SpinnerStates{std::vector<std::string>{"-", "\\", "|", "/"}} // Custom rotating characters
};
// Simulate task
std::this_thread::sleep_for(std::chrono::seconds(5));
spinner.mark_as_completed(); // Mark task as completed
return 0;
}
After running, you will see a rotating character until the task is completed.
Tips
indicatorsrequires support for C++17 or higher.- In some terminals, Unicode characters may not display correctly, so ensure that the terminal supports Unicode.
Conclusion
indicators is a powerful and flexible C++ library that can add various progress bars and indicators to your programs. Whether it’s a simple progress bar or a complex multi-task progress display, it can handle it with ease. With simple configuration and code, you can make your program’s interaction more friendly and intuitive.