2019-12-03 09:54:50 -06:00
|
|
|
#pragma once
|
2020-01-19 16:20:01 +03:00
|
|
|
|
|
|
|
|
#include <indicators/details/stream_helper.hpp>
|
|
|
|
|
|
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>
|
2020-02-04 23:29:14 +01:00
|
|
|
#include <indicators/setting.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>
|
2020-02-13 14:21:01 +05:30
|
|
|
#include <tuple>
|
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 {
|
2020-02-13 14:21:01 +05:30
|
|
|
using Settings =
|
|
|
|
|
std::tuple<option::ForegroundColor, option::PrefixText, option::PostfixText,
|
|
|
|
|
option::ShowPercentage, option::ShowElapsedTime, option::ShowRemainingTime,
|
|
|
|
|
option::ShowSpinner, option::SavedStartTime, option::Completed,
|
2020-04-30 21:37:06 -05:00
|
|
|
option::MaxPostfixTextLen, option::SpinnerStates, option::FontStyles,
|
2020-05-09 20:33:15 -05:00
|
|
|
option::MaxProgress, option::Stream>;
|
2020-02-13 14:21:01 +05:30
|
|
|
|
2019-12-03 21:25:22 -06:00
|
|
|
public:
|
2020-02-13 14:21:01 +05:30
|
|
|
template <typename... Args,
|
|
|
|
|
typename std::enable_if<details::are_settings_from_tuple<
|
|
|
|
|
Settings, typename std::decay<Args>::type...>::value,
|
|
|
|
|
void *>::type = nullptr>
|
|
|
|
|
explicit ProgressSpinner(Args &&... args)
|
|
|
|
|
: settings_(details::get<details::ProgressBarOption::foreground_color>(
|
2020-04-06 11:10:41 -07:00
|
|
|
option::ForegroundColor{Color::unspecified}, std::forward<Args>(args)...),
|
2020-02-13 14:21:01 +05:30
|
|
|
details::get<details::ProgressBarOption::prefix_text>(
|
|
|
|
|
option::PrefixText{}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::postfix_text>(
|
|
|
|
|
option::PostfixText{}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::show_percentage>(
|
|
|
|
|
option::ShowPercentage{true}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::show_elapsed_time>(
|
|
|
|
|
option::ShowElapsedTime{false}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::show_remaining_time>(
|
|
|
|
|
option::ShowRemainingTime{false}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::spinner_show>(
|
|
|
|
|
option::ShowSpinner{true}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::saved_start_time>(
|
|
|
|
|
option::SavedStartTime{false}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::completed>(option::Completed{false},
|
|
|
|
|
std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::max_postfix_text_len>(
|
|
|
|
|
option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::spinner_states>(
|
|
|
|
|
option::SpinnerStates{std::vector<std::string>{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴",
|
|
|
|
|
"⠦", "⠧", "⠇", "⠏"}},
|
2020-04-06 11:25:54 -07:00
|
|
|
std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::font_styles>(
|
2020-04-30 21:37:06 -05:00
|
|
|
option::FontStyles{std::vector<FontStyle>{}}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::max_progress>(
|
2020-05-09 20:33:15 -05:00
|
|
|
option::MaxProgress{100}, std::forward<Args>(args)...),
|
|
|
|
|
details::get<details::ProgressBarOption::stream>(
|
|
|
|
|
option::Stream{std::cout}, std::forward<Args>(args)...)) {}
|
2020-02-04 23:29:14 +01:00
|
|
|
|
|
|
|
|
template <typename T, details::ProgressBarOption id>
|
2020-02-13 14:21:01 +05:30
|
|
|
void set_option(details::Setting<T, id> &&setting) {
|
|
|
|
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
|
|
|
|
|
std::declval<Settings>()))>::type>::value,
|
|
|
|
|
"Setting has wrong type!");
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<id>() = std::move(setting).value;
|
2019-12-03 13:50:00 -06:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 23:29:14 +01:00
|
|
|
template <typename T, details::ProgressBarOption id>
|
2020-02-13 14:21:01 +05:30
|
|
|
void set_option(const details::Setting<T, id> &setting) {
|
|
|
|
|
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
|
|
|
|
|
std::declval<Settings>()))>::type>::value,
|
|
|
|
|
"Setting has wrong type!");
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<id>() = setting.value;
|
2019-12-04 11:18:52 -06:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 14:21:01 +05:30
|
|
|
void set_option(
|
|
|
|
|
const details::Setting<std::string, details::ProgressBarOption::postfix_text> &setting) {
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
|
2020-02-13 14:21:01 +05:30
|
|
|
if (setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
|
|
|
|
|
}
|
2019-12-03 21:55:38 -06:00
|
|
|
}
|
|
|
|
|
|
2020-02-13 14:21:01 +05:30
|
|
|
void
|
|
|
|
|
set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text> &&setting) {
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
|
2020-02-13 14:21:01 +05:30
|
|
|
auto &new_value = get_value<details::ProgressBarOption::postfix_text>();
|
|
|
|
|
if (new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-04 13:52:14 -06:00
|
|
|
|
2020-02-22 11:02:16 +05:30
|
|
|
void set_progress(size_t value) {
|
2019-12-03 20:14:05 -06:00
|
|
|
{
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock{mutex_};
|
|
|
|
|
progress_ = value;
|
2019-12-03 09:54:50 -06:00
|
|
|
}
|
2020-02-11 16:31:31 +05:30
|
|
|
save_start_time();
|
|
|
|
|
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
|
|
|
{
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock{mutex_};
|
|
|
|
|
progress_ += 1;
|
2019-12-03 20:25:43 -06:00
|
|
|
}
|
2020-02-11 16:31:31 +05:30
|
|
|
save_start_time();
|
|
|
|
|
print_progress();
|
2019-12-03 20:25:43 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-18 07:55:22 -06:00
|
|
|
size_t current() {
|
2020-02-11 16:31:31 +05:30
|
|
|
std::lock_guard<std::mutex> lock{mutex_};
|
2020-04-30 21:37:06 -05:00
|
|
|
return std::min(progress_, size_t(get_value<details::ProgressBarOption::max_progress>()));
|
2019-12-18 12:47:10 -06:00
|
|
|
}
|
2019-12-03 22:53:38 -06:00
|
|
|
|
2020-02-04 23:29:14 +01:00
|
|
|
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
2019-12-03 21:25:22 -06:00
|
|
|
|
2019-12-04 11:54:30 -06:00
|
|
|
void mark_as_completed() {
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::completed>() = true;
|
2020-02-11 16:31:31 +05:30
|
|
|
print_progress();
|
2019-12-04 11:32:57 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-03 21:25:22 -06:00
|
|
|
private:
|
2020-02-04 23:29:14 +01:00
|
|
|
Settings settings_;
|
2020-02-22 11:02:16 +05:30
|
|
|
size_t progress_{0};
|
2020-02-11 16:31:31 +05:30
|
|
|
size_t index_{0};
|
|
|
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
|
|
|
|
|
std::mutex mutex_;
|
2020-02-04 23:29:14 +01:00
|
|
|
|
|
|
|
|
template <details::ProgressBarOption id>
|
2020-02-13 14:21:01 +05:30
|
|
|
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
|
2020-02-04 23:29:14 +01:00
|
|
|
return details::get_value<id>(settings_).value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <details::ProgressBarOption id>
|
2020-02-13 14:21:01 +05:30
|
|
|
auto get_value() const
|
|
|
|
|
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
|
2020-02-04 23:29:14 +01:00
|
|
|
return details::get_value<id>(settings_).value;
|
|
|
|
|
}
|
2019-12-03 21:25:22 -06:00
|
|
|
|
2020-02-11 16:31:31 +05:30
|
|
|
void save_start_time() {
|
2020-02-13 14:21:01 +05:30
|
|
|
auto &show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
|
|
|
|
|
auto &show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
|
|
|
|
|
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
2020-02-04 23:29:14 +01:00
|
|
|
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
|
2020-02-11 16:31:31 +05:30
|
|
|
start_time_point_ = std::chrono::high_resolution_clock::now();
|
2020-02-04 23:29:14 +01:00
|
|
|
saved_start_time = true;
|
2019-12-17 09:20:10 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-08 15:14:52 -05:00
|
|
|
public:
|
2020-02-11 16:31:31 +05:30
|
|
|
void print_progress() {
|
|
|
|
|
std::lock_guard<std::mutex> lock{mutex_};
|
2020-05-09 20:33:15 -05:00
|
|
|
|
|
|
|
|
auto& os = get_value<details::ProgressBarOption::stream>();
|
|
|
|
|
|
2020-04-30 21:37:06 -05:00
|
|
|
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
|
2019-12-17 09:20:10 -06:00
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
2020-02-11 16:31:31 +05:30
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
|
2019-12-17 09:20:10 -06:00
|
|
|
|
2020-04-06 11:10:41 -07:00
|
|
|
if (get_value<details::ProgressBarOption::foreground_color>() != Color::unspecified)
|
2020-05-09 20:33:15 -05:00
|
|
|
details::set_stream_color(os, get_value<details::ProgressBarOption::foreground_color>());
|
2020-04-06 11:25:54 -07:00
|
|
|
|
|
|
|
|
for (auto &style : get_value<details::ProgressBarOption::font_styles>())
|
2020-05-09 20:33:15 -05:00
|
|
|
details::set_font_style(os, style);
|
2020-04-06 11:25:54 -07:00
|
|
|
|
2020-05-09 20:33:15 -05:00
|
|
|
os << get_value<details::ProgressBarOption::prefix_text>();
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::spinner_show>())
|
2020-05-09 20:33:15 -05:00
|
|
|
os << get_value<details::ProgressBarOption::spinner_states>()
|
2020-02-13 14:21:01 +05:30
|
|
|
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
2020-05-09 20:33:15 -05:00
|
|
|
os << " " << std::min(progress_, size_t(max_progress)) << "%";
|
2019-12-03 22:53:38 -06:00
|
|
|
}
|
2019-12-17 09:20:10 -06:00
|
|
|
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
2020-05-09 20:33:15 -05:00
|
|
|
os << " [";
|
|
|
|
|
details::write_duration(os, elapsed);
|
2019-12-17 09:20:10 -06:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
|
|
|
|
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
2020-05-09 20:33:15 -05:00
|
|
|
os << "<";
|
2019-12-17 09:31:43 -06:00
|
|
|
else
|
2020-05-09 20:33:15 -05:00
|
|
|
os << " [";
|
2019-12-17 09:20:10 -06:00
|
|
|
auto eta = std::chrono::nanoseconds(
|
2020-04-30 21:37:06 -05:00
|
|
|
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
|
2019-12-17 09:20:10 -06:00
|
|
|
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
2020-05-09 20:33:15 -05:00
|
|
|
details::write_duration(os, remaining);
|
|
|
|
|
os << "]";
|
2019-12-17 20:06:50 -06:00
|
|
|
} else {
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::show_elapsed_time>())
|
2020-05-09 20:33:15 -05:00
|
|
|
os << "]";
|
2019-12-17 09:20:10 -06:00
|
|
|
}
|
|
|
|
|
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
|
|
|
|
|
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
|
2020-05-09 20:33:15 -05:00
|
|
|
os << " " << get_value<details::ProgressBarOption::postfix_text>()
|
2020-02-13 14:21:01 +05:30
|
|
|
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
|
|
|
|
|
<< "\r";
|
2020-05-09 20:33:15 -05:00
|
|
|
os.flush();
|
2020-02-11 16:31:31 +05:30
|
|
|
index_ += 1;
|
2020-04-30 21:37:06 -05:00
|
|
|
if (progress_ > max_progress) {
|
2020-02-04 23:29:14 +01:00
|
|
|
get_value<details::ProgressBarOption::completed>() = true;
|
2019-12-04 11:54:30 -06:00
|
|
|
}
|
2020-02-04 23:29:14 +01:00
|
|
|
if (get_value<details::ProgressBarOption::completed>())
|
2020-05-09 20:33:15 -05:00
|
|
|
os << 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
|