mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-07 12:58:54 +08:00
Added sample showing progress spinner
This commit is contained in:
133
include/indicator/progress_spinner.hpp
Normal file
133
include/indicator/progress_spinner.hpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <indicator/color.hpp>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace indicator {
|
||||
|
||||
class ProgressSpinner {
|
||||
public:
|
||||
void set_foreground_color(Color color) {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_foreground_color = color;
|
||||
}
|
||||
|
||||
void set_prefix_text(const std::string &text) {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_prefix_text = text;
|
||||
}
|
||||
|
||||
void set_postfix_text(const std::string &text) {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_postfix_text = text;
|
||||
if (_postfix_text.length() > _max_postfix_text_length)
|
||||
_max_postfix_text_length = _postfix_text.length();
|
||||
}
|
||||
|
||||
void show_percentage() { _show_percentage = true; }
|
||||
|
||||
void hide_percentage() { _show_percentage = false; }
|
||||
|
||||
void show_spinner() { _show_spinner = true; }
|
||||
|
||||
void hide_spinner() { _show_spinner = false; }
|
||||
|
||||
void set_progress(float value) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_progress = value;
|
||||
}
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_progress += 1;
|
||||
}
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
size_t current() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return _completed; }
|
||||
|
||||
void mark_as_completed() {
|
||||
_completed = true;
|
||||
_print_progress();
|
||||
}
|
||||
|
||||
void set_spinner_states(const std::vector<std::string>& states) {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_states = states;
|
||||
}
|
||||
|
||||
private:
|
||||
float _progress{0.0};
|
||||
std::string _prefix_text{""};
|
||||
size_t _index{0};
|
||||
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||
std::string _postfix_text{""};
|
||||
std::atomic<size_t> _max_postfix_text_length{0};
|
||||
std::atomic<bool> _completed{false};
|
||||
std::atomic<bool> _show_percentage{true};
|
||||
std::atomic<bool> _show_spinner{true};
|
||||
std::mutex _mutex;
|
||||
Color _foreground_color;
|
||||
|
||||
void _print_progress() {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
std::cout << termcolor::bold;
|
||||
switch (_foreground_color) {
|
||||
case Color::GREY:
|
||||
std::cout << termcolor::grey;
|
||||
break;
|
||||
case Color::RED:
|
||||
std::cout << termcolor::red;
|
||||
break;
|
||||
case Color::GREEN:
|
||||
std::cout << termcolor::green;
|
||||
break;
|
||||
case Color::YELLOW:
|
||||
std::cout << termcolor::yellow;
|
||||
break;
|
||||
case Color::BLUE:
|
||||
std::cout << termcolor::blue;
|
||||
break;
|
||||
case Color::MAGENTA:
|
||||
std::cout << termcolor::magenta;
|
||||
break;
|
||||
case Color::CYAN:
|
||||
std::cout << termcolor::cyan;
|
||||
break;
|
||||
case Color::WHITE:
|
||||
std::cout << termcolor::white;
|
||||
break;
|
||||
}
|
||||
std::cout << _prefix_text;
|
||||
if (_show_spinner)
|
||||
std::cout << _states[_index % _states.size()];
|
||||
if (_show_percentage) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
||||
}
|
||||
if (_max_postfix_text_length == 0)
|
||||
_max_postfix_text_length = 10;
|
||||
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
||||
std::cout.flush();
|
||||
_index += 1;
|
||||
if (_progress > 100.0) {
|
||||
_completed = true;
|
||||
}
|
||||
if (_completed)
|
||||
std::cout << termcolor::reset << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class ProgressSpinner {
|
||||
std::string _name{"Running"};
|
||||
size_t _bar_width{80};
|
||||
std::mutex _mutex;
|
||||
float _progress{0.0};
|
||||
size_t _index{0};
|
||||
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||
|
||||
void hide_cursor() { std::cout << "\e[?25l"; }
|
||||
|
||||
void show_cursor() { std::cout << "\e[?25h"; }
|
||||
|
||||
public:
|
||||
explicit ProgressSpinner(const std::string &name) : _name(name) {}
|
||||
|
||||
void increment(float value) {
|
||||
std::unique_lock<std::mutex> lock{_mutex};
|
||||
_progress = value / 100.0;
|
||||
float pos = _progress * static_cast<float>(_bar_width);
|
||||
std::cout << _states[_index % _states.size()];
|
||||
std::cout << " " << static_cast<int>(value) << "%";
|
||||
std::cout << " " << _name << "\r";
|
||||
std::cout.flush();
|
||||
_index += 1;
|
||||
}
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <indicator/progressbar.hpp>
|
||||
#include <indicator/progress_bar.hpp>
|
||||
#include <indicator/progress_spinner.hpp>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
@@ -161,6 +162,7 @@ int main() {
|
||||
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
|
||||
index4 += 1;
|
||||
if (p4.current() + 2 >= 100) {
|
||||
std::cout << std::endl;
|
||||
p4.set_foreground_color(indicator::Color::RED);
|
||||
p4.set_prefix_text("{ ERROR } ");
|
||||
p4.show_percentage();
|
||||
@@ -209,37 +211,29 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
indicator::ProgressBar p;
|
||||
p.set_bar_width(50);
|
||||
p.start_bar_with("");
|
||||
p.fill_bar_progress_with("");
|
||||
p.lead_bar_progress_with("");
|
||||
p.fill_bar_remainder_with("");
|
||||
p.end_bar_with("");
|
||||
indicator::ProgressSpinner p;
|
||||
p.set_postfix_text("Checking credentials");
|
||||
p.set_foreground_color(indicator::Color::YELLOW);
|
||||
std::atomic<int> spinner_index{0};
|
||||
std::vector<std::string> spinner{"⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"};
|
||||
auto job = [&p, &spinner_index, &spinner]() {
|
||||
p.set_spinner_states({"⠈", "⠐", "⠠", "⢀", "⡀", "⠄", "⠂", "⠁"});
|
||||
auto job = [&p]() {
|
||||
while (true) {
|
||||
p.set_prefix_text(spinner[spinner_index % spinner.size()]);
|
||||
p.tick();
|
||||
spinner_index -= 1;
|
||||
if (p.current() + 2 >= 100) {
|
||||
std::cout << std::endl;
|
||||
if (p.is_completed()) {
|
||||
p.set_foreground_color(indicator::Color::GREEN);
|
||||
p.set_prefix_text("✔");
|
||||
p.hide_spinner();
|
||||
p.hide_percentage();
|
||||
p.set_postfix_text("Authenticated!");
|
||||
p.mark_as_completed();
|
||||
break;
|
||||
}
|
||||
else
|
||||
p.tick();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(60));
|
||||
}
|
||||
};
|
||||
std::thread thread(job);
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user