1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-16 07:22:43 +01:00

diff-closures: print sizes with dynamic unit

This commit is contained in:
Marcel 2025-10-27 00:05:39 +01:00
parent bef3c37cb2
commit 9d4d10954a
No known key found for this signature in database
GPG key ID: 446F3B093DF81C6A
5 changed files with 9 additions and 8 deletions

View file

@ -104,7 +104,7 @@ N string2IntWithUnitPrefix(std::string_view s)
* GiB`. If `align` is set, the number will be right-justified by
* padding with spaces on the left.
*/
std::string renderSize(uint64_t value, bool align = false);
std::string renderSize(int64_t value, bool align = false);
/**
* Parse a string into a float.

View file

@ -132,15 +132,16 @@ std::optional<N> string2Float(const std::string_view s)
template std::optional<double> string2Float<double>(const std::string_view s);
template std::optional<float> string2Float<float>(const std::string_view s);
std::string renderSize(uint64_t value, bool align)
std::string renderSize(int64_t value, bool align)
{
static const std::array<char, 9> prefixes{{'K', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'}};
size_t power = 0;
double res = value;
while (res > 1024 && power < prefixes.size()) {
double abs_value = std::abs(value);
while (abs_value > 1024 && power < prefixes.size()) {
++power;
res /= 1024;
abs_value /= 1024;
}
double res = (double) value / std::pow(1024.0, power);
return fmt(align ? "%6.1f %ciB" : "%.1f %ciB", power == 0 ? res / 1024 : res, prefixes.at(power));
}