Files
indicators/include/progress/spinner.hpp
Pranav Srinivas KumaR 537e350855 Added termcolor
2019-12-03 21:25:22 -06:00

35 lines
917 B
C++

#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;
}
};