mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 12:06:01 +01:00
diff-closures: print sizes with dynamic unit
(cherry picked from commit 9d4d10954a)
This commit is contained in:
parent
fc0601b153
commit
01fd08803e
5 changed files with 9 additions and 8 deletions
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue