feat(net): add {linkSpeed} format replacement
Adds an ethernet link speed ({linkSpeed}) read from
/sys/class/net/<iface>/speed, plus a skip-decimal option on pow_format
to drop a trailing .0. Rebased onto master's store-based network
formatting; existing compact bandwidth args updated for the new
pow_format signature.
This commit is contained in:
+19
-9
@@ -5,12 +5,18 @@
|
||||
|
||||
class pow_format {
|
||||
public:
|
||||
pow_format(long long val, std::string&& unit, bool binary = false, int min_pow_for_decimal = 0)
|
||||
: val_(val), unit_(unit), binary_(binary), min_pow_for_decimal_(min_pow_for_decimal) {};
|
||||
pow_format(long long val, std::string&& unit, bool binary = false, bool skip_decimal = false,
|
||||
int min_pow_for_decimal = 0)
|
||||
: val_(val),
|
||||
unit_(unit),
|
||||
binary_(binary),
|
||||
skip_decimal_(skip_decimal),
|
||||
min_pow_for_decimal_(min_pow_for_decimal) {};
|
||||
|
||||
long long val_;
|
||||
std::string unit_;
|
||||
bool binary_;
|
||||
bool skip_decimal_;
|
||||
int min_pow_for_decimal_;
|
||||
};
|
||||
|
||||
@@ -50,17 +56,21 @@ struct formatter<pow_format> {
|
||||
const char* units[] = {"", "k", "M", "G", "T", "P", nullptr};
|
||||
|
||||
auto base = s.binary_ ? 1024ull : 1000ll;
|
||||
auto div = 1ll;
|
||||
auto fraction = (double)s.val_;
|
||||
|
||||
int pow;
|
||||
for (pow = 0; units[pow + 1] != nullptr && fraction / base >= 1; ++pow) {
|
||||
fraction /= base;
|
||||
div *= base;
|
||||
}
|
||||
|
||||
auto number_width = 5 // coeff in {:.1f} format
|
||||
+ s.binary_; // potential 4th digit before the decimal point
|
||||
auto max_width = number_width + 1 // prefix from units array
|
||||
+ s.binary_ // for the 'i' in GiB.
|
||||
auto fixed_precision = (s.skip_decimal_ && ((s.val_ % div) == 0)) ? 0 : 1;
|
||||
auto number_width = 3 + fixed_precision // coeff in {:.{fixed_precision}f} format
|
||||
+ (fixed_precision != 0) // float dot
|
||||
+ s.binary_; // potential digit before the decimal point
|
||||
auto max_width = number_width + 1 // prefix from units array
|
||||
+ s.binary_ // for the 'i' in GiB.
|
||||
+ s.unit_.length();
|
||||
|
||||
const char* format;
|
||||
@@ -71,17 +81,17 @@ struct formatter<pow_format> {
|
||||
case '<':
|
||||
return fmt::format_to(ctx.out(), "{:<{}}", fmt::format("{}", s), max_width);
|
||||
case '=':
|
||||
format = "{coefficient:<{number_width}.1f}{padding}{prefix}{unit}";
|
||||
format = "{coefficient:<{number_width}.{fixed_precision}f}{padding}{prefix}{unit}";
|
||||
break;
|
||||
case 0:
|
||||
default:
|
||||
format = pow < s.min_pow_for_decimal_ ? "{coefficient:.0f}{prefix}{unit}"
|
||||
: "{coefficient:.1f}{prefix}{unit}";
|
||||
: "{coefficient:.{fixed_precision}f}{prefix}{unit}";
|
||||
break;
|
||||
}
|
||||
return fmt::format_to(
|
||||
ctx.out(), fmt::runtime(format), fmt::arg("coefficient", fraction),
|
||||
fmt::arg("number_width", number_width),
|
||||
fmt::arg("fixed_precision", fixed_precision), fmt::arg("number_width", number_width),
|
||||
fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : "")),
|
||||
fmt::arg("unit", s.unit_),
|
||||
fmt::arg("padding", pow ? ""
|
||||
|
||||
Reference in New Issue
Block a user