2019-12-04 20:53:04 -06:00
|
|
|
/*
|
2019-12-04 21:10:58 -06:00
|
|
|
Activity Indicators for Modern C++
|
|
|
|
|
https://github.com/p-ranav/indicators
|
2019-12-04 20:53:04 -06:00
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
|
SPDX-License-Identifier: MIT
|
|
|
|
|
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
*/
|
2019-12-03 09:54:50 -06:00
|
|
|
#pragma once
|
2019-12-17 15:23:45 -06:00
|
|
|
#define NOMINMAX
|
2019-12-12 15:15:01 -06:00
|
|
|
#include <algorithm>
|
2019-12-03 09:54:50 -06:00
|
|
|
#include <atomic>
|
2019-12-17 09:20:10 -06:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <cmath>
|
2019-12-04 21:10:58 -06:00
|
|
|
#include <indicators/color.hpp>
|
2019-12-17 09:20:10 -06:00
|
|
|
#include <iomanip>
|
2019-12-03 21:25:22 -06:00
|
|
|
#include <iostream>
|
2019-12-03 09:54:50 -06:00
|
|
|
#include <mutex>
|
2019-12-03 21:25:22 -06:00
|
|
|
#include <string>
|
2019-12-03 09:54:50 -06:00
|
|
|
#include <thread>
|
2019-12-04 13:52:14 -06:00
|
|
|
#include <vector>
|
2019-12-03 09:54:50 -06:00
|
|
|
|
2019-12-04 21:10:58 -06:00
|
|
|
namespace indicators {
|
2019-12-04 13:24:15 -06:00
|
|
|
|
2019-12-04 13:52:14 -06:00
|
|
|
class ProgressSpinner {
|
2019-12-03 21:25:22 -06:00
|
|
|
public:
|
2019-12-04 11:18:52 -06:00
|
|
|
void set_foreground_color(Color color) {
|
2019-12-03 21:25:22 -06:00
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
2019-12-04 11:18:52 -06:00
|
|
|
_foreground_color = color;
|
2019-12-03 13:50:00 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:54:30 -06:00
|
|
|
void set_prefix_text(const std::string &text) {
|
2019-12-03 21:55:38 -06:00
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
2019-12-04 11:18:52 -06:00
|
|
|
_prefix_text = text;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:54:30 -06:00
|
|
|
void set_postfix_text(const std::string &text) {
|
2019-12-04 11:18:52 -06:00
|
|
|
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();
|
2019-12-03 21:55:38 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:35:48 -06:00
|
|
|
void show_percentage() { _show_percentage = true; }
|
2019-12-03 11:54:06 -06:00
|
|
|
|
2019-12-04 11:32:57 -06:00
|
|
|
void hide_percentage() { _show_percentage = false; }
|
|
|
|
|
|
2019-12-17 09:20:10 -06:00
|
|
|
void show_elapsed_time() { _show_elapsed_time = true; }
|
|
|
|
|
|
|
|
|
|
void hide_elapsed_time() { _show_elapsed_time = false; }
|
|
|
|
|
|
|
|
|
|
void show_remaining_time() { _show_remaining_time = true; }
|
|
|
|
|
|
|
|
|
|
void hide_remaining_time() { _show_remaining_time = false; }
|
|
|
|
|
|
2019-12-04 13:52:14 -06:00
|
|
|
void show_spinner() { _show_spinner = true; }
|
|
|
|
|
|
2019-12-04 20:59:51 -06:00
|
|
|
void hide_spinner() { _show_spinner = false; }
|
2019-12-04 13:52:14 -06:00
|
|
|
|
2019-12-03 19:59:43 -06:00
|
|
|
void set_progress(float value) {
|
2019-12-03 20:14:05 -06:00
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
|
|
|
|
_progress = value;
|
2019-12-03 09:54:50 -06:00
|
|
|
}
|
2019-12-17 09:20:10 -06:00
|
|
|
_save_start_time();
|
2019-12-03 19:59:43 -06:00
|
|
|
_print_progress();
|
2019-12-03 09:54:50 -06:00
|
|
|
}
|
2019-12-03 19:59:43 -06:00
|
|
|
|
|
|
|
|
void tick() {
|
2019-12-03 20:25:43 -06:00
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
|
|
|
|
_progress += 1;
|
|
|
|
|
}
|
2019-12-17 09:20:10 -06:00
|
|
|
_save_start_time();
|
2019-12-03 20:25:43 -06:00
|
|
|
_print_progress();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 07:55:22 -06:00
|
|
|
size_t current() {
|
|
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
|
|
|
|
return std::min(static_cast<size_t>(_progress), size_t(100));
|
2019-12-18 12:47:10 -06:00
|
|
|
}
|
2019-12-03 22:53:38 -06:00
|
|
|
|
2019-12-04 11:19:17 -06:00
|
|
|
bool is_completed() const { return _completed; }
|
2019-12-03 21:25:22 -06:00
|
|
|
|
2019-12-04 11:54:30 -06:00
|
|
|
void mark_as_completed() {
|
2019-12-04 11:32:57 -06:00
|
|
|
_completed = true;
|
|
|
|
|
_print_progress();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 20:59:51 -06:00
|
|
|
void set_spinner_states(const std::vector<std::string> &states) {
|
2019-12-04 13:52:14 -06:00
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
|
|
|
|
_states = states;
|
|
|
|
|
}
|
2019-12-04 20:59:51 -06:00
|
|
|
|
2019-12-03 21:25:22 -06:00
|
|
|
private:
|
|
|
|
|
float _progress{0.0};
|
2019-12-04 11:18:52 -06:00
|
|
|
std::string _prefix_text{""};
|
2019-12-04 13:52:14 -06:00
|
|
|
size_t _index{0};
|
2019-12-04 20:59:51 -06:00
|
|
|
std::vector<std::string> _states{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
2019-12-04 11:18:52 -06:00
|
|
|
std::string _postfix_text{""};
|
|
|
|
|
std::atomic<size_t> _max_postfix_text_length{0};
|
2019-12-03 21:25:22 -06:00
|
|
|
std::atomic<bool> _completed{false};
|
|
|
|
|
std::atomic<bool> _show_percentage{true};
|
2019-12-17 09:43:53 -06:00
|
|
|
std::atomic<bool> _show_elapsed_time{false};
|
|
|
|
|
std::atomic<bool> _show_remaining_time{false};
|
2019-12-17 09:20:10 -06:00
|
|
|
std::atomic<bool> _saved_start_time{false};
|
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
2019-12-04 20:59:51 -06:00
|
|
|
std::atomic<bool> _show_spinner{true};
|
2019-12-03 21:25:22 -06:00
|
|
|
std::mutex _mutex;
|
2019-12-04 11:18:52 -06:00
|
|
|
Color _foreground_color;
|
2019-12-03 21:25:22 -06:00
|
|
|
|
2019-12-17 09:20:10 -06:00
|
|
|
std::ostream &_print_duration(std::ostream &os, std::chrono::nanoseconds ns) {
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
typedef duration<int, ratio<86400>> days;
|
|
|
|
|
char fill = os.fill();
|
|
|
|
|
os.fill('0');
|
|
|
|
|
auto d = duration_cast<days>(ns);
|
|
|
|
|
ns -= d;
|
|
|
|
|
auto h = duration_cast<hours>(ns);
|
|
|
|
|
ns -= h;
|
|
|
|
|
auto m = duration_cast<minutes>(ns);
|
|
|
|
|
ns -= m;
|
|
|
|
|
auto s = duration_cast<seconds>(ns);
|
|
|
|
|
if (d.count() > 0)
|
|
|
|
|
os << setw(2) << d.count() << "d:";
|
|
|
|
|
if (h.count() > 0)
|
|
|
|
|
os << setw(2) << h.count() << "h:";
|
|
|
|
|
os << setw(2) << m.count() << "m:" << setw(2) << s.count() << 's';
|
|
|
|
|
os.fill(fill);
|
|
|
|
|
return os;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void _save_start_time() {
|
2019-12-17 09:31:43 -06:00
|
|
|
if ((_show_elapsed_time || _show_remaining_time) && !_saved_start_time) {
|
2019-12-17 09:20:10 -06:00
|
|
|
_start_time_point = std::chrono::high_resolution_clock::now();
|
|
|
|
|
_saved_start_time = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 21:25:22 -06:00
|
|
|
void _print_progress() {
|
|
|
|
|
std::unique_lock<std::mutex> lock{_mutex};
|
2019-12-17 09:20:10 -06:00
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
|
|
|
|
|
2019-12-03 22:53:38 -06:00
|
|
|
std::cout << termcolor::bold;
|
2019-12-04 11:54:30 -06:00
|
|
|
switch (_foreground_color) {
|
2019-12-03 21:25:22 -06:00
|
|
|
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;
|
2019-12-04 11:54:30 -06:00
|
|
|
break;
|
2019-12-03 21:25:22 -06:00
|
|
|
}
|
2019-12-04 11:18:52 -06:00
|
|
|
std::cout << _prefix_text;
|
2019-12-04 13:52:14 -06:00
|
|
|
if (_show_spinner)
|
|
|
|
|
std::cout << _states[_index % _states.size()];
|
2019-12-03 22:53:38 -06:00
|
|
|
if (_show_percentage) {
|
|
|
|
|
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
|
|
|
|
}
|
2019-12-17 09:20:10 -06:00
|
|
|
|
|
|
|
|
if (_show_elapsed_time) {
|
|
|
|
|
std::cout << " [";
|
|
|
|
|
_print_duration(std::cout, elapsed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_show_remaining_time) {
|
|
|
|
|
if (_show_elapsed_time)
|
|
|
|
|
std::cout << "<";
|
2019-12-17 09:31:43 -06:00
|
|
|
else
|
|
|
|
|
std::cout << " [";
|
2019-12-17 09:20:10 -06:00
|
|
|
auto eta = std::chrono::nanoseconds(
|
|
|
|
|
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
|
|
|
|
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
|
|
|
|
_print_duration(std::cout, remaining);
|
|
|
|
|
std::cout << "]";
|
2019-12-17 20:06:50 -06:00
|
|
|
} else {
|
|
|
|
|
if (_show_elapsed_time)
|
|
|
|
|
std::cout << "]";
|
2019-12-17 09:20:10 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-04 11:54:30 -06:00
|
|
|
if (_max_postfix_text_length == 0)
|
|
|
|
|
_max_postfix_text_length = 10;
|
2019-12-04 11:18:52 -06:00
|
|
|
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
|
2019-12-03 21:25:22 -06:00
|
|
|
std::cout.flush();
|
2019-12-04 13:52:14 -06:00
|
|
|
_index += 1;
|
2019-12-03 23:07:44 -06:00
|
|
|
if (_progress > 100.0) {
|
|
|
|
|
_completed = true;
|
2019-12-04 11:54:30 -06:00
|
|
|
}
|
2019-12-03 21:25:22 -06:00
|
|
|
if (_completed)
|
|
|
|
|
std::cout << termcolor::reset << std::endl;
|
2019-12-04 11:54:30 -06:00
|
|
|
}
|
2019-12-03 09:54:50 -06:00
|
|
|
};
|
2019-12-04 13:24:15 -06:00
|
|
|
|
2019-12-04 21:10:58 -06:00
|
|
|
} // namespace indicators
|