format+net: skip trailing .0 in linkSpeed
parent
8c30f43096
commit
7ab57ab37a
|
@ -5,12 +5,13 @@
|
||||||
|
|
||||||
class pow_format {
|
class pow_format {
|
||||||
public:
|
public:
|
||||||
pow_format(long long val, std::string&& unit, bool binary = false)
|
pow_format(long long val, std::string&& unit, bool binary = false, bool skip_decimal = false)
|
||||||
: val_(val), unit_(unit), binary_(binary){};
|
: val_(val), unit_(unit), binary_(binary), skip_decimal_(skip_decimal){};
|
||||||
|
|
||||||
long long val_;
|
long long val_;
|
||||||
std::string unit_;
|
std::string unit_;
|
||||||
bool binary_;
|
bool binary_;
|
||||||
|
bool skip_decimal_;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
|
@ -49,17 +50,21 @@ struct formatter<pow_format> {
|
||||||
const char* units[] = {"", "k", "M", "G", "T", "P", nullptr};
|
const char* units[] = {"", "k", "M", "G", "T", "P", nullptr};
|
||||||
|
|
||||||
auto base = s.binary_ ? 1024ull : 1000ll;
|
auto base = s.binary_ ? 1024ull : 1000ll;
|
||||||
|
auto div = 1ll;
|
||||||
auto fraction = (double)s.val_;
|
auto fraction = (double)s.val_;
|
||||||
|
|
||||||
int pow;
|
int pow;
|
||||||
for (pow = 0; units[pow + 1] != nullptr && fraction / base >= 1; ++pow) {
|
for (pow = 0; units[pow + 1] != nullptr && fraction / base >= 1; ++pow) {
|
||||||
fraction /= base;
|
fraction /= base;
|
||||||
|
div *= base;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto number_width = 5 // coeff in {:.1f} format
|
auto fixed_precision = (s.skip_decimal_ && ((s.val_%div) == 0)) ? 0 : 1;
|
||||||
+ s.binary_; // potential 4th digit before the decimal point
|
auto number_width = 3 + fixed_precision // coeff in {:.{fixed_precision}f} format
|
||||||
auto max_width = number_width + 1 // prefix from units array
|
+ (fixed_precision != 0)// float dot
|
||||||
+ s.binary_ // for the 'i' in GiB.
|
+ 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();
|
+ s.unit_.length();
|
||||||
|
|
||||||
const char* format;
|
const char* format;
|
||||||
|
@ -70,15 +75,16 @@ struct formatter<pow_format> {
|
||||||
case '<':
|
case '<':
|
||||||
return fmt::format_to(ctx.out(), "{:<{}}", fmt::format("{}", s), max_width);
|
return fmt::format_to(ctx.out(), "{:<{}}", fmt::format("{}", s), max_width);
|
||||||
case '=':
|
case '=':
|
||||||
format = "{coefficient:<{number_width}.1f}{padding}{prefix}{unit}";
|
format = "{coefficient:<{number_width}.{fixed_precision}f}{padding}{prefix}{unit}";
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
format = "{coefficient:.1f}{prefix}{unit}";
|
format = "{coefficient:.{fixed_precision}f}{prefix}{unit}";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return fmt::format_to(
|
return fmt::format_to(
|
||||||
ctx.out(), fmt::runtime(format), fmt::arg("coefficient", fraction),
|
ctx.out(), fmt::runtime(format), fmt::arg("coefficient", fraction),
|
||||||
|
fmt::arg("fixed_precision", fixed_precision),
|
||||||
fmt::arg("number_width", number_width),
|
fmt::arg("number_width", number_width),
|
||||||
fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : "")),
|
fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : "")),
|
||||||
fmt::arg("unit", s.unit_),
|
fmt::arg("unit", s.unit_),
|
||||||
|
|
|
@ -368,7 +368,7 @@ auto waybar::modules::Network::update() -> void {
|
||||||
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
||||||
fmt::arg("bandwidthTotalBytes",
|
fmt::arg("bandwidthTotalBytes",
|
||||||
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
|
||||||
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s")));
|
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s", false, true)));
|
||||||
if (text.compare(label_.get_label()) != 0) {
|
if (text.compare(label_.get_label()) != 0) {
|
||||||
label_.set_markup(text);
|
label_.set_markup(text);
|
||||||
if (text.empty()) {
|
if (text.empty()) {
|
||||||
|
@ -402,7 +402,7 @@ auto waybar::modules::Network::update() -> void {
|
||||||
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
|
||||||
fmt::arg("bandwidthTotalBytes",
|
fmt::arg("bandwidthTotalBytes",
|
||||||
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
|
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
|
||||||
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s")));
|
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s", false, true)));
|
||||||
if (label_.get_tooltip_text() != tooltip_text) {
|
if (label_.get_tooltip_text() != tooltip_text) {
|
||||||
label_.set_tooltip_markup(tooltip_text);
|
label_.set_tooltip_markup(tooltip_text);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue