Improve error message for mixed arg indexing in format string

This commit is contained in:
Lukas Fink 2024-09-18 17:28:58 +02:00
parent de170fa579
commit 254111ff91
1 changed files with 42 additions and 34 deletions

View File

@ -159,43 +159,51 @@ auto waybar::modules::Custom::update() -> void {
parseOutputRaw(); parseOutputRaw();
} }
auto str = fmt::format(fmt::runtime(format_), fmt::arg("text", text_), fmt::arg("alt", alt_), try {
fmt::arg("icon", getIcon(percentage_, alt_)), auto str = fmt::format(fmt::runtime(format_), fmt::arg("text", text_), fmt::arg("alt", alt_),
fmt::arg("percentage", percentage_)); fmt::arg("icon", getIcon(percentage_, alt_)),
if ((config_["hide-empty-text"].asBool() && text_.empty()) || str.empty()) { fmt::arg("percentage", percentage_));
event_box_.hide(); if ((config_["hide-empty-text"].asBool() && text_.empty()) || str.empty()) {
} else { event_box_.hide();
label_.set_markup(str); } else {
if (tooltipEnabled()) { label_.set_markup(str);
if (tooltip_format_enabled_) { if (tooltipEnabled()) {
auto tooltip = config_["tooltip-format"].asString(); if (tooltip_format_enabled_) {
tooltip = fmt::format(fmt::runtime(tooltip), fmt::arg("text", text_), auto tooltip = config_["tooltip-format"].asString();
fmt::arg("alt", alt_), fmt::arg("icon", getIcon(percentage_, alt_)), tooltip = fmt::format(fmt::runtime(tooltip), fmt::arg("text", text_),
fmt::arg("percentage", percentage_)); fmt::arg("alt", alt_), fmt::arg("icon", getIcon(percentage_, alt_)),
label_.set_tooltip_markup(tooltip); fmt::arg("percentage", percentage_));
} else if (text_ == tooltip_) { label_.set_tooltip_markup(tooltip);
if (label_.get_tooltip_markup() != str) { } else if (text_ == tooltip_) {
label_.set_tooltip_markup(str); if (label_.get_tooltip_markup() != str) {
} label_.set_tooltip_markup(str);
} else { }
if (label_.get_tooltip_markup() != tooltip_) { } else {
label_.set_tooltip_markup(tooltip_); if (label_.get_tooltip_markup() != tooltip_) {
label_.set_tooltip_markup(tooltip_);
}
} }
} }
auto style = label_.get_style_context();
auto classes = style->list_classes();
for (auto const& c : classes) {
if (c == id_) continue;
style->remove_class(c);
}
for (auto const& c : class_) {
style->add_class(c);
}
style->add_class("flat");
style->add_class("text-button");
style->add_class(MODULE_CLASS);
event_box_.show();
} }
auto style = label_.get_style_context(); } catch (const fmt::format_error& e) {
auto classes = style->list_classes(); if (std::strcmp(e.what(), "cannot switch from manual to automatic argument indexing") != 0)
for (auto const& c : classes) { throw;
if (c == id_) continue;
style->remove_class(c); throw fmt::format_error("mixing manual and automatic argument indexing is no longer supported; "
} "try replacing \"{}\" with \"{text}\" in your format specifier");
for (auto const& c : class_) {
style->add_class(c);
}
style->add_class("flat");
style->add_class("text-button");
style->add_class(MODULE_CLASS);
event_box_.show();
} }
} }
// Call parent update // Call parent update