mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
Closes #24
This commit is contained in:
@@ -77,19 +77,19 @@ public:
|
||||
template <typename T, details::ProgressBarOption id>
|
||||
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!");
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
get_value<id>() = std::move(setting).value;
|
||||
}
|
||||
|
||||
template <typename T, details::ProgressBarOption id>
|
||||
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!");
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
get_value<id>() = setting.value;
|
||||
}
|
||||
|
||||
void set_option(const details::Setting<std::string, details::ProgressBarOption::postfix_text>& setting){
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
|
||||
if(setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
|
||||
@@ -97,7 +97,7 @@ public:
|
||||
}
|
||||
|
||||
void set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text>&& setting){
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
|
||||
auto& new_value = get_value<details::ProgressBarOption::postfix_text>();
|
||||
if(new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()){
|
||||
@@ -107,32 +107,32 @@ public:
|
||||
|
||||
void set_progress(float value) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{_mutex};
|
||||
_progress = value;
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
progress_ = value;
|
||||
}
|
||||
_save_start_time();
|
||||
_print_progress();
|
||||
save_start_time();
|
||||
print_progress();
|
||||
}
|
||||
|
||||
void tick() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{_mutex};
|
||||
_progress += 1;
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
progress_ += 1;
|
||||
}
|
||||
_save_start_time();
|
||||
_print_progress();
|
||||
save_start_time();
|
||||
print_progress();
|
||||
}
|
||||
|
||||
size_t current() {
|
||||
std::lock_guard<std::mutex> lock{_mutex};
|
||||
return std::min(static_cast<size_t>(_progress), size_t(100));
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
return std::min(static_cast<size_t>(progress_), size_t(100));
|
||||
}
|
||||
|
||||
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
|
||||
|
||||
void mark_as_completed() {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
_print_progress();
|
||||
print_progress();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -148,33 +148,33 @@ private:
|
||||
}
|
||||
|
||||
Settings settings_;
|
||||
float _progress{0.0};
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> _start_time_point;
|
||||
std::mutex _mutex;
|
||||
float progress_{0.0};
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
|
||||
std::mutex mutex_;
|
||||
|
||||
template <typename Indicator, size_t count> friend class MultiProgress;
|
||||
std::atomic<bool> _multi_progress_mode{false};
|
||||
std::atomic<bool> multi_progress_mode_{false};
|
||||
|
||||
void _save_start_time() {
|
||||
void save_start_time() {
|
||||
auto& show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
|
||||
auto& saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
|
||||
auto& show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
|
||||
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
|
||||
_start_time_point = std::chrono::high_resolution_clock::now();
|
||||
start_time_point_ = std::chrono::high_resolution_clock::now();
|
||||
saved_start_time = true;
|
||||
}
|
||||
}
|
||||
|
||||
void _print_progress(bool from_multi_progress = false) {
|
||||
if (_multi_progress_mode && !from_multi_progress) {
|
||||
if (_progress > 100.0) {
|
||||
void print_progress(bool from_multi_progress = false) {
|
||||
if (multi_progress_mode_ && !from_multi_progress) {
|
||||
if (progress_ > 100.0) {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock{_mutex};
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - _start_time_point);
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
|
||||
|
||||
std::cout << termcolor::bold;
|
||||
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
|
||||
@@ -182,11 +182,11 @@ private:
|
||||
std::cout << get_value<details::ProgressBarOption::start>();
|
||||
|
||||
details::BlockProgressScaleWriter writer{std::cout, get_value<details::ProgressBarOption::bar_width>()};
|
||||
writer.write(_progress);
|
||||
writer.write(progress_);
|
||||
|
||||
std::cout << get_value<details::ProgressBarOption::end>();
|
||||
if (get_value<details::ProgressBarOption::show_percentage>()) {
|
||||
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
|
||||
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
|
||||
}
|
||||
|
||||
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
|
||||
@@ -200,7 +200,7 @@ private:
|
||||
else
|
||||
std::cout << " [";
|
||||
auto eta = std::chrono::nanoseconds(
|
||||
_progress > 0 ? static_cast<long long>(elapsed.count() * 100 / _progress) : 0);
|
||||
progress_ > 0 ? static_cast<long long>(elapsed.count() * 100 / progress_) : 0);
|
||||
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
|
||||
details::write_duration(std::cout, remaining);
|
||||
std::cout << "]";
|
||||
@@ -213,7 +213,7 @@ private:
|
||||
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
|
||||
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>() << std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ') << "\r";
|
||||
std::cout.flush();
|
||||
if (_progress > 100.0) {
|
||||
if (progress_ > 100.0) {
|
||||
get_value<details::ProgressBarOption::completed>() = true;
|
||||
}
|
||||
if (get_value<details::ProgressBarOption::completed>() && !from_multi_progress) // Don't std::endl if calling from MultiProgress
|
||||
|
||||
Reference in New Issue
Block a user