summaryrefslogtreecommitdiff
path: root/apt-pkg/install-progress.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2016-08-12 09:07:59 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2016-08-12 11:12:04 +0200
commit1cb047079aa2c26a8159d100348b7e69a49bc117 (patch)
tree9591f7e0900f87a04683de723090161d0d324d9a /apt-pkg/install-progress.cc
parentecaae01f31878a70771f75046b3e00173ff397b5 (diff)
don't perform int<float in progress bar drawing
Comparing floating numbers is always fun and in this instance a 9 < 9.0 is "somehow" true on hurd-i386 letting the tests fail by reporting that too much progress achieved. A bit mysterious, but with some rework we can use code which avoids dealing with the floats in this way entirely and make our testcases happy.
Diffstat (limited to 'apt-pkg/install-progress.cc')
-rw-r--r--apt-pkg/install-progress.cc20
1 files changed, 8 insertions, 12 deletions
diff --git a/apt-pkg/install-progress.cc b/apt-pkg/install-progress.cc
index c77c240c3..be5b8b5c7 100644
--- a/apt-pkg/install-progress.cc
+++ b/apt-pkg/install-progress.cc
@@ -14,6 +14,7 @@
#include <algorithm>
#include <stdio.h>
#include <sstream>
+#include <cmath>
#include <apti18n.h>
@@ -321,19 +322,14 @@ std::string
PackageManagerFancy::GetTextProgressStr(float Percent, int OutputSize)
{
std::string output;
- int i;
-
- // should we raise a exception here instead?
- if (Percent < 0.0 || Percent > 1.0 || OutputSize < 3)
+ if (unlikely(OutputSize < 3))
return output;
-
- int BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
- output += "[";
- for(i=0; i < BarSize*Percent; i++)
- output += "#";
- for (/*nothing*/; i < BarSize; i++)
- output += ".";
- output += "]";
+
+ int const BarSize = OutputSize - 2; // bar without the leading "[" and trailing "]"
+ int const BarDone = std::max(0, std::min(BarSize, static_cast<int>(std::floor(Percent * BarSize))));
+ output.append("[");
+ std::fill_n(std::fill_n(std::back_inserter(output), BarDone, '#'), BarSize - BarDone, '.');
+ output.append("]");
return output;
}