Skip to content

Commit 05e8da1

Browse files
committed
custom formatter for numbers in 'pow' units format
1 parent 89ffbe8 commit 05e8da1

File tree

4 files changed

+90
-38
lines changed

4 files changed

+90
-38
lines changed

include/util/format.hpp

+64-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
11
#pragma once
22

3-
#include <sstream>
3+
#include <fmt/format.h>
44

5-
namespace waybar::util {
5+
class pow_format {
6+
public:
7+
pow_format(long long val, std::string&& unit, bool binary = false):
8+
val_(val), unit_(unit), binary_(binary) { };
69

7-
std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false);
10+
long long val_;
11+
std::string unit_;
12+
bool binary_;
13+
};
814

15+
16+
namespace fmt {
17+
template <>
18+
struct formatter<pow_format> {
19+
char spec = 0;
20+
21+
template <typename ParseContext>
22+
constexpr auto parse(ParseContext& ctx) -> decltype (ctx.begin()) {
23+
auto it = ctx.begin();
24+
if (it != ctx.end() && *it == ':') ++it;
25+
if (*it == '>' || *it == '<' || *it == '=') {
26+
spec = *it;
27+
++it;
28+
}
29+
return it;
30+
}
31+
32+
template<class FormatContext>
33+
auto format(const pow_format& s, FormatContext &ctx) -> decltype (ctx.out()) {
34+
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr};
35+
36+
auto base = s.binary_ ? 1024ull : 1000ll;
37+
auto fraction = (double) s.val_;
38+
39+
int pow;
40+
for (pow = 0; units[pow+1] != nullptr && fraction / base >= 1; ++pow) {
41+
fraction /= base;
42+
}
43+
44+
const char * format;
45+
std::string string;
46+
switch (spec) {
47+
case '>':
48+
case '<':
49+
return format_to(ctx.out()
50+
, spec == '<' ? "{:<{}}" : "{:>{}}"
51+
, fmt::format("{}", s)
52+
, 4 /*coeff in :.3g format*/ + 1 /*prefix*/ + s.binary_ + s.unit_.length()
53+
);
54+
case '=':
55+
format = "{coefficient:<4.3g}{padding}{prefix}{unit}";
56+
break;
57+
case 0:
58+
default:
59+
format = "{coefficient:.3g}{prefix}{unit}";
60+
}
61+
return format_to(ctx.out(), format
62+
, fmt::arg("coefficient", fraction)
63+
, fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : ""))
64+
, fmt::arg("unit", s.unit_)
65+
, fmt::arg("padding", pow ? "" : s.binary_ ? " " : " ")
66+
);
67+
}
68+
};
969
}
70+

meson.build

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ src_files = files(
9191
'src/modules/disk.cpp',
9292
'src/modules/idle_inhibitor.cpp',
9393
'src/modules/temperature.cpp',
94-
'src/util/format.cpp',
9594
'src/main.cpp',
9695
'src/bar.cpp',
9796
'src/client.cpp'

src/modules/disk.cpp

+26-8
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,33 @@ auto waybar::modules::Disk::update() -> void {
4444
return;
4545
}
4646

47-
label_.set_markup(fmt::format(format_,
48-
stats.f_bavail * 100 / stats.f_blocks,
49-
fmt::arg("free", pow_format(stats.f_bavail * stats.f_bsize, "B", true)),
50-
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks),
51-
fmt::arg("used", pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true)),
52-
fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
53-
));
47+
auto free = pow_format(stats.f_bavail * stats.f_bsize, "B", true);
48+
auto used = pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true);
49+
auto total = pow_format(stats.f_blocks * stats.f_bsize, "B", true);
50+
51+
label_.set_markup(fmt::format(format_
52+
, stats.f_bavail * 100 / stats.f_blocks
53+
, fmt::arg("free", free)
54+
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
55+
, fmt::arg("used", used)
56+
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
57+
, fmt::arg("total", total)
58+
, fmt::arg("path", path_)
59+
));
5460
if (tooltipEnabled()) {
55-
label_.set_tooltip_text(fmt::format("{} used", pow_format(stats.f_bavail * stats.f_bsize, "B", true)));
61+
std::string tooltip_format = "{used} out of {total} used on ({percentage_used}%)";
62+
if (config_["tooltip-format"].isString()) {
63+
tooltip_format = config_["tooltip-format"].asString();
64+
}
65+
label_.set_tooltip_text(fmt::format(tooltip_format
66+
, stats.f_bavail * 100 / stats.f_blocks
67+
, fmt::arg("free", free)
68+
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
69+
, fmt::arg("used", used)
70+
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
71+
, fmt::arg("total", total)
72+
, fmt::arg("path", path_)
73+
));
5674
}
5775
event_box_.show();
5876
}

src/util/format.cpp

-26
This file was deleted.

0 commit comments

Comments
 (0)