Fix lsb error for ticks with huge max_progress values.

When the max_progress value is >1.67772e+07, the progress_ which is
float, is not chanegd when tick() is called as +1 is outside of the
precission for a float value. The bar stays at value of 1.67772e+07
and 67%.
In this fix I introduced tics_ varialbe of size_t which is increased
with every tick. And the progress_ is calcualted as a ratio of
tick_/max_progress.
The breaking and required change is the change of set_progress(float)
to set_progress(size_t), so it modifies directly the tics_ value.
This commit is contained in:
Rafał Lalik
2025-04-30 23:57:01 +02:00
parent ac6c93ea2b
commit fbdac646ee
2 changed files with 14 additions and 14 deletions

View File

@@ -102,10 +102,10 @@ public:
} }
} }
void set_progress(float value) { void set_progress(size_t value) {
{ {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
progress_ = value; tick_ = value;
} }
save_start_time(); save_start_time();
print_progress(); print_progress();
@@ -114,7 +114,7 @@ public:
void tick() { void tick() {
{ {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
progress_ += 1; tick_++;
} }
save_start_time(); save_start_time();
print_progress(); print_progress();
@@ -122,8 +122,7 @@ public:
size_t current() { size_t current() {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
return (std::min)(static_cast<size_t>(progress_), return (std::min)(tick_, size_t(get_value<details::ProgressBarOption::max_progress>()));
size_t(get_value<details::ProgressBarOption::max_progress>()));
} }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); } bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
@@ -147,6 +146,7 @@ private:
Settings settings_; Settings settings_;
float progress_{0.0}; float progress_{0.0};
size_t tick_{0};
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_; std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_; std::mutex mutex_;
@@ -175,11 +175,12 @@ private:
std::pair<std::string, int> get_postfix_text() { std::pair<std::string, int> get_postfix_text() {
std::stringstream os; std::stringstream os;
const auto max_progress = get_value<details::ProgressBarOption::max_progress>(); const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
progress_ = static_cast<float>(tick_)/max_progress;
auto now = std::chrono::high_resolution_clock::now(); 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_);
if (get_value<details::ProgressBarOption::show_percentage>()) { if (get_value<details::ProgressBarOption::show_percentage>()) {
os << " " << (std::min)(static_cast<size_t>(progress_ / max_progress * 100.0), size_t(100)) os << " " << (std::min)(static_cast<size_t>(progress_ * 100.0), size_t(100))
<< "%"; << "%";
} }
@@ -201,9 +202,8 @@ private:
if (saved_start_time) { if (saved_start_time) {
auto eta = std::chrono::nanoseconds( auto eta = std::chrono::nanoseconds(
progress_ > 0 tick_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) * ? static_cast<long long>(std::ceil(float(elapsed.count()) * progress_))
max_progress / progress_))
: 0); : 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta); auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(os, remaining); details::write_duration(os, remaining);
@@ -232,7 +232,7 @@ public:
const auto max_progress = get_value<details::ProgressBarOption::max_progress>(); const auto max_progress = get_value<details::ProgressBarOption::max_progress>();
if (multi_progress_mode_ && !from_multi_progress) { if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > max_progress) { if (tick_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
return; return;
@@ -253,7 +253,7 @@ public:
details::BlockProgressScaleWriter writer{os, details::BlockProgressScaleWriter writer{os,
get_value<details::ProgressBarOption::bar_width>()}; get_value<details::ProgressBarOption::bar_width>()};
writer.write(progress_ / max_progress * 100); writer.write(progress_ * 100);
os << get_value<details::ProgressBarOption::end>(); os << get_value<details::ProgressBarOption::end>();
@@ -278,7 +278,7 @@ public:
} }
os.flush(); os.flush();
if (progress_ > max_progress) { if (tick_ > max_progress) {
get_value<details::ProgressBarOption::completed>() = true; get_value<details::ProgressBarOption::completed>() = true;
} }
if (get_value<details::ProgressBarOption::completed>() && if (get_value<details::ProgressBarOption::completed>() &&

View File

@@ -17,7 +17,7 @@ int main() {
auto progress = 0.0f; auto progress = 0.0f;
while (true) { while (true) {
bar.set_progress(progress); bar.set_progress(progress);
progress += 0.25f; progress += 1;
if (bar.is_completed()) if (bar.is_completed())
break; break;
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));