mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-13 18:48:53 +08:00
Updated test example
This commit is contained in:
@@ -45,6 +45,13 @@ public:
|
|||||||
_end = end;
|
_end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void append_text(const std::string& text) {
|
||||||
|
std::unique_lock<std::mutex> lock{_mutex};
|
||||||
|
_text_after = text;
|
||||||
|
if (_text_after.length() > _max_text_after_length)
|
||||||
|
_max_text_after_length = _text_after.length();
|
||||||
|
}
|
||||||
|
|
||||||
void show_percentage(bool flag) { _show_percentage = flag; }
|
void show_percentage(bool flag) { _show_percentage = flag; }
|
||||||
|
|
||||||
void set_progress(float value) {
|
void set_progress(float value) {
|
||||||
@@ -78,11 +85,14 @@ public:
|
|||||||
private:
|
private:
|
||||||
float _progress{0.0};
|
float _progress{0.0};
|
||||||
size_t _bar_width{100};
|
size_t _bar_width{100};
|
||||||
|
std::string _text_before{""};
|
||||||
std::string _start{"["};
|
std::string _start{"["};
|
||||||
std::string _fill{"="};
|
std::string _fill{"="};
|
||||||
std::string _lead{">"};
|
std::string _lead{">"};
|
||||||
std::string _remainder{" "};
|
std::string _remainder{" "};
|
||||||
std::string _end{"]"};
|
std::string _end{"]"};
|
||||||
|
std::string _text_after{""};
|
||||||
|
std::atomic<size_t> _max_text_after_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::mutex _mutex;
|
std::mutex _mutex;
|
||||||
@@ -128,9 +138,8 @@ private:
|
|||||||
}
|
}
|
||||||
std::cout << _end;
|
std::cout << _end;
|
||||||
if (_show_percentage)
|
if (_show_percentage)
|
||||||
std::cout << " " << static_cast<int>(_progress) << "%\r";
|
std::cout << " " << static_cast<int>(_progress) << "%";
|
||||||
else
|
std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r";
|
||||||
std::cout << "\r";
|
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
if (_completed)
|
if (_completed)
|
||||||
std::cout << termcolor::reset << std::endl;
|
std::cout << termcolor::reset << std::endl;
|
||||||
|
|||||||
26
test/bar.cpp
26
test/bar.cpp
@@ -1,4 +1,5 @@
|
|||||||
#include <progress/bar.hpp>
|
#include <progress/bar.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ int main() {
|
|||||||
bar.lead_progress_with("■");
|
bar.lead_progress_with("■");
|
||||||
bar.fill_remainder_with("-");
|
bar.fill_remainder_with("-");
|
||||||
bar.end_with(" ]");
|
bar.end_with(" ]");
|
||||||
bar.color(ProgressBar::Color::GREEN);
|
bar.color(ProgressBar::Color::YELLOW);
|
||||||
|
|
||||||
// As configured, the bar will look like this:
|
// As configured, the bar will look like this:
|
||||||
//
|
//
|
||||||
@@ -19,12 +20,29 @@ int main() {
|
|||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
auto job = [&bar]() {
|
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"
|
||||||
|
"Flexing struts",
|
||||||
|
"Releasing space whales",
|
||||||
|
"Watching paint dry"
|
||||||
|
};
|
||||||
|
|
||||||
|
auto job = [&bar, &index, &status_text]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (bar.completed())
|
if (bar.completed()) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
bar.append_text(status_text[index % status_text.size()]);
|
||||||
bar.tick();
|
bar.tick();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
index += 1;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user