First pass implementation of dynamic progress bar

This commit is contained in:
Pranav Srinivas Kumar
2020-02-21 22:40:16 +05:30
parent 0ace816e52
commit 5e75c285fe
6 changed files with 267 additions and 2 deletions

View File

@@ -164,6 +164,7 @@ private:
std::mutex mutex_;
template <typename Indicator, size_t count> friend class MultiProgress;
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};
void save_start_time() {

View File

@@ -0,0 +1,137 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
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.
*/
#pragma once
#include <atomic>
#include <functional>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <iostream>
#include <mutex>
#include <vector>
namespace indicators {
template <typename Indicator> class DynamicProgress {
using Settings = std::tuple<option::HideBarWhenComplete>;
public:
template <typename... Indicators> explicit DynamicProgress(Indicators &... bars) {
bars_ = {bars...};
for (auto &bar : bars_) {
bar.get().multi_progress_mode_ = true;
++total_count_;
}
}
Indicator &operator[](size_t index) {
print_progress();
std::lock_guard<std::mutex> lock{mutex_};
return bars_[index].get();
}
size_t push_back(Indicator &bar) {
std::lock_guard<std::mutex> lock{mutex_};
bar.multi_progress_mode_ = true;
bars_.push_back(bar);
return bars_.size() - 1;
}
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_);
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_);
get_value<id>() = setting.value;
}
private:
Settings settings_;
std::atomic<bool> started_{false};
std::mutex mutex_;
std::vector<std::reference_wrapper<Indicator>> bars_;
std::atomic<size_t> total_count_{0};
std::atomic<size_t> incomplete_count_{0};
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
void print_progress() {
std::lock_guard<std::mutex> lock{mutex_};
auto &hide_bar_when_complete = get_value<details::ProgressBarOption::hide_bar_when_complete>();
if (hide_bar_when_complete) {
// incomplete_count_ = 0;
// for (auto &bar : bars_)
// if (!bar.get().is_completed())
// incomplete_count_ += 1;
// if (started_) {
// for (size_t i = 0; i < incomplete_count_; ++i)
// std::cout << "\e[A"
// << "\r\e[K" << std::flush;
// }
// for (auto &bar : bars_) {
// if (!bar.get().is_completed()) {
// bar.get().print_progress(true);
// std::cout << "\n";
// }
// }
} else {
// Don't hide any bars
if (started_) {
for (size_t i = 0; i < total_count_; ++i)
std::cout << "\x1b[A";
}
for (auto &bar: bars_) {
bar.get().print_progress(true);
std::cout << "\n";
}
if (!started_)
started_ = true;
}
total_count_ = bars_.size();
std::cout << termcolor::reset;
}
};
} // namespace indicators

View File

@@ -176,6 +176,7 @@ private:
std::mutex mutex_;
template <typename Indicator, size_t count> friend class MultiProgress;
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};
void save_start_time() {
@@ -189,13 +190,13 @@ private:
}
void print_progress(bool from_multi_progress = false) {
std::lock_guard<std::mutex> lock{mutex_};
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_};
auto now = std::chrono::high_resolution_clock::now();
if (!get_value<details::ProgressBarOption::completed>())
elapsed_ = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);

View File

@@ -85,7 +85,8 @@ enum class ProgressBarOption {
saved_start_time,
foreground_color,
spinner_show,
spinner_states
spinner_states,
hide_bar_when_complete
};
template <typename T, ProgressBarOption Id> struct Setting {
@@ -196,5 +197,7 @@ using ForegroundColor = details::Setting<Color, details::ProgressBarOption::fore
using ShowSpinner = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
using SpinnerStates =
details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
using HideBarWhenComplete =
details::BooleanSetting<details::ProgressBarOption::hide_bar_when_complete>;
} // namespace option
} // namespace indicators