Add a MaxProgress option (default: 100).

The MaxProgress option allows you to set the maximum number of ticks
that are within a progress bar. Each call to tick() increments the tick
count. The progress bar percentage is the number of ticks divided by the
MaxProgress option.

The default MaxProgress is 100, so each tick would be 1%.  If
MaxProgress is set to 500, for example, then each tick would be 0.2%.
This commit is contained in:
Kevin M. Godby
2020-04-30 21:37:06 -05:00
parent df4a97b068
commit 371bcb2c3b
6 changed files with 71 additions and 23 deletions

View File

@@ -46,7 +46,8 @@ class BlockProgressBar {
using Settings = std::tuple<option::ForegroundColor, option::BarWidth, option::Start, option::End,
option::PrefixText, option::PostfixText, option::ShowPercentage,
option::ShowElapsedTime, option::ShowRemainingTime, option::Completed,
option::SavedStartTime, option::MaxPostfixTextLen, option::FontStyles>;
option::SavedStartTime, option::MaxPostfixTextLen, option::FontStyles,
option::MaxProgress>;
public:
template <typename... Args,
@@ -79,7 +80,10 @@ public:
details::get<details::ProgressBarOption::max_postfix_text_len>(
option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::font_styles>(
option::FontStyles{std::vector<FontStyle>{}}, std::forward<Args>(args)...)) {}
option::FontStyles{std::vector<FontStyle>{}}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_progress>(option::MaxProgress{100},
std::forward<Args>(args)...)
) {}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id> &&setting) {
@@ -138,7 +142,7 @@ public:
size_t current() {
std::lock_guard<std::mutex> lock{mutex_};
return std::min(static_cast<size_t>(progress_), size_t(100));
return std::min(static_cast<size_t>(progress_), size_t(get_value<details::ProgressBarOption::max_progress>()));
}
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
@@ -180,8 +184,9 @@ private:
}
void print_progress(bool from_multi_progress = false) {
const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > 100.0) {
if (progress_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true;
}
return;
@@ -201,11 +206,11 @@ private:
details::BlockProgressScaleWriter writer{std::cout,
get_value<details::ProgressBarOption::bar_width>()};
writer.write(progress_);
writer.write(progress_ / max_progress * 100);
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_ / max_progress * 100.0), size_t(100)) << "%";
}
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
@@ -226,7 +231,7 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * 100 / progress_) : 0);
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(std::cout, remaining);
} else {
@@ -245,7 +250,7 @@ private:
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
<< "\r";
std::cout.flush();
if (progress_ > 100.0) {
if (progress_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true;
}
if (get_value<details::ProgressBarOption::completed>() &&