feat(custom): Add format-icons to custom module

This commit allows custom modules (json only) to set a percentage. This can be displayed either by using {percentage} or by using {icon} with format-icons set.
This commit is contained in:
Robinhuett 2018-12-26 03:52:05 +01:00
parent 76bbdd0425
commit 11c98f13e3
2 changed files with 23 additions and 1 deletions

View File

@ -19,12 +19,14 @@ class Custom : public ALabel {
void continuousWorker();
void parseOutputRaw();
void parseOutputJson();
bool isInteger(const std::string&);
const std::string name_;
std::string text_;
std::string tooltip_;
std::string class_;
std::string prevclass_;
int percentage_;
waybar::util::SleeperThread thread_;
waybar::util::command::res output_;
waybar::util::JsonParser parser_;

View File

@ -87,7 +87,9 @@ auto waybar::modules::Custom::update() -> void
parseOutputRaw();
}
auto str = fmt::format(format_, text_);
auto str = fmt::format(format_, text_,
fmt::arg("icon", getIcon(percentage_)),
fmt::arg("percentage", percentage_));
label_.set_markup(str);
if (text_ == tooltip_) {
label_.set_tooltip_text(str);
@ -130,6 +132,19 @@ void waybar::modules::Custom::parseOutputRaw()
}
}
bool waybar::modules::Custom::isInteger(const std::string& n)
{
if (std::isdigit(n[0]) || (n.size() > 1 && (n[0] == '-' || n[0] == '+'))) {
for (std::string::size_type i{ 1 }; i < n.size(); ++i) {
if (!std::isdigit(n[i])) {
return false;
}
}
return true;
}
return false;
}
void waybar::modules::Custom::parseOutputJson()
{
std::istringstream output(output_.out);
@ -139,6 +154,11 @@ void waybar::modules::Custom::parseOutputJson()
text_ = parsed["text"].asString();
tooltip_ = parsed["tooltip"].asString();
class_ = parsed["class"].asString();
if (!parsed["percentage"].asString().empty() && isInteger(parsed["percentage"].asString())) {
percentage_ = std::stoi(parsed["percentage"].asString(), nullptr);
} else {
percentage_ = 0;
}
break;
}
}