1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 23:12:44 +01:00

Merge pull request #14364 from MarcelCoding/human-sizes

diff-closures: print sizes with dynamic unit
This commit is contained in:
Jörg Thalheim 2025-10-27 18:49:23 +00:00 committed by GitHub
commit c5515bb22e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 24 additions and 30 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.
@ -333,8 +333,6 @@ struct overloaded : Ts...
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
std::string showBytes(uint64_t bytes);
/**
* Provide an addition operator between strings and string_views
* inexplicably omitted from the standard library.

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));
}
@ -256,9 +257,4 @@ std::pair<std::string_view, std::string_view> getLine(std::string_view s)
}
}
std::string showBytes(uint64_t bytes)
{
return fmt("%.2f MiB", bytes / (1024.0 * 1024.0));
}
} // namespace nix