mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Minor API updates
This commit is contained in:
@@ -45,11 +45,11 @@ public:
|
||||
_end = end;
|
||||
}
|
||||
|
||||
void append_text(const std::string& text) {
|
||||
void set_status_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();
|
||||
_status_text = text;
|
||||
if (_status_text.length() > _max_status_text_length)
|
||||
_max_status_text_length = _status_text.length();
|
||||
}
|
||||
|
||||
void show_percentage(bool flag) { _show_percentage = flag; }
|
||||
@@ -86,8 +86,8 @@ private:
|
||||
std::string _lead{">"};
|
||||
std::string _remainder{" "};
|
||||
std::string _end{"]"};
|
||||
std::string _text_after{""};
|
||||
std::atomic<size_t> _max_text_after_length{0};
|
||||
std::string _status_text{""};
|
||||
std::atomic<size_t> _max_status_text_length{0};
|
||||
std::atomic<bool> _completed{false};
|
||||
std::atomic<bool> _show_percentage{true};
|
||||
std::mutex _mutex;
|
||||
@@ -136,8 +136,8 @@ private:
|
||||
if (_show_percentage) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
||||
}
|
||||
if (_max_text_after_length == 0) _max_text_after_length = 10;
|
||||
std::cout << " " << _text_after << std::string(_max_text_after_length, ' ') << "\r";
|
||||
if (_max_status_text_length == 0) _max_status_text_length = 10;
|
||||
std::cout << " " << _status_text << std::string(_max_status_text_length, ' ') << "\r";
|
||||
std::cout.flush();
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
|
||||
@@ -15,7 +15,7 @@ int main() {
|
||||
p1.fill_progress_with("■");
|
||||
p1.lead_progress_with("■");
|
||||
p1.fill_remainder_with(" ");
|
||||
p1.end_with(" ]");
|
||||
p1.end_with("]");
|
||||
p1.color(ProgressBar::Color::YELLOW);
|
||||
|
||||
std::atomic<size_t> index1{0};
|
||||
@@ -36,7 +36,7 @@ int main() {
|
||||
while (true) {
|
||||
if (p1.completed())
|
||||
break;
|
||||
p1.append_text(status_text1[index1 % status_text1.size()]);
|
||||
p1.set_status_text(status_text1[index1 % status_text1.size()]);
|
||||
p1.set_progress(index1 * 10);
|
||||
index1 += 1;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
||||
@@ -55,19 +55,19 @@ int main() {
|
||||
p2.lead_progress_with(">");
|
||||
p2.fill_remainder_with(" ");
|
||||
p2.end_with("]");
|
||||
p2.append_text("Getting started");
|
||||
p2.set_status_text("Getting started");
|
||||
p2.color(ProgressBar::Color::GREEN);
|
||||
auto job2 = [&p2]() {
|
||||
while (true) {
|
||||
auto ticks = p2.current();
|
||||
if (ticks > 20 && ticks < 50)
|
||||
p2.append_text("Delaying the inevitable");
|
||||
p2.set_status_text("Delaying the inevitable");
|
||||
else if (ticks > 50 && ticks < 80)
|
||||
p2.append_text("Crying quietly");
|
||||
p2.set_status_text("Crying quietly");
|
||||
else if (ticks > 80 && ticks < 98)
|
||||
p2.append_text("Almost there");
|
||||
p2.set_status_text("Almost there");
|
||||
else if (ticks >= 98)
|
||||
p2.append_text("Done");
|
||||
p2.set_status_text("Done");
|
||||
p2.tick();
|
||||
if (p2.completed())
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user