Avoid overflow and use boost::format

If the user has an object greater than 1024 yottabytes, it'll just display it as
N yottabytes instead of overflowing.

Swaps to use boost::format strings instead of std::setw and std::setprecision.
This commit is contained in:
Benjamin Hipple 2018-08-26 18:12:06 -04:00 committed by Eelco Dolstra
parent 3407a5d936
commit c908df881f
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -4,8 +4,9 @@
#include "json.hh"
#include "common-args.hh"
#include <iomanip>
#include <algorithm>
#include <boost/format.hpp>
#include <iomanip>
using namespace nix;
@ -67,20 +68,20 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
void printSize(int value)
{
if (!humanReadable) {
std::cout << '\t' << std::setw(11) << value;
std::cout << '\t' << boost::format("%11d") % value;
return;
}
static constexpr std::array<char, 6> idents = {
' ', 'K', 'M', 'G', 'T', 'P'
static constexpr std::array<char, 9> idents = {
' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
};
size_t power = 0;
double res = value;
while (res > 1024) {
while (res > 1024 && power < idents.size()) {
++power;
res /= 1024;
}
std::cout << '\t' << std::setw(11) << std::setprecision(3) << res << idents[power];
std::cout << '\t' << boost::format("%11.1f") % res << idents[power];
}
void run(ref<Store> store, Paths storePaths) override