Reimplement format & getIcon

pull/1722/head
Samuel Grahn 2022-11-27 17:13:47 +01:00
parent 70fa0a26d9
commit 2b8c9d845b
2 changed files with 34 additions and 7 deletions

View File

@ -33,7 +33,6 @@ class Custom : public AModule {
std::string text_; std::string text_;
std::string alt_; std::string alt_;
std::string tooltip_; std::string tooltip_;
std::string format_;
std::string onclick_; std::string onclick_;
bool hide_; bool hide_;
std::vector<std::string> class_; std::vector<std::string> class_;
@ -42,7 +41,6 @@ class Custom : public AModule {
text_(""), text_(""),
alt_(""), alt_(""),
tooltip_(""), tooltip_(""),
format_(""),
onclick_(""), onclick_(""),
hide_(false), hide_(false),
class_(std::vector<std::string>()), class_(std::vector<std::string>()),
@ -59,6 +57,7 @@ class Custom : public AModule {
void handleClick(std::string name); void handleClick(std::string name);
Node parseItem(Json::Value &parsed); Node parseItem(Json::Value &parsed);
Gtk::Button &addButton(const Node &node); Gtk::Button &addButton(const Node &node);
std::string getIcon(uint16_t percentage, const std::string& alt = "", uint16_t max = 0);
const std::chrono::seconds interval_; const std::chrono::seconds interval_;
util::command::res output_; util::command::res output_;
@ -68,6 +67,7 @@ class Custom : public AModule {
std::unordered_map<std::string, Gtk::Button> buttons_; std::unordered_map<std::string, Gtk::Button> buttons_;
Gtk::Box box_; Gtk::Box box_;
std::string format_;
FILE* fp_; FILE* fp_;
int pid_; int pid_;

View File

@ -9,6 +9,7 @@ waybar::modules::Custom::Custom(const std::string& name,
: AModule(config, "custom-" + name, id, false, !config["disable-scroll"].asBool()), : AModule(config, "custom-" + name, id, false, !config["disable-scroll"].asBool()),
interval_(config["interval"].isUInt() ? config_["interval"].asUInt() : 0), interval_(config["interval"].isUInt() ? config_["interval"].asUInt() : 0),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
format_(config["format"].isString() ? config["format"].asString() : "{}"),
fp_(nullptr), fp_(nullptr),
pid_(-1) { pid_(-1) {
box_.set_name("custom-" + name); box_.set_name("custom-" + name);
@ -142,11 +143,9 @@ auto waybar::modules::Custom::update() -> void {
auto &button = bit == buttons_.end() ? addButton(*res) : bit->second; auto &button = bit == buttons_.end() ? addButton(*res) : bit->second;
auto str = res->text_; auto str = fmt::format(format_, res->text_, fmt::arg("alt", res->alt_),
//auto str = fmt::format(res->format_, res->text_, fmt::arg("alt", res->alt_), fmt::arg("icon", getIcon(res->percentage_, res->alt_)),
//fmt::arg("icon", getIcon(res->percentage_, res->alt_)), TODO fmt::arg("percentage", res->percentage_));
// fmt::arg("percentage", res->percentage_));
if(str.empty() || res->hide_) { if(str.empty() || res->hide_) {
button.hide(); button.hide();
} else { } else {
@ -202,6 +201,28 @@ auto waybar::modules::Custom::update() -> void {
AModule::update(); AModule::update();
} }
std::string waybar::modules::Custom::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) {
auto format_icons = config_["format-icons"];
if (format_icons.isObject()) {
if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) {
format_icons = format_icons[alt];
} else {
format_icons = format_icons["default"];
}
}
if (format_icons.isArray()) {
auto size = format_icons.size();
if (size) {
auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1);
format_icons = format_icons[idx];
}
}
if (format_icons.isString()) {
return format_icons.asString();
}
return "";
}
void waybar::modules::Custom::parseOutputRaw() { void waybar::modules::Custom::parseOutputRaw() {
Node n = Node(); Node n = Node();
// Retain name if there is only one node // Retain name if there is only one node
@ -262,6 +283,12 @@ waybar::modules::Custom::Node waybar::modules::Custom::parseItem(Json::Value &pa
if (!parsed["onclick"].asString().empty() && parsed["onclick"].isString()) { if (!parsed["onclick"].asString().empty() && parsed["onclick"].isString()) {
n.onclick_ = parsed["onclick"].asString(); n.onclick_ = parsed["onclick"].asString();
} }
if(parsed["hide"].isBool()) {
n.hide_ = parsed["hide"].asBool();
}
return n; return n;
} }