From d681473eb1bf934c714bc18f3888a733db7377cb Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 26 Nov 2022 16:26:45 +0100 Subject: [PATCH] chore: Refine ProgressBar value calculation --- src/ProgressBar.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ProgressBar.cpp b/src/ProgressBar.cpp index 99a37a088..f3f93f0bf 100644 --- a/src/ProgressBar.cpp +++ b/src/ProgressBar.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2021 Joel Rosdahl and other contributors +// Copyright (C) 2019-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -20,6 +20,7 @@ #include "fmtmacros.hpp" +#include #include #include "third_party/fmt/core.h" @@ -74,25 +75,30 @@ ProgressBar::update(double value) return; } + DEBUG_ASSERT(value >= 0.0); + DEBUG_ASSERT(value <= 1.0); + int16_t new_value = static_cast(1000 * value); if (new_value == m_current_value) { return; } m_current_value = new_value; + double new_value_percent = new_value / 10.0; + size_t first_part_width = m_header.size() + 10; if (first_part_width + 10 > m_width) { // The progress bar would be less than 10 characters, so just print the // percentage. - PRINT(stdout, "\r{} {:5.1f}%", m_header, 100 * value); + PRINT(stdout, "\r{} {:5.1f}%", m_header, new_value_percent); } else { size_t total_bar_width = m_width - first_part_width; - size_t filled_bar_width = value * total_bar_width; + size_t filled_bar_width = (new_value_percent / 100) * total_bar_width; size_t unfilled_bar_width = total_bar_width - filled_bar_width; PRINT(stdout, "\r{} {:5.1f}% [{:=<{}}{: <{}}]", m_header, - 100 * value, + new_value / 10.0, "", filled_bar_width, "", -- 2.47.2