Skip to content

Commit

Permalink
custom formatter for numbers in 'pow' units format
Browse files Browse the repository at this point in the history
  • Loading branch information
layus committed Sep 24, 2019
1 parent 89ffbe8 commit 05e8da1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 38 deletions.
67 changes: 64 additions & 3 deletions include/util/format.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
#pragma once

#include <sstream>
#include <fmt/format.h>

namespace waybar::util {
class pow_format {
public:
pow_format(long long val, std::string&& unit, bool binary = false):
val_(val), unit_(unit), binary_(binary) { };

std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false);
long long val_;
std::string unit_;
bool binary_;
};


namespace fmt {
template <>
struct formatter<pow_format> {
char spec = 0;

template <typename ParseContext>
constexpr auto parse(ParseContext& ctx) -> decltype (ctx.begin()) {
auto it = ctx.begin();
if (it != ctx.end() && *it == ':') ++it;
if (*it == '>' || *it == '<' || *it == '=') {
spec = *it;
++it;
}
return it;
}

template<class FormatContext>
auto format(const pow_format& s, FormatContext &ctx) -> decltype (ctx.out()) {
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr};

auto base = s.binary_ ? 1024ull : 1000ll;
auto fraction = (double) s.val_;

int pow;
for (pow = 0; units[pow+1] != nullptr && fraction / base >= 1; ++pow) {
fraction /= base;
}

const char * format;
std::string string;
switch (spec) {
case '>':
case '<':
return format_to(ctx.out()
, spec == '<' ? "{:<{}}" : "{:>{}}"
, fmt::format("{}", s)
, 4 /*coeff in :.3g format*/ + 1 /*prefix*/ + s.binary_ + s.unit_.length()
);
case '=':
format = "{coefficient:<4.3g}{padding}{prefix}{unit}";
break;
case 0:
default:
format = "{coefficient:.3g}{prefix}{unit}";
}
return format_to(ctx.out(), format
, fmt::arg("coefficient", fraction)
, fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : ""))
, fmt::arg("unit", s.unit_)
, fmt::arg("padding", pow ? "" : s.binary_ ? " " : " ")
);
}
};
}

1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ src_files = files(
'src/modules/disk.cpp',
'src/modules/idle_inhibitor.cpp',
'src/modules/temperature.cpp',
'src/util/format.cpp',
'src/main.cpp',
'src/bar.cpp',
'src/client.cpp'
Expand Down
34 changes: 26 additions & 8 deletions src/modules/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,33 @@ auto waybar::modules::Disk::update() -> void {
return;
}

label_.set_markup(fmt::format(format_,
stats.f_bavail * 100 / stats.f_blocks,
fmt::arg("free", pow_format(stats.f_bavail * stats.f_bsize, "B", true)),
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks),
fmt::arg("used", pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true)),
fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
));
auto free = pow_format(stats.f_bavail * stats.f_bsize, "B", true);
auto used = pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true);
auto total = pow_format(stats.f_blocks * stats.f_bsize, "B", true);

label_.set_markup(fmt::format(format_
, stats.f_bavail * 100 / stats.f_blocks
, fmt::arg("free", free)
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
, fmt::arg("used", used)
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
, fmt::arg("total", total)
, fmt::arg("path", path_)
));
if (tooltipEnabled()) {
label_.set_tooltip_text(fmt::format("{} used", pow_format(stats.f_bavail * stats.f_bsize, "B", true)));
std::string tooltip_format = "{used} out of {total} used on ({percentage_used}%)";
if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
label_.set_tooltip_text(fmt::format(tooltip_format
, stats.f_bavail * 100 / stats.f_blocks
, fmt::arg("free", free)
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
, fmt::arg("used", used)
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
, fmt::arg("total", total)
, fmt::arg("path", path_)
));
}
event_box_.show();
}
26 changes: 0 additions & 26 deletions src/util/format.cpp

This file was deleted.

0 comments on commit 05e8da1

Please sign in to comment.